Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="app"></div>
              
            
!

CSS

              
                $width: 12vw;
@keyframes hit {
  0% {
    background: hsla(60, 96%, 55%, 0.4);
  }
  50% {
    background: hsla(60, 96%, 55%, 0.8);
  }
  100% {
    background: hsla(60, 96%, 55%, 0.4);
  }
}

body {
  font-family: "Cinzel", serif;
  font-size: 48px;
  margin: 10% 0 0;
}

main {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  margin: 0 auto;
  width: $width * 4;
}

.student-wrapper {
  width: $width;
  height: calc(#{$width} * 1.47);
  position: relative;
  user-select: none;

  img {
    position: absolute;
    -webkit-user-drag: none;
    -khtml-user-drag: none;
    -moz-user-drag: none;
    -o-user-drag: none;
    user-drag: none;
  }

  &__chair {
    bottom: 0;
    left: 26%;
    width: 50%;
    z-index: 10;
  }
  &__student {
    bottom: 0;
    left: 27%;
    width: 47%;
    z-index: 50;
    transition: bottom 200ms linear;

    &[data-standing="true"] {
      cursor: pointer;
      z-index: 200;
      bottom: 40%;
    }

    &[data-hit="true"] {
      animation: hit linear 20ms 4;
    }
  }
  &__table {
    bottom: 0px;
    z-index: 100;
  }
}

.quote {
  font-size: 22px;
  font-family: "Cardo", serif;
  max-width: 400px;
  text-align: center;
}

#killscreen {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

#timer {
  background: linear-gradient(90deg,  (rgb(224, 120, 0), rgb(248, 224, 152)));
  position: absolute;
  top: 0px;
  left: 0px;
  width: 100%;
  height: 20px;
  border: 1px solid #555;
}

#score {
  position: absolute;
  top: 20px;
  right: 20px;
}

#finalscore {
  display: block;
  font-size: 64px;
}

              
            
!

JS

              
                const MY_CAPTAIN = 100;
const GAME_LENGTH = 45;
const FINALS = [
  `Show me the heart unfettered by foolish dreams and I'll show you a happy man.`,
  `There's a time for daring and there's a time for caution, and a wise man understands which is called for.`,
  `What will your verse be?`,
  `No matter what anybody tells you, words and ideas can change the world.`,
  `To put to rout all that was not life; and not, when I had come to die, discover that I had not lived.`,
  `This is a battle, a war, and the casualties could be your hearts and souls.`,
  `Carpe diem!`,
  `We're not laughing at you. We're laughing near you.`
];

class Student extends React.Component {
  constructor(props) {
    super(props);
    const possibleStudents = [
      "https://s3-us-west-2.amazonaws.com/s.cdpn.io/159921/student1.svg",
      "https://s3-us-west-2.amazonaws.com/s.cdpn.io/159921/student2.svg",
      "https://s3-us-west-2.amazonaws.com/s.cdpn.io/159921/student3.svg"
    ];
    this.state = {
      hit: false,
      image: possibleStudents[getRandomInt(0, 3)],
      standing: false,
      startedStanding: null
    };
  }

  componentDidMount() {
    this.sitStandCycle();
  }

  componentWillUnmount() {
    clearTimeout(this.timeout);
  }

  handleWhack = () => {
    // Ignore click on sitting students
    if (!this.state.standing) {
      return false;
    }
    clearTimeout(this.timeout);
    this.setState(
      {
        hit: true,
        standing: !this.state.standing
      },
      () => this.sitStandCycle()
    );
    this.props.updateScore();
  };


  sitStandCycle = () => {
    this.timeout = setTimeout(() => {
      this.setState(
        {
          hit: false,
          standing: !this.state.standing
        },
        () => {
          this.sitStandCycle();
        }
      );
    }, getRandomInt(1000, 4000));
  };

  render() {
    return (
      <div className="student-wrapper" onClick={this.handleWhack}>
        <img
          src={this.state.image}
          className="student-wrapper__student"
          data-hit={this.state.hit}
          data-standing={this.state.standing}       
        />
        <img
          src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/159921/table.svg"
          className="student-wrapper__table"
        />
        <img
          src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/159921/chair.svg"
          className="student-wrapper__chair"
        />
      </div>
    );
  }
}

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      score: 0,
      timer: GAME_LENGTH
    };
  }

  componentDidMount() {
    const timer = setInterval(() => {
      this.setState(
        {
          timer: this.state.timer - 1
        },
        () => {
          if (this.state.timer <= 0) {
            clearInterval(timer);
          }
        }
      );
    }, 1000);
  }

  buildStudents = () => {
    let students = [];
    for (let i = 0; i < 8; i++) {
      students = [
        <Student key={i} updateScore={this.updateScore} />,
        ...students
      ];
    }

    return students;
  };

  updateScore = () => {
    this.setState({
      score: this.state.score + MY_CAPTAIN
    });
  };

  render() {
    if (this.state.timer <= 0) {
      return (
        <div id="killscreen">
          <span id="finalscore">{this.state.score}</span>
          <p className="quote">{FINALS[getRandomInt(0, FINALS.length)]}</p>
        </div>
      );
    }
    return (
      <main>
        <div id="timer" style={{width: `${(this.state.timer/GAME_LENGTH) * 100}%`}}></div>
        <div id="score">{this.state.score}</div>
        {this.buildStudents()}
      </main>
    );
  }
}

// Help!
function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
}

ReactDOM.render(<App />, document.getElementById("app"));

              
            
!
999px

Console