-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
450 lines (392 loc) · 16.6 KB
/
game.js
File metadata and controls
450 lines (392 loc) · 16.6 KB
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
let texturesCreated = false;
// Player class
class Player extends Phaser.GameObjects.Sprite {
constructor(scene, bulletGroup) {
super(scene, scene.cameras.main.width / 2, scene.cameras.main.height - 50, 'player_frame1');
this.scene = scene;
this.bulletGroup = bulletGroup;
this.speed = 5;
this.shootDelay = 250;
this.lastShot = 0;
this.play('player_anim');
this.setOrigin(0.5, 1);
this.scene.physics.add.existing(this); // Enable physics
this.setDepth(5);
}
update() {
if (this.scene.cursors.left.isDown) {
console.log('Left key pressed'); // Debug log
this.x -= this.speed;
}
if (this.scene.cursors.right.isDown) {
console.log('Right key pressed'); // Debug log
this.x += this.speed;
}
if (Phaser.Input.Keyboard.JustDown(this.scene.spaceKey)) {
console.log('Space key pressed'); // Debug log
this.shoot();
}
// Clamp position to keep player fully within canvas
this.x = Phaser.Math.Clamp(this.x, this.width / 2, this.scene.cameras.main.width - this.width / 2);
}
shoot() {
const now = this.scene.time.now;
if (now - this.lastShot > this.shootDelay) {
this.lastShot = now;
const bullet = new Bullet(this.scene, this.x, this.y - 25, -1);
this.bulletGroup.add(bullet);
}
}
}
// Bullet class
class Bullet extends Phaser.GameObjects.Rectangle {
constructor(scene, x, y, direction) {
super(scene, x, y, 5, 10, 0xffffff);
this.scene.add.existing(this); // For rendering
this.scene.physics.add.existing(this); // For physics
this.speed = 10 * direction;
}
update() {
this.y += this.speed;
if (this.y < 0 || this.y > 600) this.destroy();
}
}
// Alien class
class Alien extends Phaser.GameObjects.Sprite {
constructor(scene, x, y, colorKey) {
super(scene, x, y, 'alien_frame1');
this.scene = scene;
this.setScale(0.7); // Reduced from 0.75 to 0.6
this.scene.add.existing(this);
this.scene.physics.add.existing(this);
this.setDepth(10);
this.setVisible(true);
this.setAlpha(1); // Fully opaque
// Get the base color for the level
const baseColor = scene.levelColors[(scene.level - 1) % scene.levelColors.length];
// Apply a solid tint based on the level color
this.setTint(Phaser.Display.Color.GetColor(baseColor[0], baseColor[1], baseColor[2]));
// Use normal blend mode
this.setBlendMode(Phaser.BlendModes.NORMAL);
// Play the alien animation
this.play('alien_anim_original');
console.log(`Alien created at x: ${this.x}, y: ${this.y}, tint: ${baseColor}`);
}
}
// AlienGroup class
class AlienGroup {
constructor(scene, bulletGroup, level, barriers) {
this.scene = scene;
this.aliens = scene.physics.add.group(); // Physics group for aliens
this.bulletGroup = bulletGroup;
this.barriers = barriers;
this.direction = 1;
this.baseSpeed = 1;
this.baseShootInterval = 1000;
this.downSpeed = 8;
this.level = level;
this.updateDifficulty();
this.tintColor = scene.levelColors[(level - 1) % scene.levelColors.length];
this.lastShot = 0;
this.createAliens();
}
updateDifficulty() {
this.speed = Math.min(3, this.baseSpeed + (this.level - 1) * 0.2);
this.shootInterval = Math.max(700, this.baseShootInterval - (this.level - 1) * 25);
}
createAliens() {
const colorKey = `color${(this.level - 1) % this.scene.levelColors.length}`;
const rows = Math.min(5 + Math.floor((this.level - 1) / 4), 7);
const cols = Math.min(10 + Math.floor((this.level - 1) / 6), 12);
// Define the scale (matches Alien class)
const scale = 0.6; // Reduced from 0.75 to 0.6
// Get original sprite dimensions
const alienTexture = this.scene.textures.get('alien_frame1');
const originalWidth = alienTexture.source[0].width;
const originalHeight = alienTexture.source[0].height;
// Calculate scaled dimensions
const scaledWidth = originalWidth * scale;
const scaledHeight = originalHeight * scale;
// Define spacing with a smaller gap
const gapX = 19; // Reduced from 5 to 3
const gapY = 7; // Reduced from 5 to 3
const spacingX = scaledWidth + gapX;
const spacingY = scaledHeight + gapY;
// Calculate total formation width and height
const totalWidth = (cols - 1) * spacingX;
const totalHeight = (rows - 1) * spacingY;
// Center horizontally, set a top margin
const startX = (this.scene.cameras.main.width - totalWidth) / 2;
const startY = 50; // Top margin, adjustable
// Create the aliens
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const x = startX + col * spacingX;
const y = startY + row * spacingY;
const alien = new Alien(this.scene, x, y, colorKey);
this.aliens.add(alien);
console.log(`Added alien to group at x: ${alien.x}, y: ${alien.y}, texture: ${alien.texture.key}`);
}
}
console.log(`Total aliens in group: ${this.aliens.getLength()}`);
}
update() {
let edgeHit = false;
const scale = 0.6; // Matches scale used above
const scaledWidth = this.scene.textures.get('alien_frame1').source[0].width * scale;
const halfWidth = scaledWidth / 2;
this.aliens.getChildren().forEach(alien => {
alien.x += this.speed * this.direction;
if (alien.x + halfWidth >= this.scene.cameras.main.width || alien.x - halfWidth <= 0) {
edgeHit = true;
}
});
if (edgeHit) {
this.direction *= -1;
this.aliens.getChildren().forEach(alien => alien.y += this.downSpeed);
}
const now = this.scene.time.now;
if (now - this.lastShot > this.shootInterval && this.aliens.getLength() > 0) {
this.lastShot = now;
const shooter = Phaser.Utils.Array.GetRandom(this.aliens.getChildren());
const bullet = new Bullet(this.scene, shooter.x, shooter.y + 20, 1);
this.bulletGroup.add(bullet);
}
}
}
// BarrierBlock class
class BarrierBlock extends Phaser.GameObjects.Sprite {
constructor(scene, x, y) {
super(scene, x, y, 'barrier_health3');
this.scene.add.existing(this);
this.setScale(0.7); // Reduce size by 30%
this.health = 3;
this.setDepth(5);
}
hit() {
this.health -= 1;
if (this.health <= 0) this.destroy();
else this.setTexture(`barrier_health${this.health}`);
}
}
// Explosion class
class Explosion extends Phaser.GameObjects.Sprite {
constructor(scene, x, y) {
super(scene, x, y, 'explosion_frame1');
this.scene.add.existing(this);
this.play('explosion_anim');
this.on('animationcomplete', () => this.destroy());
}
}
// MainScene class
class MainScene extends Phaser.Scene {
constructor() {
super('MainScene');
}
preload() {
this.load.image('background', 'assets/space_background.png');
for (let i = 1; i <= 7; i++) {
this.load.image(`player_frame${i}`, `assets/spaceship${i}.png`);
this.load.image(`alien_frame${i}`, `assets/alien${i}.png`);
this.load.image(`explosion_frame${i}`, `assets/explosion${i}.png`);
}
this.load.image('barrier_health3', 'assets/barrier_health3.png');
this.load.image('barrier_health2', 'assets/barrier_health2.png');
this.load.image('barrier_health1', 'assets/barrier_health1.png');
}
create() {
this.score = 0;
this.lives = 3;
this.level = 1;
this.continuesLeft = 3;
const background = this.add.image(0, 0, 'background').setOrigin(0, 0).setDepth(0);
background.setDisplaySize(this.cameras.main.width, this.cameras.main.height);
this.cameras.main.setBackgroundColor('#000000');
this.cameras.main.setBounds(0, 0, 800, 600);
// Updated levelColors with a stronger blue for level 2
this.levelColors = [
[255, 255, 255], // Bright green for level 1
[0, 255, 0], // Stronger blue for level 2
[255, 0, 0], // Bright red for level 3
[0, 102, 255], // Bright yellow for level 4
[245, 176, 65], // Bright cyan for level 5
[153, 0, 255] // Bright magenta for level 6
];
// Animations
this.anims.create({
key: 'player_anim',
frames: Array.from({ length: 7 }, (_, i) => ({ key: `player_frame${i + 1}` })),
frameRate: 10,
repeat: -1
});
this.anims.create({
key: 'explosion_anim',
frames: Array.from({ length: 7 }, (_, i) => ({ key: `explosion_frame${i + 1}` })),
frameRate: 10,
repeat: 0
});
this.anims.create({
key: 'alien_anim_original',
frames: Array.from({ length: 7 }, (_, i) => ({ key: `alien_frame${i + 1}` })),
frameRate: 10,
repeat: -1
});
this.cursors = this.input.keyboard.createCursorKeys();
this.spaceKey = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);
// Initialize groups
this.playerBullets = this.physics.add.group();
this.alienBullets = this.physics.add.group();
this.explosions = this.add.group();
this.barriers = this.createBarriers();
this.player = new Player(this, this.playerBullets);
this.add.existing(this.player);
this.alienGroup = new AlienGroup(this, this.alienBullets, this.level, this.barriers);
// UI text
this.scoreText = this.add.text(10, 10, `Score: ${this.score}`, { fontSize: '16px', color: '#fff' });
this.livesText = this.add.text(650, 10, `Lives: ${this.lives}`, { fontSize: '16px', color: '#fff' });
this.levelText = this.add.text(350, 10, `Level: ${this.level}`, { fontSize: '16px', color: '#fff' });
// Set up colliders
this.setupColliders();
}
setupColliders() {
this.physics.add.collider(this.playerBullets, this.alienGroup.aliens, (bullet, alien) => {
bullet.destroy();
alien.destroy();
this.score += 10;
this.explosions.add(new Explosion(this, alien.x, alien.y));
this.scoreText.setText(`Score: ${this.score}`);
});
this.physics.add.collider(this.player, this.alienBullets, (player, bullet) => {
bullet.destroy();
this.lives -= 1;
this.explosions.add(new Explosion(this, player.x, player.y));
this.livesText.setText(`Lives: ${this.lives}`);
});
this.physics.add.collider(this.playerBullets, this.barriers, (bullet, barrier) => {
bullet.destroy();
barrier.hit();
});
this.physics.add.collider(this.alienBullets, this.barriers, (bullet, barrier) => {
bullet.destroy();
barrier.hit();
});
}
createBarriers() {
const barriers = this.physics.add.group();
const barrierY = this.cameras.main.height * 0.7;
const barrierWidth = 5 * 14;
const totalSpace = this.cameras.main.width * 0.8;
const gap = (totalSpace - 4 * barrierWidth) / 3;
const startX = this.cameras.main.width * 0.1;
for (let i = 0; i < 4; i++) {
const barrierX = startX + i * (barrierWidth + gap);
for (let row = 0; row < 4; row++) {
for (let col = 0; col < 5; col++) {
const x = barrierX + col * 14;
const y = barrierY + row * 14;
const barrier = new BarrierBlock(this, x, y);
barriers.add(barrier);
}
}
}
return barriers;
}
update() {
this.player.update();
this.alienGroup.update();
this.playerBullets.getChildren().forEach(b => b.update());
this.alienBullets.getChildren().forEach(b => b.update());
if (this.alienGroup.aliens.getLength() === 0) {
this.level += 1;
this.levelText.setText(`Level: ${this.level}`);
this.alienGroup = new AlienGroup(this, this.alienBullets, this.level, this.barriers);
this.setupColliders(); // Reapply colliders for new aliens
}
let gameOver = this.lives <= 0;
this.alienGroup.aliens.getChildren().forEach(alien => {
if (alien.y + 20 >= 500) gameOver = true;
});
if (gameOver) {
if (this.continuesLeft > 0) {
this.scene.launch('ContinueScene', {
continuesLeft: this.continuesLeft,
score: this.score,
level: this.level
});
this.scene.pause();
} else {
this.scene.start('GameOverScene', { score: this.score });
}
}
const lowestAlienY = Math.max(...this.alienGroup.aliens.getChildren().map(a => a.y + 20));
const topBarrierY = Math.min(...this.barriers.getChildren().map(b => b.y));
if (lowestAlienY >= topBarrierY) {
this.barriers.getChildren().filter(b => b.y === topBarrierY).forEach(b => b.destroy());
}
}
}
// ContinueScene class
class ContinueScene extends Phaser.Scene {
constructor() {
super('ContinueScene');
}
create(data) {
this.continuesLeft = data.continuesLeft;
this.score = data.score;
this.level = data.level;
this.add.image(0, 0, 'background').setOrigin(0, 0);
this.add.text(300, 250, `Continues left: ${this.continuesLeft}`, { fontSize: '16px', color: '#fff' });
this.add.text(200, 300, 'Press C to continue, R to restart, Q to quit', { fontSize: '16px', color: '#fff' });
this.input.keyboard.on('keydown-C', () => {
this.scene.stop();
const mainScene = this.scene.get('MainScene');
mainScene.continuesLeft -= 1;
mainScene.lives = 3;
mainScene.player.setPosition(400, 590);
mainScene.playerBullets.clear(true, true);
mainScene.alienBullets.clear(true, true);
mainScene.explosions.clear(true, true);
mainScene.barriers = mainScene.createBarriers();
if (mainScene.alienGroup) {
mainScene.alienGroup.aliens.clear(true, true);
}
mainScene.alienGroup = new AlienGroup(mainScene, mainScene.alienBullets, this.level, mainScene.barriers);
mainScene.setupColliders();
mainScene.livesText.setText(`Lives: 3`);
mainScene.scene.resume();
});
}
}
// GameOverScene class
class GameOverScene extends Phaser.Scene {
constructor() {
super('GameOverScene');
}
create(data) {
this.add.image(0, 0, 'background').setOrigin(0, 0);
this.add.text(350, 250, 'Game Over!', { fontSize: '32px', color: '#ff0000' });
this.add.text(350, 300, `Final Score: ${data.score}`, { fontSize: '16px', color: '#fff' });
this.add.text(250, 350, 'Press R to Restart or Q to Quit', { fontSize: '16px', color: '#fff' });
this.input.keyboard.on('keydown-R', () => this.scene.start('MainScene'));
this.input.keyboard.on('keydown-Q', () => this.game.destroy(true));
}
}
// Game configuration
const config = {
type: Phaser.AUTO,
width: Math.min(800, window.innerWidth * 0.8),
height: Math.min(600, window.innerHeight * 0.8),
scene: [MainScene, ContinueScene, GameOverScene],
parent: 'game-container',
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH
},
physics: {
default: 'arcade', // Explicitly enable arcade physics
arcade: {
debug: false // Set to true for debugging physics issues
}
}
};
const game = new Phaser.Game(config);