-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
385 lines (330 loc) · 11.7 KB
/
script.js
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
class GameManager {
constructor(canvasId) {
this.canvas = document.getElementById(canvasId);
this.ctx = this.canvas.getContext('2d');
this.score = 0;
this.gameOver = false;
this.alertShown = false;
this.player = null;
this.enemies = [];
this.enemyProjectiles = [];
this.moveRight = true;
this.moveDown = false;
this.lastFiredTime = 0;
this.fireInterval = 1000;
this.mysteryShip = null;
}
drawScore() {
this.ctx.font = '16px Arial';
this.ctx.fillStyle = '#fff'; // Color of the score text
this.ctx.fillText('Score: ' + this.score, 20, 40); // Position the score text on the canvas
}
spawnMysteryShip() {
if (!this.mysteryShip || !this.mysteryShip.active) {
this.mysteryShip = new MysteryShip(this.canvas.width, 5); // Mystery ship speed is 5
}
}
createEnemies() {
const rows = 3;
const cols = 10;
const enemyWidth = 40;
const enemyHeight = 40;
const enemySpacing = 10;
const totalWidth = cols * (enemyWidth + enemySpacing);
const startX = (this.canvas.width - totalWidth) / 2;
const startY = 30; // Start 30px down from the top of the canvas
const enemyImages = {};
for (let row = 1; row <= rows; row++) {
enemyImages['row' + row] = new Image();
enemyImages['row' + row].src = `SliceE${row}.png`;
}
// Continue for other images
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
let x = startX + col * (enemyWidth + enemySpacing);
let y = startY + row * (enemyHeight + enemySpacing);
let image = enemyImages['row' + (row + 1)];
let enemy = new Enemy(x, y, image);
enemy.row = row; // Assign row index
this.enemies.push(enemy);
}
}
this.totalRows = rows; // Set total number of rows
}
updateEnemies() {
let edgeReached = false;
this.enemies.forEach(enemy => {
enemy.move(this.moveRight, this.moveDown);
// Check if any enemy has reached the edge
if (enemy.x + enemy.width > this.canvas.width || enemy.x < 0) {
edgeReached = true;
}
});
if (edgeReached) {
this.moveRight = !this.moveRight;
this.moveDown = true;
} else {
this.moveDown = false;
}
// Random firing logic
if (this.enemies.length > 0 && Date.now() - this.lastFiredTime > this.fireInterval) {
const firingEnemy = this.enemies[Math.floor(Math.random() * this.enemies.length)];
this.enemyProjectiles.push(firingEnemy.fire());
this.lastFiredTime = Date.now();
}
}
drawEnemies() {
this.enemies.forEach(enemy => enemy.draw(this.ctx));
}
checkPlayerProjectileCollisions() {
this.player.projectiles.forEach((projectile, pIndex) => {
this.enemies.forEach((enemy, eIndex) => {
if (projectile.x < enemy.x + enemy.width &&
projectile.x > enemy.x &&
projectile.y < enemy.y + enemy.height &&
projectile.y > enemy.y) {
// Collision detected
this.player.projectiles.splice(pIndex, 1); // Remove the projectile
const rowScore = (this.totalRows - enemy.row) * 10; // Row 1 = 30 points, Row 2 = 20 points, Row 3 = 10 points
this.score += rowScore;
this.enemies.splice(eIndex, 1); // Remove the enemy
}
});
});
}
checkEnemyProjectileCollisions() {
this.enemyProjectiles.forEach((projectile) => {
if (projectile.x < this.player.x + this.player.width &&
projectile.x > this.player.x &&
projectile.y < this.player.y + this.player.height &&
projectile.y > this.player.y) {
// Collision detected with player ship
this.gameOver = true;
}
});
}
checkEnemyShipCollisions() {
this.enemies.forEach((enemy) => {
if (enemy.y + enemy.height >= this.player.y) {
// Enemy has reached the player's ship
this.gameOver = true;
}
});
}
checkGameCompletion() {
if (this.enemies.length === 0) {
this.gameOver = true;
this.playerWon = true;
}
}
checkMysteryShipCollision() {
if (this.mysteryShip && this.mysteryShip.active) {
this.player.projectiles.forEach((projectile, pIndex) => {
if (projectile.x < this.mysteryShip.x + this.mysteryShip.width &&
projectile.x > this.mysteryShip.x &&
projectile.y < this.mysteryShip.y + this.mysteryShip.height &&
projectile.y > this.mysteryShip.y) {
// Collision detected
this.player.projectiles.splice(pIndex, 1); // Remove the projectile
this.score += 1000; // Award points
this.mysteryShip.active = false; // Deactivate the mystery ship
}
});
}
}
start() {
setInterval(() => this.spawnMysteryShip(), 10000); // Spawn every 10 seconds
this.player = new Player(this.canvas.width / 2, this.canvas.height - 120, this.canvas);
// Create enemies and add them to this.enemies array
this.createEnemies();
this.mainLoop();
}
mainLoop() {
if (!this.gameOver) {
this.checkPlayerProjectileCollisions();
this.checkEnemyProjectileCollisions();
this.checkEnemyShipCollisions();
this.checkGameCompletion();
// Clear the canvas
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
// Draw the player
this.player.move();
this.player.draw(this.ctx);
this.player.projectiles.forEach(projectile => {
projectile.update();
projectile.draw(this.ctx);
});
// Draw the enemies
this.drawEnemies();
this.updateEnemies();
this.enemyProjectiles.forEach(projectile => {
projectile.update();
projectile.draw(this.ctx);
});
// Draw the mystery ship
if (this.mysteryShip) {
this.mysteryShip.move();
this.mysteryShip.draw(this.ctx);
}
this.checkMysteryShipCollision();
// Draw the score
this.drawScore();
requestAnimationFrame(() => this.mainLoop());
} else {
// Handle game over or game completion scenario
if (this.playerWon) {
// Display a winning message
alert('Congratulations! You won!');
} else {
alert("Game Over!");
}
}
}
}
class Player {
constructor(x, y, canvas) {
this.x = x;
this.y = y;
this.canvas = canvas;
this.width = 57; // Width of the player ship
this.height = 90; // Height of the player ship
this.speed = 5; // Speed of the player ship
this.projectiles = []; // To store fired projectiles
this.moveLeft = false;
this.moveRight = false;
this.image = new Image();
this.image.src = 'Slice1.png';
document.addEventListener('keydown', (event) => {
switch(event.key) {
case 'ArrowLeft':
// Move player left
this.moveLeft = true;
break;
case 'ArrowRight':
// Move player right
this.moveRight = true;
break;
case 'ArrowUp':
// Player fires
this.fire();
break;
}
});
document.addEventListener('keyup', (event) => {
switch(event.key) {
case 'ArrowLeft':
// Move player left
this.moveLeft = false;
break;
case 'ArrowRight':
// Move player right
this.moveRight = false;
break;
}
});
}
move() {
// Assuming direction is either 'left' or 'right'
if (this.moveLeft) {
this.x = Math.max(0, this.x - this.speed);
}
if (this.moveRight) {
this.x = Math.min(this.canvas.width - this.width, this.x + this.speed);
}
// Optional: Prevent the player from moving off the canvas
this.x = Math.max(0, Math.min(this.x, this.canvas.width - this.width));
}
fire() {
// Create a new projectile and add it to the projectiles array
// Assuming the projectile starts from the middle of the ship
let projectile = new Projectile(this.x + this.width / 2, this.y, 5);
projectile.sound.play();
this.projectiles.push(projectile);
}
draw(ctx) {
ctx.drawImage(this.image, this.x, this.y, this.width, this.height);
// Draw each projectile
this.projectiles.forEach(projectile => {
projectile.draw(ctx);
});
}
}
class Projectile {
constructor(x, y, speed, enemyFire = false) {
this.width = 20;
this.height = 40;
this.x = x - 10;
this.y = y;
this.speed = speed;
this.enemyFire = enemyFire;
this.image = new Image();
this.image.src = enemyFire ? 'Slice8.png' : 'Slice7.png';
this.sound = new Audio('shot.wav');
}
update() {
if (this.enemyFire) {
return this.y += this.speed;
}
// Move the projectile up
this.y -= this.speed;
}
draw(ctx) {
ctx.drawImage(this.image, this.x, this.y, this.width, this.height);
}
}
class Enemy {
constructor(x, y, image) {
this.x = x;
this.y = y;
this.width = 40;
this.height = 40;
this.horizontalMove = 1;
this.verticalMove = 10;
this.image = image;
}
move(moveRight, moveDown) {
if (moveRight) {
this.x += this.horizontalMove;
} else {
this.x -= this.horizontalMove;
}
if (moveDown) {
this.y += this.verticalMove;
}
}
draw(ctx) {
ctx.drawImage(this.image, this.x, this.y, this.width, this.height);
}
fire() {
// Assuming the projectile starts from the middle-bottom of the enemy
return new Projectile(this.x + this.width / 2, this.y + this.height, 3, true); // Speed of the projectile is 3
}
}
class MysteryShip {
constructor(canvasWidth, speed) {
this.x = -50;
this.y = 50;
this.width = 50;
this.height = 50;
this.speed = speed;
this.active = true; // Is the ship currently active?
this.canvasWidth = canvasWidth;
this.image = new Image();
this.image.src = 'Slice6.png';
}
move() {
if (this.active) {
this.x += this.speed;
if (this.x > this.canvasWidth) { // If it moves past the right edge
this.active = false; // Deactivate the ship
}
}
}
draw(ctx) {
if (this.active) {
ctx.drawImage(this.image, this.x, this.y, this.width, this.height);
}
}
}
const game = new GameManager('gameCanvas');
game.start();