-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.js
More file actions
395 lines (309 loc) · 11.9 KB
/
project.js
File metadata and controls
395 lines (309 loc) · 11.9 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
var canvas = document.getElementById('can');
var ctx = canvas.getContext('2d');
var animationTimeout = null;
const playButton = document.getElementById('playButton');
canvas.width = 3 * window.innerWidth / 5;
canvas.height = window.innerHeight - "100";
const backImg = new Image()
backImg.src = "./space.jpg"
$(document).ready(
function() {
playButton.addEventListener("click", startGame);
var firstRawRightBourd = false;
var firstRawLeftBourd = true; // the last bourd which has been touched
var secondRawRightBourd = false;
var secondRawLeftBourd = true;
var numberOfAliensInRaw = 7;
var alienList1 = [];
var alienList2 = [];
function allAliensDown() {
for (var i = 0; i < numberOfAliensInRaw; i++) {
alienList1[i].moveDown();
alienList2[i].moveDown();
}
}
function allSeconRawDead() {
for (var i = 0; i < numberOfAliensInRaw; i++) {
if (alienList2[i].alive) return false;
}
return true;
}
class Alien {
constructor(x, y, raw) {
this.width = 50;
this.height = 50;
this.raw = raw;
this.x = x;
this.y = y;
this.alive = true;
this.image = new Image();
this.image.src = './alien.png';
}
moveDown() {
if (this.alive) {
this.y += 15;
}
}
moveAlien() {
if (this.raw == 1) {
if (firstRawLeftBourd) {
if (this.x + this.width < canvas.width) {
this.x += 5;
} else {
this.x -= 5;
firstRawLeftBourd = false;
firstRawRightBourd = true;
if (allSeconRawDead()) allAliensDown();
}
} else {
if (this.x > 4) {
this.x -= 5;
} else {
this.x += 5;
firstRawLeftBourd = true;
firstRawRightBourd = false;
if (allSeconRawDead()) allAliensDown();
}
}
} else if (this.raw == 2) {
if (secondRawLeftBourd) {
if (this.x + this.width < canvas.width) {
this.x += 5;
} else {
this.x -= 5;
secondRawLeftBourd = false;
secondRawRightBourd = true;
allAliensDown();
}
} else {
if (this.x > 4) {
this.x -= 5;
} else {
this.x += 5;
secondRawLeftBourd = true;
secondRawRightBourd = false;
allAliensDown()
}
}
}
}
draw(move) {
if (this.alive) {
if (move) this.moveAlien();
ctx.drawImage(this.image, this.x, this.y, this.width, this.height);
}
}
kill() {
this.alive = false;
}
}
class Bulet {
constructor(x, y) {
this.width = 5;
this.height = 15;
this.x = x;
this.y = y - this.height;
this.active = false;
}
moveUp() {
this.y = this.y - 7;
if (this.y <= 0) {
this.kill();
}
}
checkIfkill() {
var endXposition = this.x + this.width;
for (var i = 0; i < numberOfAliensInRaw; i++) {
if (this.active && alienList1[i].alive && (this.y >= alienList1[i].y && this.y <= alienList1[i].y + alienList1[i].height) &&
((this.x >= alienList1[i].x && this.x <= alienList1[i].x + alienList1[i].width) ||
(endXposition >= alienList1[i].x && this.x <= alienList1[i].x + alienList1[i].width))) {
alienList1[i].kill();
this.kill();
break;
}
if (this.active && alienList2[i].alive && (this.y >= alienList2[i].y && this.y <= alienList2[i].y + alienList2[i].height) &&
((this.x >= alienList2[i].x && this.x <= alienList2[i].x + alienList2[i].width) ||
(endXposition >= alienList2[i].x && this.x <= alienList2[i].x + alienList2[i].width))) {
alienList2[i].kill();
this.kill();
break;
}
}
}
draw() {
if (this.active) {
this.checkIfkill();
this.moveUp();
this.checkIfkill();
ctx.fillStyle = "white";
ctx.fillRect(this.x, this.y, this.width, this.height);
}
}
start() {
this.active = true;
}
kill() {
console.log("bullet is killed");
this.active = false;
}
}
class Player {
constructor() {
this.width = 65;
this.height = 75;
this.x = canvas.width / 2 - this.width / 2;
this.y = canvas.height - this.height;
this.alive = true;
this.image = new Image();
this.image.src = './player.png';
}
moverigth() {
if (this.x + this.width < canvas.width) {
this.x += 5;
}
}
moveleft() {
if (this.x > 4) {
this.x -= 5;
}
}
draw() {
ctx.drawImage(this.image, this.x, this.y, this.width, this.height);
}
}
var player = new Player();
var moveLeft = false;
var moveRight = false;
var shooting = null;
initialiseAliens();
function initialiseAliens() {
for (var i = 0; i < numberOfAliensInRaw; i++) {
alienList1[i] = new Alien(i * canvas.width / 10, 100, 1);
alienList2[i] = new Alien(i * canvas.width / 10, 175, 2);
}
}
function shoot() {
if (shooting === null || !shooting.active) {
shooting = new Bulet(player.x + player.width / 2, player.y);
shooting.start();
}
}
function startGame() {
$(window).keydown(function(e) {
var keyCode = e.keyCode;
if (keyCode == 39) {
moveRight = true;
} else if (keyCode == 37) {
moveLeft = true;
} else if (keyCode == 32) {
shoot();
};
});
$(window).keyup(function(e) {
var keyCode = e.keyCode;
if (keyCode == 39) {
moveRight = false;
} else if (keyCode == 37) {
moveLeft = false;
};
});
if (!(animationTimeout === null)) clearTimeout(animationTimeout);
animate();
}
function drawShoots() {
if (!(shooting === null)) {
shooting.draw();
}
}
function drawAliens(move) {
if (firstRawLeftBourd) {
for (var i = numberOfAliensInRaw - 1; i >= 0; i--) {
alienList1[i].draw(move);
}
} else {
for (var i = 0; i < numberOfAliensInRaw; i++) {
alienList1[i].draw(move);
}
}
if (secondRawLeftBourd) {
for (var i = numberOfAliensInRaw - 1; i >= 0; i--) {
alienList2[i].draw(move);
}
} else {
for (var i = 0; i < numberOfAliensInRaw; i++) {
alienList2[i].draw(move);
}
}
}
function checkIfWin() {
for (var i = 0; i < numberOfAliensInRaw; i++) {
if (alienList1[i].alive || alienList2[i].alive) { return false; }
}
return true;
}
function checkIfLoose() {
for (var i = 0; i < numberOfAliensInRaw; i++) {
if (alienList1[i].alive && ((alienList1[i].y + alienList1[i].height >= canvas.height - 5) ||
(player.y >= alienList1[i].y && player.y <= alienList1[i].y + alienList1[i].height &&
alienList1[i].x >= player.x && alienList1[i].x <= player.x + player.height)))
return true;
else if (alienList2[i].alive && ((alienList2[i].y + alienList2[i].height >= canvas.height - 5) ||
(player.y >= alienList2[i].y && player.y <= alienList2[i].y + alienList2[i].height &&
alienList2[i].x >= player.x && alienList2[i].x <= player.x + player.height)))
return true;
}
return false;
}
function restart() {
firstRawRightBourd = false;
firstRawLeftBourd = true;
secondRawRightBourd = false;
secondRawLeftBourd = true;
alienList1 = [];
alienList2 = [];
player = new Player();
moveLeft = false;
moveRight = false;
shooting = null;
initialiseAliens();
playButton.innerHTML = "Restart";
}
function animate() {
ctx.drawImage(backImg, 0, 0, canvas.width, canvas.height);
ctx.font = "45px Comic Sans MS";
ctx.fillStyle = "red";
ctx.textAlign = "center";
ctx.fillText("Space Invaders", canvas.width / 2 - 15, 45);
if (moveRight) {
player.moverigth();
};
if (moveLeft) {
player.moveleft();
};
player.draw();
drawAliens(true);
drawShoots();
if (checkIfWin()) {
window.alert("Congratulations! You have won!");
restart();
} else if (checkIfLoose()) {
window.alert("Sorry! You have lost");
restart();
} else {
animationTimeout = setTimeout(animate, 25);
}
}
function initialAnimation() {
ctx.drawImage(backImg, 0, 0, canvas.width, canvas.height);
ctx.font = "45px Comic Sans MS";
ctx.fillStyle = "red";
ctx.textAlign = "center";
ctx.fillText("Space Invaders", canvas.width / 2 - 15, 45);
player.draw();
drawAliens(false);
drawShoots();
animationTimeout = setTimeout(initialAnimation, 25);
}
initialAnimation();
}
)