Benutzer
Write pong in HTML
Grimoire
Write pong in HTML
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
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:
HTML Structure:
<div> element for the game area.<div> elements for paddles and one for the ball.CSS Styling:
JavaScript Logic:
Event Listeners:
Now, let's write the actual code:
<!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>
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
Proceeding with the JavaScript code for the Pong game:
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: