css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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 id='game'>
  <div id='frame'>
    <div id='paddle'></div>
    <div id='ball'></div>
  </div>
  <div id='controls'>
    <p id='title'>Ping Pong</p>
    <button id='play' class='button'>Play</button>
    <button id='pause' class='button'>Pause</button>
    <button id='reset' class='button'>Reset</button>
    <div id='subControls'>
      <div id='speed'>
        <p class='subTitle'>Ball Speed</p>
        <button id='speedDown' class='arrow'>▼</button>
        <span id='speedDisplay' class='detail'>1</span>
        <button id='speedUp' class='arrow'>▲</button>
      </div>
      <div id='sensitivity'>
        <p class='subTitle'>Keyboard Sensitivity</p>
        <button id='sensitivityDown' class='arrow'>▼</button>
        <span id='sensitivityDisplay' class='detail'>Normal</span>
        <button id='sensitivityUp' class='arrow'>▲</button>
      </div>
      <div>
        <p class='subTitle'>Volley Count</p>
        <p id='hitsDisplay'>0</p>
        <p id='gameOverDisplay'></p>
      </div>
    </div>
  </div>
</div>
<div id='midLine'></div>
            
          
!
            
              :root {
  --paddle-top: 250px;
  --ball-top: 250px;
  --ball-left: 17px;
  --table-bg: #3a873d;
}

#game {
  display: grid;
  grid-template-columns: 520px auto;
}

#frame {
  height: 500px;
  width: 500px;
  border: 5px solid #FFFFFF;
  outline: 1px solid;
  overflow: hidden;
  background-color: var(--table-bg);
}

#paddle {
  height: 80px;
  width: 10px;
  border: 1px solid;
  position: relative;
  left: 5px;
  top: calc(var(--paddle-top) - 45px);
  background-color: #ad6e20;
  z-index: 2;
}

#ball {
  height: 20px;
  width: 20px;
  border: 1px solid;
  border-radius: 50%;
  position: relative;
  top: calc(var(--ball-top) - 80px - 10px - 5px);
  left: var(--ball-left);
  background-color: #ffaa00;
  z-index: 2;
}

#speed {
  margin-top: 10px;
}

#sensitivityDisplay {
  display: inline-block;
  text-align: center;
  width: 50px;
}

#midLine {
  position: absolute;
  left: 13px;
  top: 257px;
  height: 5px;
  width: 500px;
  background-color: #FFFFFF;
  z-index: 1;
}

#title {
  font-family: Arial, sans-serif;
  font-size: 50px;
  font-weight: bolder;
  margin-top: 20px;
  margin-bottom: 15px;
}

#play {
  margin-left: 10px;
}
.button {
  font-family: Arial, sans-serif;
  font-size: 16px;
  font-weight: bold;
  padding: 10px;
  border-radius: 5px;
  margin-right: 5px;
}

.subTitle {
  font-family: Arial, sans-serif;
  font-size: 18px;
  font-weight: bold;
}

.arrow {
  font-family: Arial, sans-serif;
  font-size: 16px;
  padding: 4px;
  border-radius: 5px;
}

.detail {
  font-family: Arial, sans-serif;
  font-size: 14px;
  font-weight: bold;
}

#hitsDisplay {
  font-family: Arial, sans-serif;
  font-size: 35px;
  font-weight: bolder;
  margin: 0;
  padding: 5px;
  border: 1px solid;
  border-radius: 5px;
  width: 75px;
  text-align: center;
}

#gameOverDisplay {
  font-family: Arial, sans-serif;
  font-size: 30px;
  font-weight: bold;
  color: var(--table-bg);
}
            
          
!
            
              // PING PONG 

// VARIABLE DECLARATIONS
var play = document.getElementById('play');
var pause = document.getElementById('pause');
var reset = document.getElementById('reset');
var paddle = document.getElementById('paddle');
var ball = document.getElementById('ball');
var speedUp = document.getElementById('speedUp');
var speedDown = document.getElementById('speedDown');
var speedDisplay = document.getElementById('speedDisplay');
var sensitivityUp = document.getElementById('sensitivityUp');
var sensitivityDown = document.getElementById('sensitivityDown');
var sensitivityDisplay = document.getElementById('sensitivityDisplay');
var hitsDisplay = document.getElementById('hitsDisplay');
var gameOverDisplay = document.getElementById('gameOverDisplay');
var paddlePosition = 250;
var ballLeft = 18;
var ballTop = 250;
var increaseRight = true;
var increaseTop = false;
var increaseBottom = false;
var increaseTopHalf = false;
var increaseBottomHalf = false;
var increaseTopMost = false;
var increaseBottomMost = false;
var impact = 'middle';
var gameSpeed = 1;
var sensitivity = 15;
var hits = 0;
var naturalGame = true;
var volley;

// HELPER FUNCTIONS
// main animation control
function animateBall() {
  if(ballLeft >= 480) {
    increaseRight = false;
  }
  if(ballLeft <= 16) {
    var paddleCoords = paddle.getBoundingClientRect();
    var paddlePosition = paddleCoords.y + (paddleCoords.height / 2);
    var ballCoords = ball.getBoundingClientRect();
    var ballPosition = ballCoords.y + (ballCoords.height / 2);
    var difference = paddlePosition - ballPosition;
    if(difference < 3 && difference > -3) {
      ballAngle('middle');
    }
    else if(difference >= 3 && difference < 15) {
      ballAngle('top-half');
    }
    else if(difference <= -3 && difference > -15) {
      ballAngle('bottom-half');
    }
    else if(difference >= 15 && difference < 40) {
      ballAngle('top-edge');
    }
    else if(difference <= -15 && difference > -40) {
      ballAngle('bottom-edge');
    }
    else if(difference >= 40 && difference < 60) {
      ballAngle('top-most');
    }
    else if(difference <= -40 && difference > -60) {
      ballAngle('bottom-most');
    }
    else {
      impact = 'none';
      gameOver();
    }
    if(impact !== 'none') {
      increaseRight = true;
      hits++;
      hits % 10 === 0 ? levelUp() : null;
      hits === 101 && naturalGame ? winner() : null;
      renderHits();
    }
  }
  if(ballTop <= 10) {
    topWall();
  }
  if(ballTop >= 490) {
    bottomWall();
  }
  if(increaseRight) {
    ballLeft += gameSpeed;
  }
  else if(!increaseRight) {
    ballLeft -= gameSpeed;
  }
  if(increaseTop) {
    ballTop--;
  }
  if(increaseBottom) {
    ballTop++;
  }
  if(increaseTopHalf) {
    ballTop -= .5;
  }
  if(increaseBottomHalf) {
    ballTop += .5;
  }
  if(increaseTopMost) {
    ballTop -= 2;
  }
  if(increaseBottomMost) {
    ballTop += 2;
  }
  document.documentElement.style.setProperty('--ball-left', `${ballLeft}px`);
  document.documentElement.style.setProperty('--ball-top', `${ballTop}px`);
}

// controls angle of the ball
function ballAngle(angle) {
  if(angle === 'middle') {
    increaseTop = false;
    increaseTopHalf = false;
    increaseTopMost = false;
    increaseBottom = false;
    increaseBottomHalf = false;
    increaseBottomMost = false;
  }
  else if(angle === 'top-half') {
    increaseTop = false;
    increaseTopHalf = true;
    increaseTopMost = false;
    increaseBottom = false;
    increaseBottomHalf = false;
    increaseBottomMost = false;
  }
  else if(angle === 'bottom-half') {
    increaseTop = false;
    increaseTopHalf = false;
    increaseTopMost = false;
    increaseBottom = false;
    increaseBottomHalf = true;
    increaseBottomMost = false;
  }
  else if(angle === 'top-edge') {
    increaseTop = true;
    increaseTopHalf = false;
    increaseTopMost = false;
    increaseBottom = false;
    increaseBottomHalf = false;
    increaseBottomMost = false;
  }
  else if(angle === 'bottom-edge') {
    increaseTop = false;
    increaseTopHalf = false;
    increaseTopMost = false;
    increaseBottom = true;
    increaseBottomHalf = false;
    increaseBottomMost = false;
  }
  else if(angle === 'top-most') {
    increaseTop = false;
    increaseTopHalf = false;
    increaseTopMost = true;
    increaseBottom = false;
    increaseBottomHalf = false;
    increaseBottomMost = false;
  }
  else if(angle === 'bottom-most') {
    increaseTop = false;
    increaseTopHalf = false;
    increaseTopMost = false;
    increaseBottom = false;
    increaseBottomHalf = false;
    increaseBottomMost = true;
  }
}

// ball hits tops wall
function topWall() {
  if(increaseTop) {
    increaseTop = false;
    increaseBottom = true;
  }
  else if(increaseTopHalf) {
    increaseTopHalf = false;
    increaseBottomHalf = true;
  }
  else if(increaseTopMost) {
    increaseTopMost = false;
    increaseBottomMost = true;
  }
}

// ball hits bottom wall
function bottomWall() {
  if(increaseBottom) {
    increaseBottom = false;
    increaseTop = true;
  }
  else if(increaseBottomHalf) {
    increaseBottomHalf = false;
    increaseTopHalf = true;
  }
  else if(increaseBottomMost) {
    increaseBottomMost = false;
    increaseTopMost = true;
  }  
}

// renders game speed html
function renderSpeed(speed) {
  switch(speed) {
    case .5: return '0';
    case 1: return '1';
    case 1.5: return '2';
    case 2: return '3';
    case 2.5: return '4';
    case 3: return '5';
    case 3.5: return '6';
    case 4: return '7';
    case 4.5: return '8';
    case 5: return '9';
    case 5.5: return '10';
  }
}

// renders paddle sensitivity
function renderSensitivity() {
  switch(sensitivity) {
    case 5: return 'Lowest';
    case 10: return 'Low';
    case 15: return 'Normal';
    case 20: return 'High';
    case 25: return 'Highest';
  }
}

// renders paddle hits html
function renderHits() {
  hitsDisplay.innerHTML = hits.toString();
}

// level up
function levelUp() {
  if(gameSpeed < 5.5) {
    gameSpeed += .5;
  }
  speedDisplay.innerHTML = renderSpeed(gameSpeed);
}

// game over 
function gameOver() {
  gameOverDisplay.innerHTML = 'Game Over';
  document.documentElement.style.setProperty('--table-bg', '#e82525');
}

// winning game over
function winner() {
  gameOverDisplay.innerHTML = 'WINNER';
  clearInterval(volley);
  disableMouse();
  document.documentElement.style.setProperty('--table-bg', '#a449d8');
}

// paddle with mouse positioning
function mousePaddle(event) {
  if(event.clientY > 45 && event.clientY < 450) {
    paddlePosition = event.clientY;
    document.documentElement.style.setProperty('--paddle-top', `${paddlePosition}px`);
  } 
}

// enable mouse control for paddle 
function enableMouse() {
  frame.addEventListener('mousemove', mousePaddle);
}

// disable mouse control for paddle
function disableMouse() {
  frame.removeEventListener('mousemove', mousePaddle);
}

// EVENT LISTENERS
//paddle position
window.addEventListener('keydown', (event) => {
  if(event.keyCode === 38) {
    paddlePosition > 45 ? paddlePosition -= sensitivity : null;
  }
  else if(event.keyCode === 40) {
    paddlePosition < 450 ? paddlePosition += sensitivity : null;
  }
  document.documentElement.style.setProperty('--paddle-top', `${paddlePosition}px`);
});

// play button
play.addEventListener('click', (event) => {
  volley = setInterval(animateBall, 5);
  enableMouse();
});

// pause button
pause.addEventListener('click', (event) => {
  clearInterval(volley);
  disableMouse();
});

// reset button
reset.addEventListener('click', (event) => {
  clearInterval(volley);
  paddlePosition = 250;
  ballLeft = 18;
  ballTop = 250;
  increaseRight = true;
  increaseTop = false;
  increaseBottom = false;
  increaseTopHalf = false;
  increaseBottomHalf = false;
  impact = 'middle';
  gameSpeed = 1;
  hits = 0;
  document.documentElement.style.setProperty('--ball-left', `${ballLeft}px`);
  document.documentElement.style.setProperty('--ball-top', `${ballTop}px`);
  document.documentElement.style.setProperty('--paddle-top', `${paddlePosition}px`);
  document.documentElement.style.setProperty('--table-bg', '#3a873d');
  speedDisplay.innerHTML = renderSpeed(gameSpeed);
  renderHits();
  disableMouse();
  gameOverDisplay.innerHTML = '';
});

// speed up ball
speedUp.addEventListener('click', (event) => {
  if(gameSpeed < 5.5) {
    gameSpeed += .5;
  }
  naturalGame = false;
  speedDisplay.innerHTML = renderSpeed(gameSpeed);
});

// slow down ball
speedDown.addEventListener('click', (event) => {
  if(gameSpeed > .5) {
    gameSpeed -= .5;
  }
  speedDisplay.innerHTML = renderSpeed(gameSpeed);
});

// sensitivity up
sensitivityUp.addEventListener('click', (event) => {
  if(sensitivity < 25) {
    sensitivity += 5;
    sensitivityDisplay.innerHTML = renderSensitivity();
  }
});

// sensitivity down
sensitivityDown.addEventListener('click', (event) => {
  if(sensitivity > 5) {
    sensitivity -= 5;
    sensitivityDisplay.innerHTML = renderSensitivity();
  }
});


            
          
!
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.
Loading ..................

Console