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.

            
              html, body, #app
  margin: 0
  padding: 0
  height: 100%
  width: 100%
  overflow: hidden
  background: #555
  
canvas
  padding: 0
  margin: auto
  display: block
 
.buttons
  position: fixed
  bottom: 5px
  background: rgba(0,0,0,0)
  color: white
  font-family: "Roboto Mono", sans-serif
  border-radius: 4px
  border: 1px solid #fff
  text-align: center
  opacity: 0.6
  
.buttonLeft
  left: 10px
  width: 30px
#buttonS
  left: 50px
    
.buttonRight
  right: 10px
  padding: 0 5px
  
#buttonUp
  right: 65px
  
button
  right: 0
  left: 0
  width: 100px
  height: 45px
  margin: 0 auto
    
            
          
!
            
              let gameWidth = document.documentElement.clientWidth,
    gameHeight = document.documentElement.clientHeight;
if(gameWidth > 800){
  gameWidth = 800;
}
let player1Score = 0,
    player2Score = 0,
    gameOver = false,
    winner = "",
    monkey = false;

var Game = React.createClass({
  componentDidMount: function(){
    $(".buttonRight").hide();
    $("#butt2").hide();
    this.update();
  },
  update: function(){
    const Width = gameWidth,
          Height = gameHeight,
          ctx = this.refs.canvas.getContext('2d'),
          fps = 60,
          paddleWidth = 100;
    let ballY = Height/2,
        ballX = Width/2,
        ballRadius = 6,
        ballSpeedY = 0,
        ballSpeedX = Height/75;
    let paddle1Y = Height / 2 - (paddleWidth/2),
        paddle2Y = Height / 2 - (paddleWidth/2),
        paddleSpeed = 6;    
    
    function KeyListener() {
      this.pressedKeys = [];
      this.keydown = function(e) { this.pressedKeys[e.keyCode] = true };
      this.keyup = function(e) { this.pressedKeys[e.keyCode] = false };
      document.addEventListener("keydown", this.keydown.bind(this));
      document.addEventListener("keyup", this.keyup.bind(this));
    }
    KeyListener.prototype.isPressed = function(key){
      return this.pressedKeys[key] ? true : false;
    };
    KeyListener.prototype.addKeyPressListener = function(keyCode, callback){
      document.addEventListener("keypress", function(e) {
        if (e.keyCode == keyCode)
          callback(e);
      });
    };   
    var keys = new KeyListener();
    
    var reset = function(){
      ballY = Height/2;
      ballX = Width/2;
      ballSpeedX = -ballSpeedX;
      ballSpeedY = 0;
    }
    // draw everything on screen
    var drawAll = function(){
      // screen
      ctx.fillStyle = "#333";
      ctx.fillRect(0, 0, Width, Height);      
      // middle dashed line
      ctx.strokeStyle = "#fff";
      ctx.setLineDash([10]);
      ctx.beginPath();
      ctx.moveTo(Width / 2,0);
      ctx.lineTo(Width / 2, Height);
      ctx.stroke();
      // score
      ctx.font = "30px Orbitron";
      ctx.fillStyle = "#888";
      ctx.fillText(player1Score,((Width/2)/2),100);
      ctx.fillText(player2Score,((Width/2)*1.5),100);
      // 2 rects
      ctx.fillStyle = "#fff";    
      ctx.fillRect(0, paddle1Y, 10, paddleWidth);    
      ctx.fillRect(Width -10, paddle2Y, 10, paddleWidth);
      // ball
      ctx.beginPath();
      ctx.arc(ballX, ballY, ballRadius, 0, Math.PI*2);
      ctx.fill();
    }
    // move stuff begore drawing again
    var moveAll = function(){
      // ball movement
      ballX += ballSpeedX;
      ballY += ballSpeedY;
      
      // horizontal 
      if(ballX > Width || ballX < 0){
        // right side collision
        if(ballX > Width/2 &&
           (ballY >= paddle2Y && ballY <= paddle2Y + paddleWidth)){
          ballSpeedX = -ballSpeedX;
          let deltaY = ballY - (paddle2Y + paddleWidth/2);
          ballSpeedY = deltaY * 0.35;
        }else if(ballX < Width/2 &&
           (ballY >= paddle1Y && ballY <= paddle1Y + paddleWidth)){
          ballSpeedX = -ballSpeedX;
          let deltaY = ballY - (paddle1Y + paddleWidth/2);
          ballSpeedY = deltaY * 0.35;
        }
        else{
          if(ballX < Width/2){
            player2Score++;
            if(player2Score === 11){
              winner = "PLAYER2"
              gameOver = true;
            }
          }else{
            player1Score++;
            if(player1Score === 11){
              winner = "PLAYER1"
              gameOver = true;
            }
          }
          reset();          
        }
      }// vertical 
      if(ballY > Height || ballY < 0){        
        ballSpeedY = -ballSpeedY;
      }
      // ai paddle movement, limits at canvas boundaries to make it more efficient
      if(!monkey){
        if(ballY > paddle2Y+paddleWidth/3 && (paddle2Y + paddleWidth) < Height){
          paddle2Y += paddleSpeed;
        }else if(ballY < paddle2Y+paddleWidth/3 && paddle2Y > 0){
          paddle2Y -= paddleSpeed;
        }
      }else{
          if (keys.isPressed(40) && (paddle2Y + paddleWidth) < Height) { // DOWN
          paddle2Y += paddleSpeed;
        } else if (keys.isPressed(38) && paddle2Y > 0) { // UP
          paddle2Y -= paddleSpeed;
        }
      }
      // player1 paddle movement thanks to
      // http://blog.mailson.org/2013/02/simple-pong-game-using-html5-and-canvas
      // same limits as ai for efficiency 
      if (keys.isPressed(83) && (paddle1Y + paddleWidth) < Height) { // DOWN
        paddle1Y += paddleSpeed;
      } else if (keys.isPressed(87) && paddle1Y > 0) { // UP
        paddle1Y -= paddleSpeed;
      }
    }
    // draw default if changing game type, else save last draw 
    var GameOver = function(){
      ballSpeedY = 0;
      paddle1Y = Height/2 - paddleWidth/2;
      paddle2Y = Height/2 - paddleWidth/2;
      player1Score = 0;
      player2Score = 0;
      ctx.textAlign = "center";
      if(winner !== ""){
        ctx.fillStyle = "#888";
        ctx.font = "36px Orbitron";
        ctx.fillText(winner + " WON!",Width/2,150);
      }else{
        ballY = Height/2;
        ballX = Width/2;
        drawAll();
        gameOver = true;
      }
      ctx.font = "14px Roboto Mono";
      ctx.fillText("Click anywhere to start a new game.",Width/2,200);
      document.addEventListener("mousedown",function(){
        gameOver = false;
        winner = "";
      });
    }
    // trigger 2 monkeys
    $("#butt1").click(function(){
      ballY = Height/2;
      ballX = Width/2;
      GameOver();
      monkey = true;
      $(this).hide();
      $("#butt2").show();
      $(".buttonRight").show();
    })
    // trigger AI
    $("#butt2").click(function(){
      GameOver();
      monkey = false;
      $(this).hide();
      $("#butt1").show();
      $(".buttonRight").hide();
    })
    // to block automatic start
    GameOver();
    // default 60fps
    setInterval(function(){
      if(gameOver === false){
        moveAll();
        drawAll();
      }else{
        GameOver();
      }
    }, 1000/fps);
  },
  render: function(){
    return(
      <div>
        <canvas ref="canvas" width={gameWidth}
          height={gameHeight} id="gameCanvas" />
        <div className="buttons buttonLeft">W</div>
        <div className="buttons buttonLeft" id="buttonS">S</div>
        <div className="buttons buttonRight" id="buttonUp">Up</div>
        <div className="buttons buttonRight">Down</div>
        <button className="buttons" id="butt1">vs Computer</button>
        <button className="buttons" id="butt2">2 Players</button>
      </div>
      )
  }
});

ReactDOM.render(<Game />,
  document.getElementById('app'));
            
          
!
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