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

              
                <div id="scoreboard" class="scoreboard">
  <table>
    <thead>
      <th id="playerScore"></th>
      <th>:</th>
      <th id="computerScore"></th>
    </thead>
    <tbody>
      <tr>
        <td>YOU</td>
        <td></td>
        <td>COM</td>
      </tr>
    </tbody>
  </table>
</div>
              
            
!

CSS

              
                body{
  margin: 50px;
}
              
            
!

JS

              
                /**
* Customization
**/

var ballColor = "#808880";
var paddleColor = "#A0AAA0";
var fieldColor = "#E0EEE0";
var maxSpeed = 5;


/**
* Initialisation
**/

var canvas = document.createElement('canvas');
var width = 400;
var height = 600;
canvas.width = width;
canvas.height = height;
var context = canvas.getContext('2d');
var keysDown = {};

window.onload = function() {
  document.body.appendChild(canvas);
  animate(step);
};


/**
* Paddle
**/

class Paddle {
  constructor( x = 175, y = 580, width = 50, height = 10 ){
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.x_speed = 0;
    this.y_speed = 0;
  }
    
  render(){
    context.fillStyle = paddleColor;
    context.fillRect(this.x, this.y, this.width, this.height);
  }
  
  move(x){
    this.x += x;
    this.x_speed += x;
    
    if(this.x < 0){ // If the paddle is all the way to the left
      this.x = 0;
      this.x_speed = 0;
    }
    else if(this.x + this.width > width){ // If the paddle is all the way to the right
      this.x = width - this.width;
      this.x_speed = 0;
    }
  }
}


/**
* Player
**/

class Player{
  constructor(){
    this.paddle = new Paddle(175,580);
  }
  
  update(){
    for(var key in keysDown){
      var value = Number(key);
      
      if(value == 37){ // Left arrow
        this.paddle.move(-4);
      }
      else if (value == 39){ // Right arrow
        this.paddle.move(4);
      }
      else {
        this.paddle.move(0);
      }
    }
  }
  
  render(){
    this.paddle.render();
  }
  
  score(){
    score.playerScore += 1;
    score.update();
  }
}


/**
* Computer
**/

class Computer {
  constructor(){
    this.paddle = new Paddle(175,10);
  }
  
  update(ball){
    var x_pos = ball.x;
    var diff = -((this.paddle.x + this.paddle.width / 2) - x_pos);
    
    if(diff < 0 && diff < -4){ // Max speed left
      diff = -maxSpeed;
    }
    else if(diff > 0 && diff > 4){ // Max speed right
      diff = maxSpeed;
    }
    
    this.paddle.move(diff);
  }
  
  render(){
    this.paddle.render();
  }
  
  score(){
    score.computerScore +=1;
    score.update();
  }
}


/**
* Ball
**/

class Ball {
  constructor(x,y){
    this.x = x;
    this.y = y;
    this.x_speed = 0;
    this.y_speed = 3;
    this.radius = 5;
  }
  
  update(paddleP, paddleC){
    this.x += this.x_speed;
    this.y += this.y_speed;
    
    var top_x = this.x - 5;
    var top_y = this.y - 5;
    var bottom_x = this.x + 5;
    var bottom_y = this.y + 5;
    
    
    // Wall collision
    
    if(this.x -5 < 0){ // If the ball hits the left wall
      this.x = 5;
      this.x_speed *= -1;
    }
    else if(this.x + 5 > width){ // If the ball hits the right wall
      this.x_speed *= -1;
    }
    
    
    // Scoring
    if(this.y < 0){ // Computer scores
      this.y_speed = -3;
      player.score();
    }
    else if(this.y > height){ // Player scores
      this.y_speed = 3;
      computer.score();
    }
    
    if(this.y < 0 || this.y > height){
      this.x_speed = 0;
      this.x = width/2;
      this.y = height/2;
    }
    
    
    
    // Collision with a paddle
    
    if( // if the ball collides with the player's paddle
      top_y < (paddleP.y + paddleP.height) &&
      bottom_y > paddleP.y &&
      top_x < (paddleP.x + paddleP.width) &&
      bottom_x > paddleP.x
    ){
      this.y_speed = -3;
      this.x_speed += (paddleP.x_speed / 2);
      this.y += this.y_speed;
    }
    else if( // if the ball collides with the computer's paddle
      top_y < (paddleC.y + paddleC.height) &&
      bottom_y > paddleC.y &&
      top_x < (paddleC.x + paddleC.width) &&
      bottom_x > paddleC.x
    ){
      this.y_speed = 3;
      this.x_speed += (paddleC.x_speed / 2);
      this.y += this.y_speed;
    }
    
    this.x_speed = this.x_speed > maxSpeed ? maxSpeed : this.x_speed;
    this.y_speed = this.y_speed > maxSpeed ? maxSpeed : this.y_speed;
    
  }
  
  render(){
    context.beginPath();
    context.arc(this.x, this.y, this.radius,  2 * Math.PI, false);
    context.fillStyle = ballColor;
    context.fill();
  }
}


/**
* Creating an instance of Player Computer and the Ball
**/

var player = new Player();
var computer = new Computer();
var ball = new Ball(width/2,height/2);


/**
* Animate Step Update Render
**/

// Animate

var animate = window.requestAnimationFrame ||
  window.webkitRequestAnimationFrame ||
  window.mozRequestAnimationFrame ||
  function(callback) { window.setTimeout(callback, 1000/60) };


// Step

var step = function() {
  update();
  render();
  animate(step);
};


// Update

var update = function() {
  player.update();
  computer.update(ball);
  ball.update(player.paddle, computer.paddle);
};


// Render

var render = function() {
  context.fillStyle = fieldColor;
  context.fillRect(0, 0, width, height);
  
  player.render();
  computer.render();
  ball.render();
};


/**
* Controls
**/

window.addEventListener('keydown', (e) => {
  keysDown[e.keyCode] = true;
});

window.addEventListener('keyup', (e) => {
  delete keysDown[e.keyCode];
});


/**
* Pong
**/

class Pong {
  constructor(){
    this.playerScore = 0;
    this.computerScore = 0;
       
    this.update();
  }
  
  update(){
    if(this.playerScore >= 5){
      this.win();
    }
    else if(this.computerScore >= 5){
      this.lose();
    }
    console.log(this.playerScore +' : '+ this.computerScore);
    document.getElementById('playerScore').textContent = this.playerScore;
    document.getElementById('computerScore').textContent = this.computerScore;
  }
  
  restart(){
    this.playerScore = 0;
    this.computerScore = 0;
  }
  
  win(){
    alert('You won!');
    this.restart();
  }
  
  lose(){
    alert('You lost!');
    this.restart();
  }
}

var score = new Pong();


              
            
!
999px

Console