src/scripts/modules/gameutils.js
// update is called on every animation step
update is called on every animation step
update is called on every animation step
function update(dt) {
  ball.dmove(vx*dt, vy*dt)
  var cx = ball.cx(), cy = ball.cy()
   and paddle
  var paddleLeftCy = paddleLeft.cy()
  var dy = Math.min(difficulty, Math.abs(cy - paddleLeftCy))
  paddleLeftCy += cy > paddleLeftCy ? dy : -dy
  paddleLeft.cy(Math.max(paddleHeight/2, Math.min(height-paddleHeight/2, paddleLeftCy)))
  if ((vy < 0 && cy <= 0) || (vy > 0 && cy >= height)) { vy = -vy }
  var paddleLeftY = paddleLeft.y() , paddleRightY = paddleRight.y()
  if((vx < 0 && cx <= paddleWidth && cy > paddleLeftY && cy < paddleLeftY + paddleHeight) ||
     (vx > 0 && cx >= width - paddleWidth && cy > paddleRightY && cy < paddleRightY + paddleHeight)) {
    vy = (cy - ((vx < 0 ? paddleLeftY : paddleRightY) + paddleHeight/2)) * 7 
    vx = -vx * 1.05
  } else
  if ((vx < 0 && cx <= 0) || (vx > 0 && cx >= width)) {
    if(vx < 0) { ++playerRight }
    else { ++playerLeft }
    reset()
    scoreLeft.text(playerLeft+'')
    scoreRight.text(playerRight+'')
  }  
  var playerPaddleY = paddleRight.y()
  if (playerPaddleY <= 0 && paddleDirection == -1) {
    paddleRight.cy(paddleHeight/2)
  } else if (playerPaddleY >= height-paddleHeight && paddleDirection == 1) {
    paddleRight.y(height-paddleHeight)
  } else {
    paddleRight.dy(paddleDirection*paddleSpeed)
  }  
  ball.fill(ballColor);
}
function update ( dt ) { ball . dmove ( vx * dt , vy * dt ) var cx = ball . cx ( ) , cy = ball . cy ( ) var paddleLeftCy = paddleLeft . cy ( ) var dy = Math . min ( difficulty , Math . abs ( cy - paddleLeftCy ) ) paddleLeftCy += cy > paddleLeftCy ? dy : - dy paddleLeft . cy ( Math . max ( paddleHeight / 2 , Math . min ( height - paddleHeight / 2 , paddleLeftCy ) ) ) if ( ( vy < 0 && cy <= 0 ) || ( vy > 0 && cy >= height ) ) { vy = - vy } var paddleLeftY = paddleLeft . y ( ) , paddleRightY = paddleRight . y ( ) if ( ( vx < 0 && cx <= paddleWidth && cy > paddleLeftY && cy < paddleLeftY + paddleHeight ) || ( vx > 0 && cx >= width - paddleWidth && cy > paddleRightY && cy < paddleRightY + paddleHeight ) ) { vy = ( cy - ( ( vx < 0 ? paddleLeftY : paddleRightY ) + paddleHeight / 2 ) ) * 7 vx = - vx * 1.05 } else if ( ( vx < 0 && cx <= 0 ) || ( vx > 0 && cx >= width ) ) { if ( vx < 0 ) { ++ playerRight } else { ++ playerLeft } reset ( ) scoreLeft . text ( playerLeft + '' ) scoreRight . text ( playerRight + '' ) } var playerPaddleY = paddleRight . y ( ) if ( playerPaddleY <= 0 && paddleDirection == - 1 ) { paddleRight . cy ( paddleHeight / 2 ) } else if ( playerPaddleY >= height - paddleHeight && paddleDirection == 1 ) { paddleRight . y ( height - paddleHeight ) } else { paddleRight . dy ( paddleDirection * paddleSpeed ) } ball . fill ( ballColor ) ; }
update is called on every animation step
update is called on every animation step
// move the ball by its velocity // get position of ball // get position of ball and paddle // move the left paddle in the direction of the ball // constraint the move to the canvas area // check if we hit top/bottom borders // check if we hit the paddle // depending on where the ball hit we adjust y velocity // for more realistic control we would need a bit more math here // just keep it simple // magic factor // make the ball faster on hit // check if we hit left/right borders // when x-velocity is negative, it's a point for player 2, otherwise player 1 // move player paddle // update ball color based on position //??.at(1/width*ball.x()))