Spaces:
Running
Running
Update index.html
Browse files- index.html +112 -113
index.html
CHANGED
@@ -3,7 +3,7 @@
|
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
-
<title>
|
7 |
<style>
|
8 |
body {
|
9 |
display: flex;
|
@@ -19,143 +19,142 @@
|
|
19 |
</style>
|
20 |
</head>
|
21 |
<body>
|
22 |
-
<canvas id="gameCanvas" width="800" height="
|
23 |
<script>
|
24 |
const canvas = document.getElementById('gameCanvas');
|
25 |
const ctx = canvas.getContext('2d');
|
26 |
|
27 |
// Game variables
|
28 |
-
|
29 |
-
|
30 |
-
let
|
31 |
-
let
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
y: 480,
|
42 |
-
width: 8,
|
43 |
-
height: 20,
|
44 |
-
speed: 10,
|
45 |
-
active: false
|
46 |
};
|
47 |
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
x: Math.random() * (canvas.width - enemyWidth),
|
54 |
-
y: Math.random() * 100 + 50,
|
55 |
-
speed: 2,
|
56 |
-
direction: 1
|
57 |
-
});
|
58 |
-
}
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
draw();
|
64 |
-
requestAnimationFrame(gameLoop);
|
65 |
-
}
|
66 |
-
|
67 |
-
function update() {
|
68 |
-
// Move player
|
69 |
-
if (keys.ArrowLeft && playerX > 0) playerX -= playerSpeed;
|
70 |
-
if (keys.ArrowRight && playerX < canvas.width - playerWidth) playerX += playerSpeed;
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
76 |
}
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
bullet.active = false;
|
89 |
-
enemy.y = Math.random() * 100 + 50;
|
90 |
-
enemy.x = Math.random() * (canvas.width - enemyWidth);
|
91 |
-
score++;
|
92 |
-
}
|
93 |
-
|
94 |
-
// Check for game over
|
95 |
-
if (enemy.y > playerY - enemyHeight) {
|
96 |
-
alert('Game Over! Your score: ' + score);
|
97 |
-
resetGame();
|
98 |
-
}
|
99 |
-
});
|
100 |
}
|
101 |
|
102 |
-
function
|
103 |
-
|
104 |
-
|
105 |
-
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
106 |
-
|
107 |
-
// Draw player
|
108 |
-
ctx.fillStyle = 'lime';
|
109 |
-
ctx.fillRect(playerX, playerY, playerWidth, playerHeight);
|
110 |
-
|
111 |
-
// Draw enemies
|
112 |
-
ctx.fillStyle = 'red';
|
113 |
-
enemies.forEach(enemy => {
|
114 |
-
ctx.fillRect(enemy.x, enemy.y, enemyWidth, enemyHeight);
|
115 |
-
});
|
116 |
-
|
117 |
-
// Draw bullet
|
118 |
-
if (bullet.active) {
|
119 |
-
ctx.fillStyle = 'white';
|
120 |
-
ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
|
121 |
-
}
|
122 |
|
123 |
-
|
124 |
-
|
125 |
-
ctx.font = '24px Arial';
|
126 |
-
ctx.fillText('Score: ' + score, 10, 30);
|
127 |
}
|
128 |
|
129 |
-
function
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
rect1.y + rect1.height > rect2.y;
|
134 |
}
|
135 |
|
136 |
-
function
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
}
|
144 |
|
145 |
-
|
146 |
-
|
|
|
|
|
|
|
|
|
|
|
147 |
|
148 |
-
|
149 |
-
|
150 |
-
if (
|
151 |
-
|
152 |
-
|
153 |
-
|
|
|
|
|
|
|
|
|
154 |
}
|
155 |
});
|
156 |
|
157 |
-
document.addEventListener('keyup', (
|
158 |
-
|
|
|
|
|
|
|
|
|
159 |
});
|
160 |
|
161 |
// Start the game
|
|
|
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 |
display: flex;
|
|
|
19 |
</style>
|
20 |
</head>
|
21 |
<body>
|
22 |
+
<canvas id="gameCanvas" width="800" height="500"></canvas>
|
23 |
<script>
|
24 |
const canvas = document.getElementById('gameCanvas');
|
25 |
const ctx = canvas.getContext('2d');
|
26 |
|
27 |
// Game variables
|
28 |
+
const screenWidth = 800;
|
29 |
+
const screenHeight = 500;
|
30 |
+
let playerScore = 0;
|
31 |
+
let opponentScore = 0;
|
32 |
+
|
33 |
+
// Ball
|
34 |
+
const ball = {
|
35 |
+
x: screenWidth / 2,
|
36 |
+
y: screenHeight / 2,
|
37 |
+
width: 30,
|
38 |
+
height: 30,
|
39 |
+
speedX: 7,
|
40 |
+
speedY: 7
|
|
|
|
|
|
|
|
|
|
|
41 |
};
|
42 |
|
43 |
+
// Paddles
|
44 |
+
const paddleWidth = 10;
|
45 |
+
const paddleHeight = 140;
|
46 |
+
const player = {
|
47 |
+
x: screenWidth - 20,
|
48 |
+
y: screenHeight / 2 - paddleHeight / 2,
|
49 |
+
width: paddleWidth,
|
50 |
+
height: paddleHeight,
|
51 |
+
speed: 0
|
52 |
+
};
|
53 |
+
const opponent = {
|
54 |
+
x: 10,
|
55 |
+
y: screenHeight / 2 - paddleHeight / 2,
|
56 |
+
width: paddleWidth,
|
57 |
+
height: paddleHeight,
|
58 |
+
speed: 0
|
59 |
+
};
|
60 |
|
61 |
+
function ballAnimation() {
|
62 |
+
ball.x += ball.speedX;
|
63 |
+
ball.y += ball.speedY;
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
+
if (ball.y <= 0 || ball.y + ball.height >= screenHeight) {
|
66 |
+
ball.speedY *= -1;
|
67 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
+
if (ball.x + ball.width >= screenWidth) {
|
70 |
+
opponentScore++;
|
71 |
+
ballRestart();
|
72 |
+
} else if (ball.x <= 0) {
|
73 |
+
playerScore++;
|
74 |
+
ballRestart();
|
75 |
}
|
76 |
|
77 |
+
if (
|
78 |
+
(ball.x <= opponent.x + opponent.width &&
|
79 |
+
ball.y + ball.height >= opponent.y &&
|
80 |
+
ball.y <= opponent.y + opponent.height) ||
|
81 |
+
(ball.x + ball.width >= player.x &&
|
82 |
+
ball.y + ball.height >= player.y &&
|
83 |
+
ball.y <= player.y + player.height)
|
84 |
+
) {
|
85 |
+
ball.speedX *= -1;
|
86 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
}
|
88 |
|
89 |
+
function playerAnimation() {
|
90 |
+
player.y = Math.max(0, Math.min(screenHeight - player.height, player.y + player.speed));
|
91 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
+
function opponentAnimation() {
|
94 |
+
opponent.y = Math.max(0, Math.min(screenHeight - opponent.height, opponent.y + opponent.speed));
|
|
|
|
|
95 |
}
|
96 |
|
97 |
+
function ballRestart() {
|
98 |
+
ball.x = screenWidth / 2;
|
99 |
+
ball.y = screenHeight / 2;
|
100 |
+
ball.speedX *= -1;
|
|
|
101 |
}
|
102 |
|
103 |
+
function draw() {
|
104 |
+
// Clear canvas
|
105 |
+
ctx.fillStyle = 'rgb(30, 30, 30)';
|
106 |
+
ctx.fillRect(0, 0, screenWidth, screenHeight);
|
107 |
+
|
108 |
+
// Draw middle line
|
109 |
+
ctx.strokeStyle = 'rgb(200, 200, 200)';
|
110 |
+
ctx.beginPath();
|
111 |
+
ctx.moveTo(screenWidth / 2, 0);
|
112 |
+
ctx.lineTo(screenWidth / 2, screenHeight);
|
113 |
+
ctx.stroke();
|
114 |
+
|
115 |
+
// Draw paddles
|
116 |
+
ctx.fillStyle = 'rgb(200, 200, 200)';
|
117 |
+
ctx.fillRect(player.x, player.y, player.width, player.height);
|
118 |
+
ctx.fillRect(opponent.x, opponent.y, opponent.width, opponent.height);
|
119 |
+
|
120 |
+
// Draw ball
|
121 |
+
ctx.beginPath();
|
122 |
+
ctx.arc(ball.x + ball.width / 2, ball.y + ball.height / 2, ball.width / 2, 0, Math.PI * 2);
|
123 |
+
ctx.fill();
|
124 |
+
|
125 |
+
// Draw scores
|
126 |
+
ctx.font = '32px Arial';
|
127 |
+
ctx.fillText(playerScore, 420, 40);
|
128 |
+
ctx.fillText(opponentScore, 360, 40);
|
129 |
}
|
130 |
|
131 |
+
function gameLoop() {
|
132 |
+
ballAnimation();
|
133 |
+
playerAnimation();
|
134 |
+
opponentAnimation();
|
135 |
+
draw();
|
136 |
+
requestAnimationFrame(gameLoop);
|
137 |
+
}
|
138 |
|
139 |
+
// Keyboard controls
|
140 |
+
document.addEventListener('keydown', (event) => {
|
141 |
+
if (event.key === 'ArrowUp') {
|
142 |
+
player.speed = -7;
|
143 |
+
} else if (event.key === 'ArrowDown') {
|
144 |
+
player.speed = 7;
|
145 |
+
} else if (event.key === 'w') {
|
146 |
+
opponent.speed = -7;
|
147 |
+
} else if (event.key === 's') {
|
148 |
+
opponent.speed = 7;
|
149 |
}
|
150 |
});
|
151 |
|
152 |
+
document.addEventListener('keyup', (event) => {
|
153 |
+
if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
|
154 |
+
player.speed = 0;
|
155 |
+
} else if (event.key === 'w' || event.key === 's') {
|
156 |
+
opponent.speed = 0;
|
157 |
+
}
|
158 |
});
|
159 |
|
160 |
// Start the game
|