Skip to content

Commit 341a076

Browse files
authored
created a ping pong game using html
fixes #1011 created a ping pong game using html
1 parent 50c652b commit 341a076

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

pingpong.html

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<style>
5+
canvas {
6+
background: black;
7+
display: block;
8+
margin: 0 auto;
9+
}
10+
</style>
11+
</head>
12+
<body>
13+
<canvas id="pong" width="800" height="400"></canvas>
14+
<script>
15+
const canvas = document.getElementById("pong");
16+
const ctx = canvas.getContext("2d");
17+
18+
// Paddle variables
19+
const paddleWidth = 10;
20+
const paddleHeight = 100;
21+
let playerY = canvas.height / 2 - paddleHeight / 2;
22+
let computerY = canvas.height / 2 - paddleHeight / 2;
23+
24+
// Ball variables
25+
const ballSize = 10;
26+
let ballX = canvas.width / 2;
27+
let ballY = canvas.height / 2;
28+
let ballSpeedX = 7;
29+
let ballSpeedY = 7;
30+
31+
// Draw a rectangle
32+
function drawRect(x, y, width, height, color) {
33+
ctx.fillStyle = color;
34+
ctx.fillRect(x, y, width, height);
35+
}
36+
37+
// Draw the ball
38+
function drawBall(x, y, size, color) {
39+
ctx.fillStyle = color;
40+
ctx.beginPath();
41+
ctx.arc(x, y, size, 0, Math.PI * 2);
42+
ctx.closePath();
43+
ctx.fill();
44+
}
45+
46+
// Draw the game
47+
function draw() {
48+
// Clear the canvas
49+
drawRect(0, 0, canvas.width, canvas.height, "black");
50+
51+
// Draw the paddles
52+
drawRect(0, playerY, paddleWidth, paddleHeight, "white");
53+
drawRect(canvas.width - paddleWidth, computerY, paddleWidth, paddleHeight, "white");
54+
55+
// Draw the ball
56+
drawBall(ballX, ballY, ballSize, "white");
57+
}
58+
59+
// Move the computer paddle
60+
function moveComputerPaddle() {
61+
const computerYCenter = computerY + paddleHeight / 2;
62+
if (computerYCenter < ballY - 35) {
63+
computerY += 7;
64+
} else if (computerYCenter > ballY + 35) {
65+
computerY -= 7;
66+
}
67+
}
68+
69+
// Update the game state
70+
function update() {
71+
moveComputerPaddle();
72+
73+
// Update ball position
74+
ballX += ballSpeedX;
75+
ballY += ballSpeedY;
76+
77+
// Ball collision with top and bottom walls
78+
if (ballY - ballSize < 0 || ballY + ballSize > canvas.height) {
79+
ballSpeedY = -ballSpeedY;
80+
}
81+
82+
// Ball out of bounds
83+
if (ballX - ballSize < 0) {
84+
// Player misses the ball
85+
ballX = canvas.width / 2;
86+
ballY = canvas.height / 2;
87+
ballSpeedX = 7;
88+
ballSpeedY = 7;
89+
} else if (ballX + ballSize > canvas.width) {
90+
// Computer misses the ball
91+
ballX = canvas.width / 2;
92+
ballY = canvas.height / 2;
93+
ballSpeedX = -7;
94+
ballSpeedY = -7;
95+
}
96+
97+
// Ball collision with paddles
98+
if (
99+
ballX - ballSize < paddleWidth &&
100+
ballY > playerY &&
101+
ballY < playerY + paddleHeight
102+
) {
103+
ballSpeedX = -ballSpeedX;
104+
} else if (
105+
ballX + ballSize > canvas.width - paddleWidth &&
106+
ballY > computerY &&
107+
ballY < computerY + paddleHeight
108+
) {
109+
ballSpeedX = -ballSpeedX;
110+
}
111+
}
112+
113+
// Game loop
114+
function gameLoop() {
115+
draw();
116+
update();
117+
requestAnimationFrame(gameLoop);
118+
}
119+
120+
// Move player paddle with mouse
121+
canvas.addEventListener("mousemove", (event) => {
122+
playerY = event.clientY - canvas.getBoundingClientRect().top - paddleHeight / 2;
123+
});
124+
125+
// Start the game loop
126+
gameLoop();
127+
</script>
128+
</body>
129+
</html>

0 commit comments

Comments
 (0)