Spaces:
Running
Running
Update index.html
Browse files- index.html +169 -19
index.html
CHANGED
@@ -1,19 +1,169 @@
|
|
1 |
-
<!
|
2 |
-
<html>
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
</
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Pong Game</title>
|
7 |
+
<style>
|
8 |
+
body {
|
9 |
+
margin: 0;
|
10 |
+
display: flex;
|
11 |
+
justify-content: center;
|
12 |
+
align-items: center;
|
13 |
+
height: 100vh;
|
14 |
+
background-color: #000;
|
15 |
+
}
|
16 |
+
canvas {
|
17 |
+
border: 2px solid #fff;
|
18 |
+
}
|
19 |
+
</style>
|
20 |
+
</head>
|
21 |
+
<body>
|
22 |
+
<canvas id="pongCanvas"></canvas>
|
23 |
+
<script>
|
24 |
+
const canvas = document.getElementById('pongCanvas');
|
25 |
+
const ctx = canvas.getContext('2d');
|
26 |
+
|
27 |
+
// Set canvas dimensions
|
28 |
+
canvas.width = 800;
|
29 |
+
canvas.height = 600;
|
30 |
+
|
31 |
+
// Ball properties
|
32 |
+
const ball = {
|
33 |
+
x: canvas.width / 2,
|
34 |
+
y: canvas.height / 2,
|
35 |
+
radius: 10,
|
36 |
+
speed: 7,
|
37 |
+
dx: 4,
|
38 |
+
dy: -4
|
39 |
+
};
|
40 |
+
|
41 |
+
// Paddle properties
|
42 |
+
const paddleWidth = 10;
|
43 |
+
const paddleHeight = 100;
|
44 |
+
const leftPaddle = {
|
45 |
+
x: 0,
|
46 |
+
y: (canvas.height - paddleHeight) / 2,
|
47 |
+
width: paddleWidth,
|
48 |
+
height: paddleHeight,
|
49 |
+
speed: 8
|
50 |
+
};
|
51 |
+
const rightPaddle = {
|
52 |
+
x: canvas.width - paddleWidth,
|
53 |
+
y: (canvas.height - paddleHeight) / 2,
|
54 |
+
width: paddleWidth,
|
55 |
+
height: paddleHeight,
|
56 |
+
speed: 8
|
57 |
+
};
|
58 |
+
|
59 |
+
// Player scores
|
60 |
+
let leftScore = 0;
|
61 |
+
let rightScore = 0;
|
62 |
+
|
63 |
+
// Draw functions
|
64 |
+
function drawBall() {
|
65 |
+
ctx.beginPath();
|
66 |
+
ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI * 2);
|
67 |
+
ctx.fillStyle = '#fff';
|
68 |
+
ctx.fill();
|
69 |
+
ctx.closePath();
|
70 |
+
}
|
71 |
+
|
72 |
+
function drawPaddle(paddle) {
|
73 |
+
ctx.beginPath();
|
74 |
+
ctx.rect(paddle.x, paddle.y, paddle.width, paddle.height);
|
75 |
+
ctx.fillStyle = '#fff';
|
76 |
+
ctx.fill();
|
77 |
+
ctx.closePath();
|
78 |
+
}
|
79 |
+
|
80 |
+
function drawScores() {
|
81 |
+
ctx.font = '30px Arial';
|
82 |
+
ctx.fillStyle = '#fff';
|
83 |
+
ctx.fillText(leftScore, canvas.width / 4, 50);
|
84 |
+
ctx.fillText(rightScore, 3 * canvas.width / 4, 50);
|
85 |
+
}
|
86 |
+
|
87 |
+
// Update game state
|
88 |
+
function update() {
|
89 |
+
// Move ball
|
90 |
+
ball.x += ball.dx;
|
91 |
+
ball.y += ball.dy;
|
92 |
+
|
93 |
+
// Bounce ball off top and bottom walls
|
94 |
+
if (ball.y + ball.radius > canvas.height || ball.y - ball.radius < 0) {
|
95 |
+
ball.dy = -ball.dy;
|
96 |
+
}
|
97 |
+
|
98 |
+
// Bounce ball off paddles
|
99 |
+
if (ball.x - ball.radius < leftPaddle.x + leftPaddle.width &&
|
100 |
+
ball.y > leftPaddle.y && ball.y < leftPaddle.y + leftPaddle.height) {
|
101 |
+
ball.dx = -ball.dx;
|
102 |
+
}
|
103 |
+
if (ball.x + ball.radius > rightPaddle.x &&
|
104 |
+
ball.y > rightPaddle.y && ball.y < rightPaddle.y + rightPaddle.height) {
|
105 |
+
ball.dx = -ball.dx;
|
106 |
+
}
|
107 |
+
|
108 |
+
// Score points
|
109 |
+
if (ball.x - ball.radius < 0) {
|
110 |
+
rightScore++;
|
111 |
+
resetBall();
|
112 |
+
}
|
113 |
+
if (ball.x + ball.radius > canvas.width) {
|
114 |
+
leftScore++;
|
115 |
+
resetBall();
|
116 |
+
}
|
117 |
+
|
118 |
+
// Move paddles
|
119 |
+
if (keys['ArrowUp'] && rightPaddle.y > 0) {
|
120 |
+
rightPaddle.y -= rightPaddle.speed;
|
121 |
+
}
|
122 |
+
if (keys['ArrowDown'] && rightPaddle.y < canvas.height - rightPaddle.height) {
|
123 |
+
rightPaddle.y += rightPaddle.speed;
|
124 |
+
}
|
125 |
+
if (keys['w'] && leftPaddle.y > 0) {
|
126 |
+
leftPaddle.y -= leftPaddle.speed;
|
127 |
+
}
|
128 |
+
if (keys['s'] && leftPaddle.y < canvas.height - leftPaddle.height) {
|
129 |
+
leftPaddle.y += leftPaddle.speed;
|
130 |
+
}
|
131 |
+
}
|
132 |
+
|
133 |
+
// Reset ball position
|
134 |
+
function resetBall() {
|
135 |
+
ball.x = canvas.width / 2;
|
136 |
+
ball.y = canvas.height / 2;
|
137 |
+
ball.dx = -ball.dx;
|
138 |
+
}
|
139 |
+
|
140 |
+
// Draw everything
|
141 |
+
function draw() {
|
142 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
143 |
+
drawBall();
|
144 |
+
drawPaddle(leftPaddle);
|
145 |
+
drawPaddle(rightPaddle);
|
146 |
+
drawScores();
|
147 |
+
}
|
148 |
+
|
149 |
+
// Game loop
|
150 |
+
function gameLoop() {
|
151 |
+
update();
|
152 |
+
draw();
|
153 |
+
requestAnimationFrame(gameLoop);
|
154 |
+
}
|
155 |
+
|
156 |
+
// Key handling
|
157 |
+
const keys = {};
|
158 |
+
document.addEventListener('keydown', function(e) {
|
159 |
+
keys[e.key] = true;
|
160 |
+
});
|
161 |
+
document.addEventListener('keyup', function(e) {
|
162 |
+
delete keys[e.key];
|
163 |
+
});
|
164 |
+
|
165 |
+
// Start the game
|
166 |
+
gameLoop();
|
167 |
+
</script>
|
168 |
+
</body>
|
169 |
+
</html>
|