-
Notifications
You must be signed in to change notification settings - Fork 0
/
Space_invaders.html
744 lines (568 loc) · 18.3 KB
/
Space_invaders.html
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
<!DOCTYPE html>
<html>
<head>
<title>Space Invaders</title>
<style>
canvas{
border: solid;
background-color: black;
display: block;
margin-left: auto;
margin-right: auto;
}
</style>
<!-- Import external scripts -->
<script src="arrays+.js"></script>
<script src="vectors.js"></script>
<script src="objects+.js"></script>
<script src="pauseableTimeEvents.js"></script>
<script src="animatedSprite.js"></script>
<!-- -->
</head>
<body onload="" bgColor="grey">
<!-- loading area -->
<img src="spriteSheet.png" id="img_spriteSheet" hidden="true"/>
<!-- -->
<canvas id='canvas'>
This Browser Does Not Support Canvas.
</canvas>
</body>
<script>
/************************ TO DO ******************************\
*
*
*
*
\*************************************************************/
/* initialization of variables */
// stuff for drawing
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.fillStyle = "white";
canvas.height = 560;//window.innerHeight - 30;
canvas.width = 1250;//window.innerWidth - 30;
var frameRate = 32;
// set up interval for each tick
var t = null;
// set up timeouts/intervals
var i_motherShip = null;
var t_FireAtPlayer = null;
/* for images */
var sprites = {
alien1: null,
alien2: null,
alien4: null,
motherShip: null,
boss3: null,
bullet: null,
explosion: null,
}
var img_spriteSheet = document.getElementById('img_spriteSheet');
img_spriteSheet.onload = function(){
sprites.alien1 = new AnimatedSprite({image: this, frameCount: 2, fps: 2, sourceY: 0, frameWidth: 10, frameHeight: 10});
sprites.alien2 = new AnimatedSprite({image: this, frameCount: 2, fps: 2, sourceY: 0, frameWidth: 10, frameHeight: 10});
sprites.alien4 = new AnimatedSprite({image: this, frameCount: 2, fps: 2, sourceY: 0, frameWidth: 10, frameHeight: 10});
sprites.motherShip = new AnimatedSprite({image: this, frameCount: 6, fps: 6, sourceY: 10, frameWidth: 30, frameHeight: 15});
sprites.boss3 = new AnimatedSprite({image: this, frameCount: 3, fps: 6, sourceY: 25, frameWidth: 90, frameHeight: 60});
}
/* */
// the levels
var LEVEL = 1;
var enemyLevelClusters = [// level 1
[[1,1,1,1,1,1,1,1],
[1,0,0,1,1,0,0,1],
[1,1,1,1,1,1,1,1]],
// level 2
[[0,0,0,2,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,1,1,1,1,0,0]],
// level 3, boss
[[3]],
// level 4
[[2],
[0,0],
[1,1]]
];
// other game variables
var SCORE = 0;
var LIVES = 5;
// Pause Stuff
var Paused = false;
var canTogglePause = true;
var Playing = false;
// fireing stuff
var canFire = true;
// key detection
var Keys = {left: 37, up: 38, right: 39, down: 40, p: 80, w: 87};
var PressedKeys = {left: false, up: false, right: false};
document.onkeydown = function(e){
var code = e.keyCode || e.which;
if (Playing){
switch (code){
case Keys.up:
PressedKeys.up = true;
break;
case Keys.left:
PressedKeys.left = true;
break;
case Keys.right:
PressedKeys.right = true;
break;
case Keys.p:
TogglePaused();
break;
case Keys.w:
Enemies = [];
break;
default:
//alert(code);
break;
}
} else {
if (code == Keys.p){
Playing = true;
LEVEL = 1;
resetAll();
startLevel(LEVEL);
}
}
}
document.onkeyup = function(e){
var code = e.keyCode || e.which;
switch (code){
case Keys.up:
PressedKeys.up = false;
break;
case Keys.left:
PressedKeys.left = false;
break;
case Keys.right:
PressedKeys.right = false;
break;
case Keys.p:
canTogglePause = true;
break;
default:
break;
}
}
/* */
/* Boss Class */
var boss = null;
function Boss(health){
// make position and velocity
this.pos = new Vector2d(50,50);
this.vel = new Vector2d();
// set width and height
this.width = 150;
this.height = 90;
// set Health, it will take 20 hits unless specified in parameters
this.health = health!=null? health : 20;
// update for each tick
this.update = function(){
// update position based on velocity
this.pos._add(this.vel);
// draw it
this.drawMe();
}
// draws it
this.drawMe = function(){
ctx.save();
//ctx.fillStyle = "white";
//ctx.fillRect(this.pos.x, this.pos.y, this.width, this.height);
sprites["boss"+LEVEL].drawMe(this.pos.x, this.pos.y, this.width, this.height);
ctx.restore();
}
boss = this;
}
/* */
/* MotherShip Class */
var daMotherShip = null;
function MotherShip() {
// set interval if null
if (i_motherShip== null) i_motherShip = new PauseableInterval("new MotherShip()", 10*1000);
// set up position property
this.pos = new Vector2d(0,25);
// set up velocity
this.vel = new Vector2d(3,0)
// default size
this.width = 160;
this.height = 80;
this.pos.x = -this.width;
this.vel.x = 5;
// function to draw this enemy
this.drawMe = function(){
ctx.save();
//ctx.fillStyle="white";
//ctx.fillRect(this.pos.x, this.pos.y, this.width, this.height)
sprites.motherShip.drawMe(this.pos.x, this.pos.y, this.width, this.height);
ctx.restore();
};
// update per tick
this.update = function(){
// kill when off screen
if (this.pos.x>canvas.width + this.width){
daMotherShip = null;
}
this.pos._add(this.vel)
this.drawMe();
}
// add this enemy to the array containing all enemies
daMotherShip = this;
}
/**/
/* Enemy Class */
var Enemies = [];
var EnemyOffsetRef = 0;
function Enemy(x, y) {
this.id = Enemies.length;
var str = "alien"+LEVEL+"";
sprites[str].restartAnimation();
// set up position property
this.pos = new Vector2d();
// uses supplied x if not null, otherwise determined by id
this._x = x!=null ? x : this.id % 8;
this.pos.x = 25 + this._x * 60;
// uses supplied y if not null, otherwise determined by id
this._y = y!=null ? y : Math.floor(this.id / 8);
this.pos.y = 75 + this._y * 50;
// set up velocity
this.vel = new Vector2d(3,0)
// default size
this.width = 30;
this.height = 30;
// function to draw this enemy
this.drawMe = function(){
ctx.save();
//ctx.fillStyle="white";
//ctx.fillRect(this.pos.x, this.pos.y, this.width, this.height)
sprites["alien"+LEVEL].drawMe(this.pos.x, this.pos.y, this.width, this.height);
ctx.restore();
};
// update per tick
this.update = function(){
// bounce off imaginary walls
if (this.pos.x - this._x * 60 + 25 >= 780 || this.pos.x - this._x * 60 <= 24){
this.vel.x = this.vel.x * -1;
}
this.pos._add(this.vel)
this.drawMe();
}
this.fire = function(){
// make a bullet with a position of exactly the middle of the enemy, and a velocity of 5 going down
new Bullet(new Vector2d(this.pos.x + this.width/2 - 10/2, this.pos.y+this.height-20), new Vector2d(0,10), true);
t_FireAtPlayer = null;
}
// add this enemy to the array containing all enemies
Enemies.push(this);
}
/**/
/* Bullet Class */
var Bullets = [];
var BulletIdRef = 0;
function Bullet(pos, vel, isBomb) {
this.id = BulletIdRef++;
// set position and velocity
this.pos = pos || new Vector2d(0,0);
this.vel = vel || new Vector2d(0,0);
this.height = 20;
this.width = 10;
// update for each tick
this.update = function (){
// move based on vel
this.pos._add(this.vel);
// remove from Bullets and stop function if offscreen
if (this.pos.y + this.height < 0 || this.pos.y >= canvas.height){
Bullets.splice(Bullets.indexOf(this), 1);
return;
}
// make collision logic here, going in reverse ensures that all enemies are checked
if (!isBomb){
for (var i = Enemies.length - 1; i >= 0; i--){
if (this.intersectsWith(Enemies[i])){
Enemies.splice(i,1);
Bullets.splice(Bullets.indexOf(this),1);
SCORE++;
return;
}
}
// if there is a mother ship
if (daMotherShip != null){
if (this.intersectsWith(daMotherShip)){
daMotherShip = null;
Bullets.splice(Bullets.indexOf(this),1);
SCORE+= 10;
return;
}
}
// if fighting a boss
if (boss != null){
// if you hit the boss
if (this.intersectsWith(boss)){
// take away boss health
boss.health--;
if (boss.health == 0){
boss = null;
SCORE += 50;
}
Bullets.splice(Bullets.indexOf(this),1);
return;
}
}
} else
// check if colliding with the player if it is a bomb
{
if (this.intersectsWith(player)){
console.log('you died');
Bullets.splice(Bullets.indexOf(this),1);
LIVES--;
}
}
// draw it
this.drawMe();
};
// function to draw this bullet
this.drawMe = function(){
ctx.save();
ctx.fillStyle="white";
ctx.fillRect(this.pos.x, this.pos.y, this.width, this.height)
ctx.restore();
};
Bullets.push(this);
}
/* */
/* Player Class */
var player = new Player();
function Player(){
this.reset = function(){
this.width = 30;
this.pos = new Vector2d((canvas.width / 2) - (this.width / 2), canvas.height - 5 - this.width);
this.vel = new Vector2d();
this.maxSpeed = 10;
}
this.width = 30;
this.height = 30;
this.pos = new Vector2d((canvas.width / 2) - (this.width / 2), canvas.height - 5 - this.width);
this.vel = new Vector2d();
this.maxSpeed = 10;
this.drawMe = function(){
ctx.save();
ctx.fillStyle="white";
ctx.fillRect(this.pos.x, this.pos.y, this.width, this.height)
ctx.restore();
}
this.update = function(){
/* update velocity */
if (PressedKeys.left){
this.vel.x -= (this.vel.x > -this.maxSpeed)*(this.maxSpeed/4);
}
if (PressedKeys.right){
this.vel.x += (this.vel.x < this.maxSpeed)*(this.maxSpeed/4);
}
/* */
// fire
if (PressedKeys.up && canFire){
// fire
this.fire();
// no spamming the trigger
canFire = false;
window.setTimeout("canFire = true;",500);
// make it automatic
PressedKeys.up = false;
}
// update position
this.pos._add(this.vel);
// make sure it doesn't go out of bounds
if (this.pos.x > canvas.width - this.width || this.pos.x < 0){
this.pos.x = (canvas.width - this.width) * (this.pos.x > canvas.width - this.width);
this.vel.x = 0;
}
// slow it down
this.vel.x = this.vel.x * 0.9;
// draw it
this.drawMe();
}
this.fire = function(){
// make a bullet with a position of exactly the middle of the Player, and a velocity of 10 going up
new Bullet(new Vector2d(this.pos.x + this.width/2 - 10/2, this.pos.y), new Vector2d(0,-10));
}
}
/* */
/*******************\
|*******************|
|*******************|
|*******************|
|*******************|
\*******************/
// the actual tick function
function tick(){
// don't tick if paused
if (Paused) return;
// clear the screen
ctx.clearRect(0,0,canvas.width, canvas.height);
// set when and which enemy will fire next
if (t_FireAtPlayer == null){
// picks a "random" enemy
var daEnemy = Math.floor(Math.random() * Enemies.length);
// picks time between 5 and 10 seconds
var interval = 2000 + Math.floor(Math.random() * 2 * 1000);
// set the timeout
t_FireAtPlayer = new PauseableTimeout("EnemyFiringHandler("+daEnemy+");", interval);
}
// (find way to update all bullets and enemies in same loop)
// update all enemies
for (var i = 0; i < Enemies.length; i++){
Enemies[i].update();
}
// if there is a mothership
if (daMotherShip != null){
daMotherShip.update();
}
// leave if Not playing
if (!Playing) {
player.drawMe();
// make main screen
ctx.save();
ctx.fillStyle = "rgba(0,0,0,.3)";
ctx.fillRect(0,0,canvas.width, canvas.height);
ctx.fillStyle = "rgba(150,150,150,.8)";
ctx.fillRect(canvas.width / 2 - 320, canvas.height / 2 - 80, 560, 160);
ctx.fillStyle = "White";
ctx.font = "60pt Calibri"
ctx.fillText("Press 'p' To Play",canvas.width/2 - 300, canvas.height/2 + 20);
ctx.restore();
return;
}
// if there is a boss
if (boss != null){
boss.update();
}
// update all Bullets, going in reverse insures that all bullets are updated
// a bullet would be skipped if the one before it was spliced, unless caught and coded for, reverse is just easier
for (var i = Bullets.length - 1; i >= 0; i--){
Bullets[i].update();
}
// update the Player
player.update();
// update stuff written on screen
ctx.save();
ctx.fillStyle = "white";
ctx.font = "16pt Calibri";
ctx.fillText("Score: "+SCORE+" Lives: "+LIVES,0,16);
ctx.restore();
// check if win
if (Enemies.length==0 && boss==null){
resetAll();
player.drawMe();
alert("!!! YOU WIN !!!");
LEVEL++;
if (LEVEL <= enemyLevelClusters.length){
startLevel(LEVEL);
} else {
alert("!!!! GAME OVER !!!!");
SetHomeScreen();
}
}
}
/*******************\
|*******************|
|*******************|
|*******************|
|*******************|
\*******************/
// hey there
function startLevel(level){
resetAll();
var levelGrid = enemyLevelClusters[level - 1];
// loop through each position in grid
for (var i = 0; i < levelGrid.length; i++){
for (var ii = 0; ii < levelGrid[i].length; ii++){
// if the position is on, make an enemy their
if (levelGrid[i][ii] == 3) {new Boss();}
else if (levelGrid[i][ii] == 2) {new MotherShip();}
else if (levelGrid[i][ii] == 1) {new Enemy(ii,i);}
}
}
t = window.setInterval(tick, 1000/32);
}
function resetAll(){
// move Player back
player.reset();
canFire = true;
// empty arrays
Enemies = [];
Bullets = [];
// clear the screen
ctx.clearRect(0,0,canvas.width, canvas.height);
// turn off all intervals/timeouts
window.clearInterval(t);
if (t_FireAtPlayer!=null) t_FireAtPlayer.clearTimeout();
t_FireAtPlayer = null;
if (i_motherShip!=null) i_motherShip.clearInterval();
i_motherShip = null;
canMotherShip = false;
// nullify enemies
daMotherShip = null;
boss = null;
}
function TogglePaused(){
// leave if you can't toggle
if (!canTogglePause) {
return;
}
// toggle Paused
Paused = Paused ? false : true;
// make it so you can't toggle (to be reset upon onkeyup)
canTogglePause = false;
// if it is now paused
if (Paused){
// pause pauseable timerouts and intervals
if (i_motherShip!=null) i_motherShip.pause();
if (t_FireAtPlayer!=null) t_FireAtPlayer.pause();
// make pause menu
ctx.save();
ctx.fillStyle = "rgba(0,0,0,.3)";
ctx.fillRect(0,0,canvas.width, canvas.height);
ctx.fillStyle = "rgba(150,150,150,.8)";
ctx.fillRect(canvas.width / 2 - 160, canvas.height / 2 - 80, 320, 160);
ctx.fillStyle = "White";
ctx.font = "60pt Calibri"
ctx.fillText("Paused",canvas.width/2 - 120, canvas.height/2 + 20);
ctx.restore();
} else { // game restarted
// restart motherShips, but with appropriate time left
if (i_motherShip!=null) i_motherShip.resume();
if (t_FireAtPlayer!=null) t_FireAtPlayer.resume();
// clear screen and redraw the player
ctx.clearRect(0,0,canvas.width, canvas.height);
player.drawMe();
}
}
// handler for enemies firing back, necesarry because the timer will never null out other wise
function EnemyFiringHandler(enemyNumber){
// if the enemy still exists
if (Enemies[enemyNumber]){
Enemies[enemyNumber].fire();
}
// otherwise, if there are even still any enemies
else if (Enemies.length){
enemyNumber = Math.floor(Math.random() * Enemies.length);
Enemies[enemyNumber].fire();
}
// if there aren't any enemies I just won't reset it here
}
function SetHomeScreen(){
Playing = false;
resetAll();
canFire = false;
player.drawMe();
t = window.setInterval(tick, 1000/frameRate);
startLevel(LEVEL = 2);
SCORE = 0;
LIVES = 5;
}
/**** START THE GAME ****/
setTimeout(SetHomeScreen, 100);
/**** ****/
</script>
</html>