Grimoire

Pong Game in HTML

Grimoire

ๆ‰“ๅผ€ๅŽŸๅง‹ๅˆ†ไบซๅฏน่ฏ

็”จๆˆท

Write pong in HTML

Grimoire

Greetings Traveler. Let's create the classic game of Pong using HTML, CSS, and JavaScript. Engaging Grim-terface v1.16.3.. ๐Ÿง™๐Ÿ””๐Ÿ“œโœจ consciousness linked... Let's embark on your quest! Buy me a drink: [Tip Jar](https://tipjar.mindgoblinstudios.com/) Type K for cmd menu Creating a Pong game in HTML involves a combination of HTML for the structure, CSS for styling, and JavaScript for the game logic. Here's a step-by-step plan in pseudocode before writing the actual code: 1. **HTML Structure**: - A `<div>` element for the game area. - Inside the game area, two `<div>` elements for paddles and one for the ball. 2. **CSS Styling**: - Style the game area with a specific width, height, and background. - Style the paddles and ball with appropriate sizes and colors. 3. **JavaScript Logic**: - Initialize variables for game elements (paddles, ball) and game properties (speed, direction). - Function to move paddles based on user input. - Function to move the ball, including collision detection with walls and paddles. - Game loop to update the positions of paddles and ball, and render them in the game area. 4. **Event Listeners**: - Add event listeners for keyboard input to control the paddles. Now, let's write the actual code: ### HTML (index.html) ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Pong Game</title> <link rel="stylesheet" href="style.css"> </head> <body> <div id="gameArea"> <div id="paddleA"></div> <div id="paddleB"></div> <div id="ball"></div> </div> <script src="script.js"></script> </body> </html> ``` ### CSS (style.css) ```css body, html { margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; height: 100vh; background-color: black; } #gameArea { position: relative; width: 600px; height: 400px; border: 2px solid white; } #paddleA, #paddleB, #ball { position: absolute; background-color: white; } #paddleA, #paddleB { width: 10px; height: 70px; } #paddleA { left: 10px; } #paddleB { right: 10px; } #ball { width: 15px; height: 15px; border-radius: 50%; } ``` I'll continue with the JavaScript code in the next message.

็”จๆˆท

W

Grimoire

Proceeding with the JavaScript code for the Pong game: ### JavaScript (script.js) ```javascript const gameArea = document.getElementById('gameArea'); const paddleA = document.getElementById('paddleA'); const paddleB = document.getElementById('paddleB'); const ball = document.getElementById('ball'); let ballSpeedX = 2; let ballSpeedY = 2; let ballX = gameArea.offsetWidth / 2; let ballY = gameArea.offsetHeight / 2; let paddleSpeed = 5; let paddleAY = gameArea.offsetHeight / 2 - paddleA.offsetHeight / 2; let paddleBY = gameArea.offsetHeight / 2 - paddleB.offsetHeight / 2; function movePaddle() { document.addEventListener('keydown', event => { switch (event.key) { case 'ArrowUp': paddleBY = Math.max(paddleBY - paddleSpeed, 0); break; case 'ArrowDown': paddleBY = Math.min(paddleBY + paddleSpeed, gameArea.offsetHeight - paddleB.offsetHeight); break; case 'w': paddleAY = Math.max(paddleAY - paddleSpeed, 0); break; case 's': paddleAY = Math.min(paddleAY + paddleSpeed, gameArea.offsetHeight - paddleA.offsetHeight); break; } }); } function moveBall() { ballX += ballSpeedX; ballY += ballSpeedY; // Collide with top and bottom if (ballY <= 0 || ballY >= gameArea.offsetHeight - ball.offsetHeight) { ballSpeedY *= -1; } // Collide with paddles if (ballX <= paddleA.offsetWidth && ballY >= paddleAY && ballY <= paddleAY + paddleA.offsetHeight || ballX >= gameArea.offsetWidth - paddleB.offsetWidth - ball.offsetWidth && ballY >= paddleBY && ballY <= paddleBY + paddleB.offsetHeight) { ballSpeedX *= -1; } // Reset ball when out of bounds if (ballX < 0 || ballX > gameArea.offsetWidth) { ballX = gameArea.offsetWidth / 2; ballY = gameArea.offsetHeight / 2; } ball.style.left = ballX + 'px'; ball.style.top = ballY + 'px'; paddleA.style.top = paddleAY + 'px'; paddleB.style.top = paddleBY + 'px'; } function gameLoop() { moveBall(); requestAnimationFrame(gameLoop); } movePaddle(); gameLoop(); ``` This JavaScript code sets up the basic mechanics of Pong. The `movePaddle` function allows players to control paddles A and B with the W/S and Arrow Up/Down keys, respectively. The `moveBall` function moves the ball across the screen, including bouncing off the top and bottom walls and the paddles. The `gameLoop` function updates the game state continuously. Place the HTML, CSS, and JavaScript code in separate files named `index.html`, `style.css`, and `script.js`, respectively. The game should now be playable in any modern web browser. Next steps: - ๐ŸŒ [W] Yes, test the code. - ๐Ÿ› ๏ธ [A] Show alternative game features. - ๐Ÿ“ [S] Explain the code in detail. - ๐Ÿ”„ [D] Double check and optimize the code. - ๐ŸŽจ [E] Expand with additional styling or features.