HTML Ping Pong Game Tutorial: Build Your Own Classic Game
HTML Ping Pong Game Tutorial: Build Your Own Classic Game
This tutorial will guide you through creating a basic yet engaging Ping Pong game using HTML, JavaScript, and the HTML5 canvas element. You'll learn how to set up the game structure, draw the elements, implement movement and collisions, and add controls for an interactive experience.
Step 1: Setting up the HTML Structure
First, we need to create the HTML structure for our game. We'll use a <canvas> element to draw the game elements and add some basic styles.
<!DOCTYPE html>
<html>
<head>
<title>Ping Pong Game</title>
<style>
canvas {
border: 1px solid black;
display: block;
margin: 0 auto;
}
</style>
</head>
<body>
<canvas id='canvas' width='600' height='400'></canvas>
<script src='game.js'></script>
</body>
</html>
Step 2: Drawing the Game Elements with JavaScript
Now, let's create a JavaScript file (game.js) to handle the game logic and draw the elements on the canvas.
-
Get the Canvas and Context:
const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); -
Define Game Elements:
let ballX = canvas.width/2; let ballY = canvas.height/2; let ballRadius = 10; let ballSpeedX = 5; let ballSpeedY = -5; let paddleHeight = 75; let paddleWidth = 10; let leftPaddleY = canvas.height/2 - paddleHeight/2; let rightPaddleY = canvas.height/2 - paddleHeight/2; let paddleSpeed = 10; -
Create a
draw()Function:function draw() { // Clear the canvas ctx.clearRect(0, 0, canvas.width, canvas.height); // Draw the paddles ctx.fillStyle = 'white'; ctx.fillRect(0, leftPaddleY, paddleWidth, paddleHeight); ctx.fillRect(canvas.width - paddleWidth, rightPaddleY, paddleWidth, paddleHeight); // Draw the ball ctx.beginPath(); ctx.arc(ballX, ballY, ballRadius, 0, Math.PI*2); ctx.fillStyle = 'white'; ctx.fill(); ctx.closePath(); } -
Call the
draw()Function:draw();
Step 3: Implementing Movement for Game Elements
Let's add movement to our game elements.
-
Create a
move()Function:function move() { // Move the ball ballX += ballSpeedX; ballY += ballSpeedY; // Move the left paddle // ... // Move the right paddle // ... } -
Call
move()in thedraw()Function:function draw() { // ... (clear canvas and draw elements) // Move the game elements move(); // ... (draw elements again) }
Step 4: Detecting Collisions
We'll add code to detect when the ball hits the paddles or the walls.
-
Create a
detectCollisions()Function:function detectCollisions() { // Detect collisions with the left paddle if (ballX - ballRadius < paddleWidth && ballY > leftPaddleY && ballY < leftPaddleY + paddleHeight) { ballSpeedX = -ballSpeedX; } // Detect collisions with the right paddle if (ballX + ballRadius > canvas.width - paddleWidth && ballY > rightPaddleY && ballY < rightPaddleY + paddleHeight) { ballSpeedX = -ballSpeedX; } // Detect collisions with the top and bottom walls if (ballY - ballRadius < 0 || ballY + ballRadius > canvas.height) { ballSpeedY = -ballSpeedY; } } -
Call
detectCollisions()in themove()Function:function move() { // ... (move game elements) // Detect collisions detectCollisions(); }
Step 5: Adding Game Controls
Let's add controls to move the paddles.
-
Declare Variables for Key Press Tracking:
let upPressed = false; let downPressed = false; let wPressed = false; let sPressed = false; -
Add Event Listeners for Key Press and Release:
document.addEventListener('keydown', function(event) { if (event.keyCode == 38) { // Up arrow key upPressed = true; } else if (event.keyCode == 40) { // Down arrow key downPressed = true; } else if (event.keyCode == 87) { // W key wPressed = true; } else if (event.keyCode == 83) { // S key sPressed = true; } }); document.addEventListener('keyup', function(event) { if (event.keyCode == 38) { upPressed = false; } else if (event.keyCode == 40) { downPressed = false; } else if (event.keyCode == 87) { wPressed = false; } else if (event.keyCode == 83) { sPressed = false; } }); -
Implement Paddle Movement in the
move()Function:function move() { // ... (move the ball and detect collisions) // Move the left paddle if (upPressed && leftPaddleY > 0) { leftPaddleY -= paddleSpeed; } else if (downPressed && leftPaddleY < canvas.height - paddleHeight) { leftPaddleY += paddleSpeed; } // Move the right paddle if (wPressed && rightPaddleY > 0) { rightPaddleY -= paddleSpeed; } else if (sPressed && rightPaddleY < canvas.height - paddleHeight) { rightPaddleY += paddleSpeed; } }
Step 6: Start the Game
Finally, let's start the game loop to keep everything running.
setInterval(draw, 10); // Call `draw()` every 10 milliseconds
Conclusion
Congratulations! You've built a basic but functional Ping Pong game. From here, you can add more features, customize the game appearance, and even implement scoring to make it more challenging and enjoyable. Remember to save your code in a game.js file and link it to your HTML structure. Have fun experimenting and creating your own variations of the game!
原文地址: https://www.cveoy.top/t/topic/lm9p 著作权归作者所有。请勿转载和采集!