-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
187 lines (158 loc) · 6.06 KB
/
game.js
File metadata and controls
187 lines (158 loc) · 6.06 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
class Game {
constructor() {
this.canvas = document.getElementById('gameCanvas');
this.ctx = this.canvas.getContext('2d');
this.canvas.width = 800;
this.canvas.height = 600;
this.player = null;
this.enemies = [];
this.score = 0;
this.gameOver = false;
this.keys = {
w: false,
a: false,
s: false,
d: false
};
this.mouseX = 0;
this.mouseY = 0;
this.setupEventListeners();
this.showStartScreen();
}
setupEventListeners() {
// Keyboard controls
window.addEventListener('keydown', (e) => {
if (e.key.toLowerCase() in this.keys) {
this.keys[e.key.toLowerCase()] = true;
}
});
window.addEventListener('keyup', (e) => {
if (e.key.toLowerCase() in this.keys) {
this.keys[e.key.toLowerCase()] = false;
}
});
// Mouse controls
this.canvas.addEventListener('mousemove', (e) => {
const rect = this.canvas.getBoundingClientRect();
this.mouseX = e.clientX - rect.left;
this.mouseY = e.clientY - rect.top;
});
this.canvas.addEventListener('click', () => {
if (!this.gameOver) {
this.player.shoot();
}
});
}
showStartScreen() {
document.getElementById('startScreen').style.display = 'flex';
document.getElementById('startButton').addEventListener('click', () => {
this.startGame();
});
}
startGame() {
document.getElementById('startScreen').style.display = 'none';
this.player = new Player(this.canvas.width / 2, this.canvas.height / 2);
this.enemies = [];
this.score = 0;
this.gameOver = false;
this.spawnEnemies();
this.gameLoop();
}
spawnEnemies() {
if (this.gameOver) return;
// Spawn new enemy every 2 seconds
setInterval(() => {
if (this.enemies.length < 10) { // Maximum 10 enemies
const side = Math.floor(Math.random() * 4); // 0: top, 1: right, 2: bottom, 3: left
let x, y;
switch(side) {
case 0: // top
x = Utils.random(0, this.canvas.width);
y = -50;
break;
case 1: // right
x = this.canvas.width + 50;
y = Utils.random(0, this.canvas.height);
break;
case 2: // bottom
x = Utils.random(0, this.canvas.width);
y = this.canvas.height + 50;
break;
case 3: // left
x = -50;
y = Utils.random(0, this.canvas.height);
break;
}
this.enemies.push(new Enemy(x, y));
}
}, 2000);
}
update() {
if (this.gameOver) return;
// Update player
this.player.update(this.keys, this.mouseX, this.mouseY);
// Update enemies
for (let i = this.enemies.length - 1; i >= 0; i--) {
const enemy = this.enemies[i];
enemy.update(this.player);
// Check bullet collisions
for (let j = this.player.weapon.bullets.length - 1; j >= 0; j--) {
const bullet = this.player.weapon.bullets[j];
if (Utils.checkCollision(
{x: bullet.x - 3, y: bullet.y - 3, width: 6, height: 6},
{x: enemy.x - enemy.width/2, y: enemy.y - enemy.height/2, width: enemy.width, height: enemy.height}
)) {
if (enemy.takeDamage(bullet.damage)) {
this.enemies.splice(i, 1);
this.score += 100;
document.getElementById('scoreValue').textContent = this.score;
}
this.player.weapon.bullets.splice(j, 1);
break;
}
}
}
// Update weapon
this.player.weapon.update(this.mouseX, this.mouseY);
// Update HUD
document.getElementById('healthValue').textContent = this.player.health;
document.getElementById('ammoValue').textContent = this.player.ammo;
// Check game over
if (this.player.health <= 0) {
this.gameOver = true;
}
}
draw() {
// Clear canvas
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
// Draw game objects
this.player.draw(this.ctx);
this.player.weapon.draw(this.ctx);
for (const enemy of this.enemies) {
enemy.draw(this.ctx);
}
// Draw game over screen
if (this.gameOver) {
this.ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
this.ctx.fillStyle = 'white';
this.ctx.font = '48px Arial';
this.ctx.textAlign = 'center';
this.ctx.fillText('Game Over', this.canvas.width/2, this.canvas.height/2);
this.ctx.font = '24px Arial';
this.ctx.fillText(`Final Score: ${this.score}`, this.canvas.width/2, this.canvas.height/2 + 40);
}
}
gameLoop() {
this.update();
this.draw();
requestAnimationFrame(() => this.gameLoop());
}
restartGame() {
this.startGame();
}
}
// Start the game when the page loads
window.addEventListener('load', () => {
new Game();
});