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

              
                <link href="https://fonts.googleapis.com/css?family=Lobster&display=swap" rel="stylesheet">
<section id="app-wrapper">
  
<section id="app-header">
  
  <section class='player-section'>
    <h1> Player</h2>
    <h3 id='player-one-score'> 0 </h3>
  </section>
  
  <section class='main-header'>
    <h1> Game of Pong</h2>
    <h3>by John Davidson </h3>
  </section>
  
  <section class='player-section'>
    <h1> Computer </h2>
    <h1 id='player-two-score'> 0 </h1>
  </section>
  
</section>
  
  <canvas id="gameCanvas" 
          width="700"
          height="600">
  </canvas>
</section>
              
            
!

CSS

              
                html {
  background-color: black;
  color: rgba(255, 255, 255, .7);
  font-family: 'Lobster', cursive;
  background-image: url('data:image/svg+xml,%3Csvg width="16" height="16" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"%3E%3Cpath d="M0 0h16v2h-6v6h6v8H8v-6H2v6H0V0zm4 4h2v2H4V4zm8 8h2v2h-2v-2zm-8 0h2v2H4v-2zm8-8h2v2h-2V4z" fill="%239C92AC" fill-opacity="0.4" fill-rule="evenodd"/%3E%3C/svg%3E');
}
#app-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center
}

#app-header {
  display: flex;
  align-content: space-between;

}

.player-section {
  margin: 0em 5em 0em 5em;
}

h1, h3 {
  text-align: center;
  font-size: = 500px;
  text-shadow: 8px 8px 5px #000;
  font-size: 30px;
}
h1:hover, h3:hover {
  color: white;
  text-align: center;
  cursor: pointer;
}

#gameCanvas {
  height: 600px;
  width: 700px;
}
              
            
!

JS

              
                let canvas;
let canvasContext;
let framesPerSecond = 30;
let ballX = 50;
let ballY = 50;
let ballSpeedX = 6;
let ballSpeedY = 6;
let paddle1Y = 250;
let paddle2Y = 250;
const paddleHeight = 100;
const paddleWidth = 10;
let player1Score = 0;
let player2Score = 0;
let display1score = document.getElementById('player-one-score');
let display2score = document.getElementById('player-two-score');

window.onload = function() {
  canvas = document.getElementById('gameCanvas');
  canvasContext = canvas.getContext('2d');
  setInterval(function() {
    moveEverything();
    drawEverything();
  } , 1000 / framesPerSecond)
  
  canvas.addEventListener('mousemove', function(evt) {
    let mousePos = calculateMousePos(evt);
    paddle1Y = mousePos.y - paddleHeight / 2;
  })
}

function moveEverything (){
  computerMovement();
  ballX = ballX + ballSpeedX;
  ballY = ballY + ballSpeedY;
    if(ballX > canvas.width - 20){
      if(ballY > paddle2Y && ballY < paddle2Y + paddleHeight){
        ballSpeedX = -ballSpeedX;
      } else {
       if (ballX > canvas.width + 10) {
        ballReset()
         player1Score++
             display1score.innerText = player1Score
             display1score.textContent = player1Score
       }
      } 
    }
    if (ballX < 20){
      if(ballY > paddle1Y && ballY < paddle1Y + paddleHeight){
        ballSpeedX = -ballSpeedX;
        
        let deltaY = ballY - (paddle1Y + paddleHeight / 2)
        ballSpeedY = deltaY * 0.5 + 6
      } else {
       if (ballX < -10) {
        ballReset()
         player2Score++
             display2score.innerText = player2Score
             display2score.textContent = player2Score
       }
      }
    }
      if (ballY > canvas.height - 10){
       ballSpeedY = -ballSpeedY;
    }
    if (ballY < 10){
       ballSpeedY = -ballSpeedY;
    }
}

function drawEverything (){
  //Court
  colorRect(0, 0, canvas.width, canvas.height, 'green');
  //Middle Circle 1
  colorCircle((canvas.width / 2), canvas.height / 2, 100, 'lightgrey');
  //Middle Circle 1
  colorCircle((canvas.width / 2), canvas.height / 2, 50, 'green');
  //Center Line
  colorRect((canvas.width - 20) / 2, 0, 20, canvas.height, 'lightgrey')
  //Ball
  colorCircle(ballX, ballY, 10, 'white');
  //Paddle Left 
  colorRect(5, paddle1Y, paddleWidth, paddleHeight, 'brown')
  //Paddle Right
  colorRect(685, paddle2Y, paddleWidth, paddleHeight, 'brown')
}

function colorRect(X, Y, width, height, color) {
  canvasContext.fillStyle = color;
  canvasContext.fillRect(X,Y, width, height);
}

function colorCircle(centerX, centerY, radius, color) {
  canvasContext.fillStyle = color;
  canvasContext.beginPath();
  canvasContext.arc(centerX, centerY, radius, 0, Math.PI*2, true)
  canvasContext.fill();
}

function calculateMousePos(evt) {
  let rect = canvas.getBoundingClientRect();
  let root = document.documentElement;
  let mouseX =evt.clientX - rect.left;
  let mouseY = evt.clientY - rect.top;
  return {
    x:mouseX,
    y:mouseY
  };
}

function ballReset() {
  ballX = canvas.width / 2;
  ballY = canvas.height / 2;
  ballSpeedX = -ballSpeedX;
  ballSpeedY = -ballSpeedY;
}

function computerMovement() {
  let paddle2YCenter = paddle2Y + (paddleHeight / 2)
  if(paddle2YCenter < ballY - 25) {
     paddle2Y += 6
  } else 
  if(paddle2YCenter > ballY + 25) {
    paddle2Y -= 6
  }
}
              
            
!
999px

Console