-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
317 lines (271 loc) · 8.62 KB
/
index.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<!DOCTYPE html>
<html>
<head>
<title>Aircraft Game</title>
<style>
canvas {
border: 1px solid black;
background: #f1f1f1;
}
</style>
</head>
<body>
<div style="margin-top: 20px">
<b>How to Play:</b>
<ul>
<li>
Press Up, Down, Right, Left arrows to move your aircraft (Red Box).
</li>
<li>Press Space to Fire Bullets to kill enemy (Green box).</li>
<li>
Try to score as much as possible in <b>60 seconds</b>(Max. limit).
</li>
</ul>
</div>
<div style="margin: 5px 0">
<button id="reload">Reload</button>
</div>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<script>
//reload button
document.getElementById("reload").addEventListener("click", reloadGame);
// Get the canvas element
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
// Game variables
let score = 0;
let isGameOver = false;
const timeLimit = 60000;
let startTime; // Variable to store the start time
// Game variables
let aircraftX = 50; // X-coordinate of the aircraft
let aircraftY = canvas.height / 2; // Y-coordinate of the aircraft
let rightPressed = false;
let leftPressed = false;
let upPressed = false;
let downPressed = false;
// let spacePressed = false;
let createEnemyInterval;
let moveEnemyAndBulletInterval;
let checkCollisionInterval;
// Event listeners for player controls
document.addEventListener("keydown", keyDownHandler);
document.addEventListener("keyup", keyUpHandler);
// Event listeners for keydown and click
document.addEventListener("keydown", handleKeyDown);
// Handle keydown event
function handleKeyDown(event) {
if (event.code === "Space") {
fireBullet();
}
}
function keyDownHandler(event) {
if (event.key === "Right" || event.key === "ArrowRight") {
rightPressed = true;
} else if (event.key === "Left" || event.key === "ArrowLeft") {
leftPressed = true;
} else if (event.key === "Up" || event.key === "ArrowUp") {
upPressed = true;
} else if (event.key === "Down" || event.key === "ArrowDown") {
downPressed = true;
}
// else if (event.key === " ") {
// spacePressed = true;
// }
}
function keyUpHandler(event) {
if (event.key === "Right" || event.key === "ArrowRight") {
rightPressed = false;
} else if (event.key === "Left" || event.key === "ArrowLeft") {
leftPressed = false;
} else if (event.key === "Up" || event.key === "ArrowUp") {
upPressed = false;
} else if (event.key === "Down" || event.key === "ArrowDown") {
downPressed = false;
}
// else if (event.key === " ") {
// spacePressed = false;
// }
}
// Start the game timer
function startTimer() {
startTime = Date.now(); // Record the start time
setTimeout(gameOver, timeLimit);
}
// Calculate remaining time
function calculateRemainingTime() {
const elapsed = Date.now() - startTime;
const remaining = Math.max(0, timeLimit - elapsed);
return Math.ceil(remaining / 1000); // Convert to seconds and round up
}
// Draw remaining time
function drawTime() {
const remainingTime = calculateRemainingTime();
ctx.font = "20px Arial";
ctx.fillStyle = "black";
ctx.fillText("Time: " + remainingTime + "s", canvas.width - 100, 20);
}
// Update score
function updateScore() {
score++;
}
// Draw score
function drawScore() {
ctx.font = "16px Arial";
ctx.fillStyle = "black";
ctx.fillText("Score: " + score, 10, 20);
}
// Reload button click event handler
function reloadGame() {
location.reload();
}
// Game loop
function draw() {
if (isGameOver) {
return;
}
// Clear the canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw the aircraft
ctx.fillStyle = "red";
ctx.fillRect(aircraftX, aircraftY, 50, 50);
drawEnemies();
drawBullets();
// Move the aircraft
if (rightPressed && aircraftX < canvas.width - 50) {
aircraftX += 5;
} else if (leftPressed && aircraftX > 0) {
aircraftX -= 5;
}
if (upPressed && aircraftY > 0) {
aircraftY -= 5;
} else if (downPressed && aircraftY < canvas.height - 50) {
aircraftY += 5;
}
// Fire bullets
// if (spacePressed) {
// fireBullet();
// }
// draw score
drawScore();
// Draw remaining time
drawTime();
// Request the next frame
requestAnimationFrame(draw);
}
// Bullets
const bullets = [];
function fireBullet() {
const bullet = {
x: aircraftX + 25,
y: aircraftY,
width: 5,
height: 10,
speed: 5,
};
bullets.push(bullet);
}
function moveBullets() {
bullets.forEach((bullet) => {
bullet.y -= bullet.speed;
});
}
function drawBullets() {
bullets.forEach((bullet) => {
ctx.fillStyle = "blue";
ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
});
}
// Enemy aircrafts
const enemies = [];
function createEnemy() {
const enemy = {
x: canvas.width,
y: Math.random() * (canvas.height - 50),
width: 50,
height: 50,
speed: Math.random() * 2 + 1,
};
enemies.push(enemy);
}
function moveEnemies() {
enemies.forEach((enemy) => {
enemy.x -= enemy.speed;
});
}
function drawEnemies() {
enemies.forEach((enemy) => {
ctx.fillStyle = "green";
ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
});
}
function checkCollision(bullet, enemy) {
if (
bullet.x < enemy.x + enemy.width &&
bullet.x + bullet.width > enemy.x &&
bullet.y < enemy.y + enemy.height &&
bullet.y + bullet.height > enemy.y
) {
updateScore();
return true;
}
return false;
}
function checkCollisions() {
bullets.forEach((bullet, bulletIndex) => {
enemies.forEach((enemy, enemyIndex) => {
if (checkCollision(bullet, enemy)) {
bullets.splice(bulletIndex, 1);
enemies.splice(enemyIndex, 1);
}
});
});
}
// Game over
function gameOver() {
cancelAnimationFrame(draw);
isGameOver = true;
// Display the "Game Over" text
ctx.font = "30px Arial";
ctx.fillStyle = "black";
ctx.fillText("Game Over", canvas.width / 2 - 80, canvas.height / 2);
clearInterval(createEnemyInterval);
clearInterval(moveEnemyAndBulletInterval);
clearInterval(checkCollisionInterval);
document.removeEventListener("keydown", keyDownHandler);
document.removeEventListener("keyUp", keyUpHandler);
console.log("Game Over");
}
// Start the game
function startGame() {
startTimer();
draw();
}
// Enemy and bullet creation interval
createEnemyInterval = setInterval(() => {
createEnemy();
}, 2000);
moveEnemyAndBulletInterval = setInterval(() => {
moveBullets();
moveEnemies();
checkCollisions();
}, 10);
// Game over condition (example: player's aircraft is destroyed)
// You can adjust the condition based on your game's logic
checkCollisionInterval = setInterval(() => {
if (
enemies.some((enemy) =>
checkCollision(
{ x: aircraftX, y: aircraftY, width: 50, height: 50 },
enemy
)
)
) {
gameOver();
}
}, 100);
// Start the game loop
startGame();
</script>
</body>
</html>