JavaScript Pong Game Implementation
JavaScript Pong Game Implementation
Full-screen mode is toggled by checking if there is a full-screen element. If not, the full-screen request is made using 'canvas.requestFullscreen' or its browser-specific variants like 'mozRequestFullScreen', 'webkitRequestFullscreen', or 'msRequestFullscreen'. If already in full-screen mode, it exits using 'document.exitFullscreen' or similar vendor-specific calls depending on the browser, such as 'document.mozCancelFullScreen', 'document.webkitExitFullscreen', or 'document.msExitFullscreen' .
The implementation checks for collisions with the top and bottom walls using the condition 'if (ballY <= 0 || ballY >= canvas.height)'. When this condition is met, it reverses the vertical velocity of the ball by multiplying 'ballVelocityY' by -1, thus ensuring it bounces off the walls .
When the ball goes out of bounds on the left, 'scorePlayer2' is incremented, and similarly, 'scorePlayer1' is incremented when it goes out on the right. The 'resetBall' function is then called, which places the ball back at the center, reverses its horizontal velocity, and can randomize the direction. This mechanism ensures a continuous gameplay cycle with scoring .
Scores are updated by directly incrementing the respective player's score and invoking 'updateScore()' which modifies the DOM elements to reflect updated scores. This is effective as it provides immediate feedback to players on scoring changes, ensuring scores stay synchronized with gameplay events .
The game differentiates control keys for the players by assigning specific keys to each player: 'W' and 'S' for Player 1, and 'ArrowUp' and 'ArrowDown' for Player 2. The game uses an object 'keys' to track the state of these keys based on 'keydown' and 'keyup' events, allowing independent control of each paddle .
The game handles key states for toggling full-screen mode using conditional checks within the 'keydown' event listener. It prevents default browser behavior for 'F11' using 'e.preventDefault()' and includes a custom function 'toggleFullScreen()' to enter or exit full-screen mode depending on the current state. It also addresses exiting full-screen with the 'Escape' key by checking 'document.fullscreenElement' status, ensuring consistent user interactions across different scenarios .
The paddle movement is bounded by checking the 'Y' position within the 'movePaddles' function. For Player 1, the movement is restricted with 'if (player1Y > 0)' and 'if (player1Y + PADDLE_HEIGHT < canvas.height)', while for Player 2, 'if (player2Y > 0)' and 'if (player2Y + PADDLE_HEIGHT < canvas.height)' conditions ensure paddles stay within the canvas .
Ball collisions with paddles are determined by checking positions relative to paddle boundaries. If the ball's X position is within a paddle's range and the Y position matches the paddle's height constraints, the 'ballVelocityX' is inverted. Specifically, checks like 'ballX <= 50 + PADDLE_WIDTH' for Player 1 and 'ballX >= canvas.width - 50 - PADDLE_WIDTH - BALL_RADIUS * 2' for Player 2 ensure proper collision detection .
The continuous game loop is maintained by using 'requestAnimationFrame(gameLoop)', which recursively calls the 'gameLoop' function. This function integrates key operations including 'movePaddles()', 'moveBall()', 'draw()', and 'updateScore()', ensuring that all game components are updated and redrawn at each frame .
The paddle positions are updated through event listeners that track key presses and releases. The 'keydown' event modifies a 'keys' object to keep track of pressed keys, while the 'keyup' event sets the respective keys back to false. In the 'movePaddles' function, the positions are updated by checking if the associated keys ('W' and 'S' for Player 1, 'ArrowUp' and 'ArrowDown' for Player 2) are true, adjusting the paddles' Y positions by 'PADDLE_SPEED' within the boundaries of the canvas .