Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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

              
                #app
  .m-boardContainer
    .m-score
      .a-score.-ai#aiScore 0
      .a-score.-separator -
      .a-score.-player#playerScore 0
    .m-board#board
      .m-pad#ai
      .m-ball#ball
      .m-pad#player
  .m-logoOverlay
    .m-logoContainer
      svg.m-logo(viewBox="-25 -25 440 250")
        each val in ['-layer2', '-layer1', '']
          path(class="a-logo " + val,d="M 20.332343,102.32138 V 42.69771 h 21.469863 c 23.563267,0 24.733656,0.124282 31.213907,3.314499 11.426637,5.625313 20.007563,18.407598 21.060469,31.372029 0.187612,2.310118 0.511247,4.305358 0.719185,4.43387 0.207938,0.128512 0.678755,-0.79034 1.046269,-2.041892 0.905481,-3.083601 4.715934,-10.728732 7.319674,-14.685904 2.98187,-4.531853 11.63684,-13.091114 16.45349,-16.271549 10.46258,-6.908441 19.6026,-9.834264 32.10505,-10.277189 11.28786,-0.3999 19.8448,1.317078 29.40715,5.900628 8.3399,3.997597 18.61919,12.770376 23.56167,20.108529 4.73444,7.029286 8.72342,16.142157 9.56571,21.853022 0.80529,5.459975 1.38936,4.002799 1.40581,-3.507275 0.0189,-8.652972 1.01599,-13.677629 3.82046,-19.2535 12.94704,-25.741493 48.96438,-31.033075 69.41843,-10.198794 7.9057,8.052671 11.3524,17.349162 11.51967,31.071037 0.054,4.433681 0.1565,5.059066 0.57462,3.507274 0.27986,-1.038693 1.00165,-3.709617 1.60397,-5.935389 4.01696,-14.844067 15.48949,-29.025986 29.61227,-36.605623 7.84789,-4.211924 14.74639,-6.289359 23.84945,-7.1821 6.31618,-0.619428 15.26247,-0.0205 19.89868,1.332187 l 2.38603,0.696156 -0.18843,12.920876 c -0.10363,7.106479 -0.23393,12.970243 -0.28957,13.030579 -0.0556,0.06034 -1.79192,-0.536144 -3.85843,-1.325523 -3.46475,-1.32349 -4.38744,-1.435123 -11.85099,-1.43378 -7.55971,0.0011 -8.36072,0.101841 -12.14057,1.522972 -22.46837,8.447563 -31.4979,33.850244 -19.30411,54.30803 4.29025,7.19784 12.69329,13.66301 21.04502,16.19171 2.41071,0.72991 3.24078,0.80349 3.43235,0.30427 0.13896,-0.36215 0.18519,-4.28997 0.10271,-8.72847 l -0.14995,-8.07 -5.39581,-0.26979 -5.39581,-0.26979 -0.14657,-10.39432 -0.14657,-10.394315 17.68294,0.142282 17.68295,0.142282 0.12216,30.313771 0.12217,30.31377 -1.74091,0.56232 c -0.9575,0.30927 -3.89731,0.93324 -6.53292,1.38659 -14.89651,2.56238 -31.11093,-1.00109 -44.3768,-9.75278 -7.4024,-4.88347 -16.23707,-14.948 -20.37536,-23.21177 -2.09638,-4.18625 -5.18776,-12.93048 -5.18776,-14.674 0,-0.59968 -0.18211,-1.53511 -0.40469,-2.07873 -0.22257,-0.54362 -0.52609,10.18093 -0.67447,23.83232 l -0.26979,24.82071 h -12.26813 -12.26814 l -0.15477,-40.19876 c -0.17571,-45.633949 0.0884,-42.932808 -4.71215,-48.185422 -3.48829,-3.81674 -6.78929,-5.416835 -11.71875,-5.680425 -8.54766,-0.457063 -15.2867,4.392581 -17.71599,12.74903 -0.7756,2.667979 -0.85771,7.397751 -0.73194,42.160347 l 0.14167,39.15523 h -12.3732 -12.3732 l -0.26979,-25.0905 c -0.23544,-21.89591 -0.35866,-24.747 -0.96782,-22.3926 -3.85134,14.88547 -8.76549,23.652 -18.3871,32.80139 -11.70305,11.12868 -25.54725,16.57025 -42.15729,16.57025 -8.05863,0 -13.24171,-0.79714 -19.96448,-3.07045 -12.80062,-4.32854 -24.79103,-13.73691 -32.00731,-25.11475 -6.557316,-10.33888 -9.589204,-20.92122 -9.580285,-33.438507 0.0027,-3.49991 -0.06585,-6.36347 -0.151876,-6.36347 -0.08602,0 -1.098095,1.881788 -2.249048,4.181751 -2.927495,5.850056 -9.64915,12.572816 -15.635156,15.637706 -6.64559,3.4026 -9.880921,4.06825 -20.773856,4.27403 l -9.442664,0.17839 -0.140879,22.12744 -0.140884,22.12744 H 32.197136 20.332343 Z m 140.378417,34.58074 c 10.05243,-1.93413 18.22041,-7.13492 24.06176,-15.32083 5.20089,-7.2884 7.82108,-17.49536 6.6465,-25.891568 -1.5125,-10.811865 -6.81534,-20.023039 -14.79037,-25.691287 -6.18086,-4.393029 -9.2894,-5.703044 -16.95991,-7.147308 -4.35481,-0.819958 -5.59911,-0.8553 -9.62401,-0.273357 -13.09428,1.893233 -23.25542,8.869332 -29.20255,20.048918 -6.62692,12.457468 -5.38593,28.297202 3.10619,39.646722 8.63402,11.53917 23.03536,17.26984 36.76239,14.62871 z M 62.689428,93.038141 c 5.034402,-2.668475 7.993646,-7.619344 7.997013,-13.379149 0.0027,-4.558362 -1.27777,-7.813464 -4.237932,-10.773631 -3.561411,-3.561405 -5.919058,-4.285328 -14.685589,-4.509228 l -7.689025,-0.19638 v 15.216512 15.216508 l 8.228606,-0.215304 c 7.086228,-0.185421 8.528247,-0.374134 10.386927,-1.359328 z")
      button(type="button", class="a-button", id="start") start
.a-instructions use up and down arrow keys to play
              
            
!

CSS

              
                @import url("https://fonts.googleapis.com/css?family=Exo|Dosis&text=START0123456789-use up and down arrow keys to play");

$color-logo: #010101;
$color-01: #f15d21;
$color-02: #f6790b;
$color-03: #facb03;

html {
  font-size: 16px;
}
body {
  height: 100vh;
  background-color: black;
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}
.m-logoOverlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  background-color: rgba(#000, 0.5);
  transition: background-color .4s ease-in-out;
  
  #app.-gameOn & {
    background-color: rgba(#000, 0);
  }
}
.m-logoContainer {
  flex: 0 1 50%;
  text-align: center;
}
.m-logo {
  width: 60vw;
  position: relative;
  opacity: 1;
  top: 0;
  transition: all .4s ease-in-out;
  
  #app.-gameOn & {
    top: -100vh;
    opacity: 0;
  }
}
.a-button {
  position: relative;
  opacity: 1;
  top: 0;
  background-color: $color-01;
  color: $color-logo;
  border: .5rem solid $color-02;
  box-shadow: 0 0 0 .5rem $color-03;
  font-size: 2rem;
  border-radius: 2rem;
  text-transform: uppercase;
  font-family: "Exo";
  padding: 0 2rem;
  transition: all .4s ease-in-out;
  
  &:focus, &:active {
    outline:none;
  }
  
  #app.-gameOn & {
    top: 100vw;
    opacity: 0;
  }
}
.a-logo {
  fill: $color-logo;
  stroke-width: 0;
  stroke: $color-logo;
  stroke-opacity: 1;
  stroke-miterlimit: 4;
  stroke-dasharray: none;

  &.-layer1 {
    fill: $color-02;
    stroke-width: 1rem;
    stroke: $color-02;
  }

  &.-layer2 {
    fill: $color-03;
    stroke-width: 2rem;
    stroke: $color-03;
  }
  &.-layer3 {
    fill: $color-03;
    stroke-width: 3rem;
    stroke: $color-03;
  }
}

$cube-color: rgba($color-02, .8);
$ball-color: rgba($color-01, .8);
#app {
  perspective: 1000px;
  perspective-origin: 50% 100px;
  transition: perspective .4s ease-in-out;  
}
.m-boardContainer {
  position: relative;
  
  transform-style: preserve-3d;
  transition: transform .4s ease-in-out;
  
   #app.-gameOn & {
     transform: rotateX(-20deg) rotateY(-35deg) rotateZ(0deg);
  }
}
.m-board {
  width: 60vmin;
  height: 60vmin;
  transform-style: preserve-3d;
  $grid-color: rgba(#fff, 0.4);
  position: relative;
  
  background-color: $color-03;
  background-image:
    linear-gradient(
      transparent,
      transparent 10%,
      $grid-color 10%,
      $grid-color 90%,
      transparent 90%,
      transparent
    );
  background-repeat: repeat-y;
  background-size: 4px 20px;
  background-position: center;
  border: 1rem solid $color-01;
}
.m-cube {
  width: 100%;
  padding-top: 100%;
  position: relative;
  transform-style: preserve-3d;
}

.a {
  &-cube {
    &__face {
      margin: 0;
      top: 1px;
      left: 1px;
      bottom: 1px;
      right: 1px;
      display: block;
      position: absolute;
      background-color: $cube-color;

      .m-ball & {
        background-color: $ball-color;
      }
      .m-ball.-blink & {
        animation: blink 1s infinite ease-in-out; 
      }

      &.-front {
        transform: rotateY(-90deg) translateX(100%) rotateY(90deg);
        background-color: lighten($cube-color, 20);

        .m-ball & {
          background-color: lighten($ball-color, 20);
        }
      }
      &.-back {
        transform: translateZ(0) rotateY(180deg);
        box-shadow: -2vmin 0 0 0 rgba(#000, 0.4);
      }
      &.-right {
        transform: rotateY(-90deg) translateX(100%) rotateY(-180deg)
          translateX(100%);
        transform-origin: top right;
        background-color: darken($cube-color, 80);
        .m-ball & {
          background-color: darken($ball-color, 80);
        }
      }
      &.-left {
        transform: rotateY(-90deg) translateX(100%) rotateY(360deg)
          translateX(-100%);
        transform-origin: center left;
        background-color: lighten($cube-color, 20);
        .m-ball & {
          background-color: lighten($ball-color, 20);
        }
      }
      &.-top {
        transform: rotateY(-90deg) translateX(50%) rotateY(90deg) rotateX(-90deg) translateY(-50%);
        transform-origin: top center;
      }
      &.-bottom {
        transform: rotateY(-90deg) translateX(50%) rotateY(90deg) rotateX(90deg) translateY(50%);
        transform-origin: bottom center;
      }
    }
  }
}
.m-pad,
.m-ball {
  position: absolute;
  transform-style: preserve-3d;
}
.m-ball {
  height: calc(100% / 17);
  width: calc(100% / 17);
  
  &.-reset {
    transition: all 1s ease-in-out;
  }
}
.m-pad {
  height: calc(100% / 17 * 5);
  width: calc(100% / 17);

  display: flex;
  flex-wrap: wrap-reverse;
}
.m-score {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  margin: 0 0 2rem;
}
.a-score {
  flex-grow: 0;
  flex-shrink: 0;
  font-family: 'Dosis';
  color: white;
  font-size: 5rem;
  
  &.-ai {
    flex-basis: 40%;
    text-align: left;
  }
  &.-separator {
    flex-basis: 20%;
    text-align: center;
  }
  &.-player {
    flex-basis: 40%;
    text-align: right;
  }
}

.a-instructions {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  text-align: center;
  color: white;
  font-family: 'Exo';
  font-size: 1rem;
  line-height: 1.4rem;
}


@keyframes blink {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0;
  }
}
              
            
!

JS

              
                const CubeFace = modifier => {
  const cubeFace = document.createElement("div");
  cubeFace.classList.add("a-cube__face");
  if (modifier) {
    cubeFace.classList.add(modifier);
  }

  return cubeFace;
};

const Cube = () => {
  const cube = document.createElement("div");
  cube.classList.add("m-cube");
  cube.appendChild(CubeFace("-front"));
  cube.appendChild(CubeFace("-right"));
  cube.appendChild(CubeFace("-back"));
  cube.appendChild(CubeFace("-left"));
  cube.appendChild(CubeFace("-top"));
  cube.appendChild(CubeFace("-bottom"));
  return cube;
};

class Pad {
  constructor(id, lenght, multiplier, position) {
    this.elem = document.getElementById(id);
    this.position = position;
    this.lenght = lenght;
    this.multiplier = multiplier;
  }
  update() {
    this.elem.style.top = `${this.position * this.multiplier}%`;
  }
  draw() {
    for (let i = 0; i < this.lenght; i += 1) {
      this.elem.appendChild(Cube());
    }
    this.update();
  }
}

class AiPad extends Pad {
  constructor(lenght, multiplier) {
    super("ai", lenght, multiplier, 6);
    this.elem.style.left = 0;
  }
}

class PlayerPad extends Pad {
  constructor(lenght, multiplier) {
    super("player", lenght, multiplier, 6);
    this.elem.style.right = 0;
  }
}

class Ball {
  constructor(multiplier) {
    this.elem = document.getElementById("ball");
    this.multiplier = multiplier;
    this.position = {
      x: 8,
      y: 8
    };
    this.vector = {
      x: Ball.getVectorSingleDirection(),
      y: Ball.getVectorSingleDirection()
    };
  }

  update() {
    this.elem.style.top = `${this.position.y * this.multiplier}%`;
    this.elem.style.left = `${this.position.x * this.multiplier}%`;
  }

  draw() {
    this.elem.appendChild(Cube());
    this.update();
  }
  
  initReset() {
    this.elem.classList.add('-blink');
    this.elem.classList.add('-reset');
  }
  endReset() {
    this.elem.classList.remove('-blink');
    this.elem.classList.remove('-reset');
  }
  resetPosition() {
    this.position.x = 8;
    this.position.y = 8;
    this.update();
  }

  static getVectorSingleDirection() {
    return Math.random() > 0.5 ? 1 : -1;
  }
}

class ScoreBoard {
  constructor() {
    this.score = {
      ai: 0,
      player: 0
    };
    this.aiScoreElem = document.getElementById("aiScore");
    this.playerScoreElem = document.getElementById("playerScore");
  }
  updateScore(key) {
    this.score[key] += 1;
    this.aiScoreElem.textContent = this.score.ai;
    this.playerScoreElem.textContent = this.score.player;
  }
  aiScored() {
    this.updateScore("ai");
  }
  playerScored() {
    this.updateScore("player");
  }
}

class App {
  constructor(id, size) {
    this.size = size;
    this.pixelSize = 100 / size;
    this.elem = document.getElementById(id);
    this.aiPad = new AiPad(5, this.pixelSize);
    this.playerPad = new PlayerPad(5, this.pixelSize);
    this.ball = new Ball(this.pixelSize);
    this.scoreBoard = new ScoreBoard();

    this.direction = {
      up: false,
      down: false
    };
    this.updatebleElements = [this.aiPad, this.playerPad, this.ball];

    this.start = this.start.bind(this);
    this.update = this.update.bind(this);
    this.updatePositions = this.updatePositions.bind(this);
    this.keyDown = this.keyDown.bind(this);
    this.resetBall = this.resetBall.bind(this);
  }

  draw() {
    this.updatebleElements.forEach(elem => elem.draw());
  }
  
  resetBall() {
    const ball = this.ball;
    ball.initReset();
    setTimeout(() => {
      ball.resetPosition();
      setTimeout(() => {
        ball.endReset();
        this.start();
      }, 2000);
    }, 1000);
  }

  updatePositions() {
    const {
      direction,
      playerPad,
      aiPad,
      ball,
      updateInterval,
      size,
      scoreBoard,
      resetBall
    } = this;
    let move = true;
    if (direction.up && playerPad.position > 0) {
      playerPad.position -= 1;
    }
    if (direction.down && playerPad.position < size - 5) {
      playerPad.position += 1;
    }
    if (ball.vector.x > 0) {
      if (ball.position.x === size - 2) {
        let diff = ball.position.y - playerPad.position;

        if (diff >= 0 && diff <= 4) {
          ball.vector.x = -1;
        }
        if (diff === -1 && ball.vector.y === 1) {
          ball.vector.y = -1;
          ball.vector.x = -1;
        }
        if (diff === 5 && ball.vector.y === -1) {
          ball.vector.y = 1;
          ball.vector.x = -1;
        }
      } else if (ball.position.x === size - 1) {
        clearInterval(updateInterval);
        this.updateInterval = null;
        scoreBoard.aiScored();
        resetBall(ball);
        move = false;
      }
    }
    if (ball.vector.x < 0) {
      if (ball.position.x === 1) {
        ball.vector.x = 1;
      }
    }

    if (ball.vector.y > 0) {
      if (ball.position.y === size - 1) {
        ball.vector.y = -1;
      }
    }
    if (ball.vector.y < 0) {
      if (ball.position.y === 0) {
        ball.vector.y = 1;
      }
    }

    if (move) {
      ball.position.x += ball.vector.x;
      ball.position.y += ball.vector.y;

      aiPad.position = ball.position.y - 2;
      aiPad.position = aiPad.position < 0 ? 0 : aiPad.position;
      aiPad.position = aiPad.position > size - 5 ? size - 5 : aiPad.position;
    }

    direction.up = false;
    direction.down = false;
  }

  keyDown(event) {
    switch (event.key) {
      case "ArrowUp":
        this.direction.up = true;
        break;
      case "ArrowDown":
        this.direction.down = true;
        break;
    }
  }

  update() {
    this.updatePositions();
    this.updatebleElements.forEach(elem => elem.update());
  }

  updateScore(key) {
    this.score[key] += 1;
    document.getElementById("aiScore").textContent = this.score.ai;
    document.getElementById("playerScore").textContent = this.score.player;
  }

  start() {
    this.elem.classList.add("-gameOn");
    setTimeout(() => {
      if (!this.updateInterval) {
        this.updateInterval = setInterval(this.update, 300);
      }  
    }, 500);
    
  }
}

function init() {
  const app = new App("app", 17);
  app.draw();
  document.body.addEventListener("keydown", app.keyDown);
  document.getElementById("start").addEventListener("click", app.start);
}
if (
  document.readyState === "complete" ||
  (document.readyState !== "loading" && !document.documentElement.doScroll)
) {
  init();
} else {
  document.addEventListener("DOMContentLoaded", init);
}

              
            
!
999px

Console