123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="play-area">
  
  <div class="court court-left">
    <div class="wall gone" id="wall-left">
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
    </div>
    <div class="score gone" id="score-left">0</div>
    <div class="paddle gone" id="paddle-left"></div>
  </div>
  <div class="court court-right">
    <div class="score gone" id="score-right">0</div>
    <div class="wall gone" id="wall-right">
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
      <span>🔥</span>
    </div>
    <div class="paddle gone" id="paddle-right"></div>
  </div>
  
  <div class="ball"></div>
</div>
<button role="button" class="play-btn">PLAY</button>

<audio controls="controls" id="audio-hit">
  <source src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1145795/pong_beeep.wav" type="audio/wav">
</audio>
<audio controls="controls" id="audio-miss">
  <source src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1145795/pong_peeeeeep.wav" type="audio/wav">
</audio>
            
          
!
            
              $dark: #333;
$darker: #1c1c1c;
$darkest: #000;
$light: #fff;

html { box-sizing: border-box; } *, *::before, *::after { box-sizing: inherit; }
html {
  height: 100vh;
}
body {
  font-family: 'Press Start 2P', cursive;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: $darkest;
}
audio {
  display: none;
}
.play-area {
  background: $darker;
  position: relative;
  display: grid;
  grid-template-columns: 1fr 1fr;
  width: 700px;
  height: 600px;
}
.court {
  position: relative;
  text-align: center;
  display: grid;
}
.court-left {
  /* the left column is for when the ball is 'out' */
  padding-right: 2px;
  grid-template-columns: 30px 1fr;
  background-image: linear-gradient(to bottom, $light 50%, $darker 50%);
  background-position: right top;
  background-repeat: repeat-y;
  background-size: 2px 20px;
}
.court-right {
  /* the right column is for when the ball is 'out' */
  grid-template-columns: 1fr 30px;
}
.wall {
  background: $dark;
  width: 30px;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: stretch;
  padding: 5px 0;
  fonr-size: 20px;
  overflow: hidden;
  text-align: center;
}
.gone {
  pointer-events: none;
  opacity: 0;
}

.paddle, .ball {
  position: absolute;
}
.paddle {
  width: 30px;
  height: 80px;
  background: $light;
}
#paddle-left {
  left: 0;
}
#paddle-right {
  right: 0;
}

.score {
  background: $darker;
  color: $light;
  font-size: 40px;
  padding-top: 20px;
}

.play-btn {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  font-family: 'Press Start 2P', cursive;
  padding: 20px 40px;
  font-size: 25px;
  background: $darkest;
  color: $light;
  border: 2px solid $light;
  cursor: pointer;
  &:hover {
    background: $light;
    color: $darkest;
  }
}

 //This bit replaces the cursor with a base64 version of a 6x6px white square. I could have replaxced the cursor using a div via JS (in conjunction with cursor: none) but that could have created lag due to mousemove listener, this way seems snappier.
.play-area { 
  cursor: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAYAAAAGCAYAAADgzO9IAAAMKGlDQ1BJQ0MgUHJvZmlsZQAASImVVwdYU8kWnluSkJDQAqFICb0J0qvU0CIISAcbIQkklBgSgogdFRVcCyoiWNFVEUXXAshiw14Wxd4fFlSUdXEVGypvkgC6+r33vnfyzb1/zpw55z/nzsw3A4B6LEcszkU1AMgTFUjiwoOZKalpTNIjgMCfFqACbw5XKg6KjY0CUIbe/5R3N6AtlKsOcl8/9/9X0eTxpVwAkFiIM3hSbh7EBwDAPbhiSQEAhB6oN59aIIaYCFkCbQkkCLGFHGcpsZccZyhxlMImIY4FcToAKlQOR5IFgJqcF7OQmwX9qC2B2EnEE4ogboXYnyvg8CD+DPHIvLwpEKvbQGyT8Z2frH/4zBj2yeFkDWNlLgpRCRFKxbmcaf9nOf635OXKhmKYw0YVSCLi5DnL65YzJVKOqRCfFWVEx0CsBfE1IU9hL8dPBbKIxEH7D1wpC9YMMABAqTxOSCTEhhCbiXKjowb1/pnCMDbEsPZogrCAnaAci/IkU+IG/aNFfGlo/BDmSBSx5DZlspzEoEGfGwR89pDPlmJBQrKSJ3q5UJgUDbEaxPekOfGRgzYvigWs6CEbiSxOzhl+cwxkSsLilDaYRZ50KC/MRyBkRw/iqAJBQoRyLDaJy1Fw04M4my9NiRriyeOHhCrzwkr4osRB/liFuCA4btB+qzg3dtAea+Xnhsv1ZhC3Swvjh8b2FsDJpswXB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8gQExR8JRHAUw2NDMTAD+3g4ALRUA+mV4fhivvJspBFHeJxUI/CesvL8pxAOABviSH8NZxwDYC5vVHOg7EAD5ETwhEKCursNtUKSZri5KX1R4YyF8GBh4YwQAqQWAL5KBgf71AwNftkKytwE4lq+8E8pFfgfd5CZHVxj5ReAH+Tccz3E8Ug6uPgAAAAlwSFlzAAAWJQAAFiUBSVIk8AAAAZlpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IlhNUCBDb3JlIDUuNC4wIj4KICAgPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4KICAgICAgPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIKICAgICAgICAgICAgeG1sbnM6ZXhpZj0iaHR0cDovL25zLmFkb2JlLmNvbS9leGlmLzEuMC8iPgogICAgICAgICA8ZXhpZjpQaXhlbFhEaW1lbnNpb24+NjwvZXhpZjpQaXhlbFhEaW1lbnNpb24+CiAgICAgICAgIDxleGlmOlBpeGVsWURpbWVuc2lvbj42PC9leGlmOlBpeGVsWURpbWVuc2lvbj4KICAgICAgPC9yZGY6RGVzY3JpcHRpb24+CiAgIDwvcmRmOlJERj4KPC94OnhtcG1ldGE+CvQIiZ0AAAAcaURPVAAAAAIAAAAAAAAAAwAAACgAAAADAAAAAwAAAEaJBU4hAAAAEklEQVQYGWL8DwQMWAAjyRIAAAAA///w2858AAAAD0lEQVRj/A8EDFgAI8kSAPMUF+9wQ7huAAAAAElFTkSuQmCC'), auto;
}
            
          
!
            
              let leftsTurn = true;
let paddleSize = 70;
let difficulty = 0.1;
let score = 0;
const walls = document.querySelectorAll('.wall');
const paddles = document.querySelectorAll('.paddle');
const paddleLeft = document.getElementById('paddle-left');
const paddleRight = document.getElementById('paddle-right');
const wallLeft = document.getElementById('wall-left');
const wallRight = document.getElementById('wall-right');
const scoreLeft = document.getElementById('score-left');
const scoreRight = document.getElementById('score-right');
const playBtn = document.querySelector('.play-btn');
const audioHit = document.getElementById('audio-hit');
const audioMiss = document.getElementById('audio-miss');

function reset() {
  leftsTurn = true;
  paddleSize = 70;
  difficulty = 0.1;
  score = 0;
  scoreLeft.classList.remove('gone');
  scoreRight.classList.remove('gone');
  wallLeft.classList.add('gone');
  paddleLeft.classList.add('gone');
  wallRight.classList.remove('gone');
  paddleRight.classList.remove('gone');
  playBtn.style.display = 'none';
}

function gameOver() {

  audioMiss.play();
  wallRight.classList.add('gone');
  paddleRight.classList.add('gone');
  wallLeft.classList.add('gone');
  paddleLeft.classList.add('gone');


  if (score > 10) {
    playBtn.innerHTML = `🎉 ${score}pts 🎉`;
  } else {
    playBtn.innerHTML = '🙈🙈🙈'
  }

  playBtn.style.display = 'block';
  setTimeout(() => {
    playBtn.innerHTML = 'PLAY'
  }, 2000)
}

function switchTurns() {

  score++;

  leftsTurn = !leftsTurn;

  audioHit.play();

  if (leftsTurn) {
    wallLeft.classList.add('gone');
    paddleLeft.classList.add('gone');
    wallRight.classList.remove('gone');
    paddleRight.classList.remove('gone');
  } else {
    wallRight.classList.add('gone');
    paddleRight.classList.add('gone');
    wallLeft.classList.remove('gone');
    paddleLeft.classList.remove('gone');
  }

  if (score > 20) {
    difficulty = 0.4;
  } else if (score > 10) {
    difficulty = 0.25;
    paddleSize = 50;
  } else if (score > 5) {
    difficulty = 0.15;
    paddleSize = 60;
  }

  $('.paddle').css('height', paddleSize);

}

movePaddles();

playBtn.addEventListener('click', () =>{
  reset();
});

Array.from(paddles).forEach(function(paddle) {
  paddle.addEventListener('mouseenter', function(e) {
    switchTurns();
  })
});

Array.from(walls).forEach(function(wall) {
  wall.addEventListener('mouseenter', function(e) {
    if (leftsTurn) {
      scoreRight.innerHTML = parseFloat(scoreRight.innerHTML) + 1
    } else {
      scoreLeft.innerHTML = parseFloat(scoreLeft.innerHTML) + 1
    }
    gameOver();
  })
});

function newPosition(){
  var h = $('.play-area').height() - paddleSize;
  var nh = Math.floor(Math.random() * h);
  return [nh];  
}

function movePaddles(){
  var newPos = newPosition();
  var oldPos = $('.paddle').offset();
  var speed = calcSpeed([oldPos.top], newPos);

  //this is basically the only reason I've included jQuery...
  $('.paddle').animate({ top: newPos[0] }, speed, function(){
    movePaddles();        
  });
};

function calcSpeed(prev, next) {
  var y = Math.abs(prev[0] - next[0]);
  var speed = Math.ceil(y/difficulty);
  return speed;
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console