-
Notifications
You must be signed in to change notification settings - Fork 0
/
space-invaders.js
201 lines (178 loc) · 4.86 KB
/
space-invaders.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
// space-invaders.js
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
canvas.width = 800;
canvas.height = 600;
const player = {
x: canvas.width / 2 - 15,
y: canvas.height - 40,
width: 30,
height: 30,
dx: 5,
bullets: []
};
const aliens = [];
const alienRows = 5;
const alienCols = 10;
const alienWidth = 40;
const alienHeight = 30;
const alienPadding = 10;
const alienOffsetTop = 30;
const alienOffsetLeft = 30;
let alienDx = 2;
let alienDy = 0;
let rightPressed = false;
let leftPressed = false;
let spacePressed = false;
document.addEventListener('keydown', keyDownHandler);
document.addEventListener('keyup', keyUpHandler);
function keyDownHandler(e) {
if (e.key === 'Right' || e.key === 'ArrowRight') {
rightPressed = true;
} else if (e.key === 'Left' || e.key === 'ArrowLeft') {
leftPressed = true;
} else if (e.key === ' ' || e.key === 'Spacebar') {
spacePressed = true;
}
}
function keyUpHandler(e) {
if (e.key === 'Right' || e.key === 'ArrowRight') {
rightPressed = false;
} else if (e.key === 'Left' || e.key === 'ArrowLeft') {
leftPressed = false;
} else if (e.key === ' ' || e.key === 'Spacebar') {
spacePressed = false;
shootBullet();
}
}
function createAliens() {
for (let c = 0; c < alienCols; c++) {
aliens[c] = [];
for (let r = 0; r < alienRows; r++) {
const alienX = (c * (alienWidth + alienPadding)) + alienOffsetLeft;
const alienY = (r * (alienHeight + alienPadding)) + alienOffsetTop;
aliens[c][r] = { x: alienX, y: alienY, status: 1 };
}
}
}
function drawPlayer() {
ctx.fillStyle = 'green';
ctx.fillRect(player.x, player.y, player.width, player.height);
}
function drawAliens() {
for (let c = 0; c < alienCols; c++) {
for (let r = 0; r < alienRows; r++) {
if (aliens[c][r].status === 1) {
const alienX = aliens[c][r].x;
const alienY = aliens[c][r].y;
ctx.fillStyle = 'red';
ctx.fillRect(alienX, alienY, alienWidth, alienHeight);
ctx.beginPath();
ctx.arc(alienX + alienWidth / 2, alienY + alienHeight / 2, 10, 0, Math.PI * 2);
ctx.fillStyle = 'white';
ctx.fill();
ctx.closePath();
}
}
}
}
function drawBullets() {
ctx.fillStyle = 'yellow';
player.bullets.forEach(bullet => {
ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
});
}
function movePlayer() {
if (rightPressed && player.x < canvas.width - player.width) {
player.x += player.dx;
} else if (leftPressed && player.x > 0) {
player.x -= player.dx;
}
}
function moveAliens() {
let moveDown = false;
for (let c = 0; c < alienCols; c++) {
for (let r = 0; r < alienRows; r++) {
if (aliens[c][r].status === 1) {
aliens[c][r].x += alienDx;
if (aliens[c][r].x + alienWidth > canvas.width || aliens[c][r].x < 0) {
moveDown = true;
}
}
}
}
if (moveDown) {
for (let c = 0; c < alienCols; c++) {
for (let r = 0; r < alienRows; r++) {
aliens[c][r].x -= alienDx;
aliens[c][r].y += 10;
}
}
alienDx = -alienDx;
}
}
function shootBullet() {
const bullet = {
x: player.x + player.width / 2 - 2.5,
y: player.y - 10,
width: 5,
height: 10,
dy: -5
};
player.bullets.push(bullet);
}
function updateBullets() {
player.bullets.forEach((bullet, index) => {
bullet.y += bullet.dy;
if (bullet.y + bullet.height < 0) {
player.bullets.splice(index, 1);
}
});
}
function detectCollisions() {
player.bullets.forEach((bullet, bulletIndex) => {
for (let c = 0; c < alienCols; c++) {
for (let r = 0; r < alienRows; r++) {
const alien = aliens[c][r];
if (alien.status === 1 && bullet.x < alien.x + alienWidth && bullet.x + bullet.width > alien.x && bullet.y < alien.y + alienHeight && bullet.y + bullet.height > alien.y) {
alien.status = 0;
player.bullets.splice(bulletIndex, 1);
break;
}
}
}
});
}
function checkWin() {
for (let c = 0; c < alienCols; c++) {
for (let r = 0; r < alienRows; r++) {
if (aliens[c][r].status === 1) {
return false;
}
}
}
return true;
}
function displayWinMessage() {
ctx.font = '48px serif';
ctx.fillStyle = 'white';
ctx.textAlign = 'center';
ctx.fillText('Congratulations! You won!', canvas.width / 2, canvas.height / 2);
}
function update() {
if (checkWin()) {
displayWinMessage();
return;
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawPlayer();
drawAliens();
drawBullets();
movePlayer();
moveAliens();
updateBullets();
detectCollisions();
requestAnimationFrame(update);
}
createAliens();
update();