-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong_game.html
209 lines (179 loc) · 6.58 KB
/
pong_game.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<!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>
<style>
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: black;
color: white;
}
canvas {
border: 1px solid white;
}
#controls {
position: absolute;
top: 10px;
left: 50%;
transform: translateX(-50%);
text-align: center;
color: white;
}
#speedBarContainer {
position: absolute;
top: 50px;
left: 50%;
transform: translateX(-50%);
width: 300px;
height: 20px;
background-color: gray;
border: 1px solid white;
}
#speedBar {
height: 100%;
background-color: green;
}
</style>
</head>
<body>
<div id="controls">
<button id="toggleDifficulty">Toggle Difficulty: Dynamic</button>
</div>
<div id="speedBarContainer">
<div id="speedBar" style="width: 0%;"></div>
</div>
<canvas id="pongCanvas" width="800" height="600"></canvas>
<script>
const canvas = document.getElementById("pongCanvas");
const ctx = canvas.getContext("2d");
const toggleDifficultyButton = document.getElementById("toggleDifficulty");
const speedBar = document.getElementById("speedBar");
let dynamicDifficulty = true;
// Paddle constants
const paddleWidth = 10;
const paddleHeight = 100;
const playerPaddleSpeed = 5;
const baseOpponentPaddleSpeed = 4; // Base speed for opponent
// Ball constants
const ballSize = 20;
let ballSpeedX = 5;
let ballSpeedY = 5;
// Paddle positions
let playerPaddleY = canvas.height / 2 - paddleHeight / 2;
let opponentPaddleY = canvas.height / 2 - paddleHeight / 2;
// Ball position
let ballX = canvas.width / 2;
let ballY = canvas.height / 2;
// Scores
let playerScore = 0;
let opponentScore = 0;
// Opponent AI precision offset
let aiPrecisionOffset = 0;
// Function to draw the paddles
function drawPaddles() {
ctx.fillStyle = "white";
ctx.fillRect(0, playerPaddleY, paddleWidth, paddleHeight);
ctx.fillRect(canvas.width - paddleWidth, opponentPaddleY, paddleWidth, paddleHeight);
}
// Function to draw the ball
function drawBall() {
ctx.beginPath();
ctx.arc(ballX, ballY, ballSize / 2, 0, Math.PI * 2);
ctx.fillStyle = "white";
ctx.fill();
ctx.closePath();
}
// Function to draw the scores
function drawScores() {
ctx.font = "32px Arial";
ctx.fillText(playerScore, canvas.width / 4, 50);
ctx.fillText(opponentScore, 3 * canvas.width / 4, 50);
}
// Function to update the game state
function update() {
// Determine AI speed
let opponentPaddleSpeed;
if (dynamicDifficulty) {
const scoreDifference = playerScore - opponentScore;
opponentPaddleSpeed = baseOpponentPaddleSpeed + Math.floor(scoreDifference / 2);
} else {
opponentPaddleSpeed = baseOpponentPaddleSpeed;
}
// Move the opponent's paddle
if (opponentPaddleY + paddleHeight / 2 < ballY - aiPrecisionOffset) {
opponentPaddleY += opponentPaddleSpeed;
} else if (opponentPaddleY + paddleHeight / 2 > ballY + aiPrecisionOffset) {
opponentPaddleY -= opponentPaddleSpeed;
}
aiPrecisionOffset = Math.random() * 6 - 3; // Small offset for better precision
// Move the ball
ballX += ballSpeedX;
ballY += ballSpeedY;
// Ball collisions with top and bottom walls
if (ballY - ballSize / 2 < 0 || ballY + ballSize / 2 > canvas.height) {
ballSpeedY = -ballSpeedY;
}
// Ball collisions with paddles
if (
(ballX - ballSize / 2 < paddleWidth && ballY > playerPaddleY && ballY < playerPaddleY + paddleHeight) ||
(ballX + ballSize / 2 > canvas.width - paddleWidth && ballY > opponentPaddleY && ballY < opponentPaddleY + paddleHeight)
) {
ballSpeedX = -ballSpeedX;
}
// Check for scoring
if (ballX - ballSize / 2 < 0) {
opponentScore++;
resetBall();
} else if (ballX + ballSize / 2 > canvas.width) {
playerScore++;
resetBall();
}
// Update the speed bar
const speedPercentage = (opponentPaddleSpeed / 10) * 100;
speedBar.style.width = speedPercentage + '%';
}
// Function to reset the ball to the center
function resetBall() {
ballX = canvas.width / 2;
ballY = canvas.height / 2;
ballSpeedX = -ballSpeedX; // Change direction to serve the ball to the scoring player
}
// Function to draw everything on the canvas
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawPaddles();
drawBall();
drawScores();
}
// Function to run the game loop
function gameLoop() {
update();
draw();
requestAnimationFrame(gameLoop);
}
// Event listener for mouse movement to control the player's paddle
canvas.addEventListener("mousemove", (e) => {
const mouseY = e.clientY - canvas.getBoundingClientRect().top;
playerPaddleY = mouseY - paddleHeight / 2;
if (playerPaddleY < 0) {
playerPaddleY = 0;
} else if (playerPaddleY > canvas.height - paddleHeight) {
playerPaddleY = canvas.height - paddleHeight;
}
});
// Toggle difficulty mode
toggleDifficultyButton.addEventListener("click", () => {
dynamicDifficulty = !dynamicDifficulty;
toggleDifficultyButton.textContent = `Toggle Difficulty: ${dynamicDifficulty ? "Dynamic" : "Fixed"}`;
});
// Start the game loop
gameLoop();
</script>
</body>
</html>