-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
322 lines (295 loc) · 7.96 KB
/
main.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
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
var Alien = function(aType,aLine,aCol){
this.type = aType;
this.points = 40-10*aType;
this.line = aLine;
this.column = aCol;
this.alive = true;
this.height = 20;
this.width = 28;
this.positionX = 100+this.width*this.column;
this.positionY = 100+30*this.line;
this.direction = 1;
this.state = 0;
this.changeState = function(){ //change the state (2 different images for each alien)
if(this.state == 0){
this.state = 20;
}else{
this.state = 0;
}
};
this.down = function(){ //down the alien after changing direction
this.positionY = this.positionY + 10;
};
this.move = function() { //set new position after moving and draw the alien
if(this.positionY >= Game.height-50){
Game.over();
}
this.positionX = this.positionX+ 5*Game.direction;
this.changeState();
if(this.alive){
this.draw();
}
};
this.checkCollision = function(){ //Check if the alien is killed by gun ray
if(Gun.ray.active == true && this.alive == true){
if((Gun.ray.positionX >= this.positionX && Gun.ray.positionX <= (this.positionX + this.width))
&& (Gun.ray.positionY >= this.positionY && Gun.ray.positionY <= (this.positionY + this.height))){
this.kill();
Gun.ray.destroy();
}
}
};
this.draw = function(){ //draw the alien to his new position
if(this.alive){ //draw the alien
canvas.drawImage(
pic,
this.width*(this.type-1),
this.state,
this.width,
this.height,
this.positionX,
this.positionY,
this.width,
this.height);
}else if(this.alive == null){ //draw the explosion
canvas.drawImage(
pic,
85,
20,
28,
20,
this.positionX,
this.positionY,
this.width,
this.height);
this.alive = false; //alien won't be displayed any more
}
};
this.kill = function() { //kill the alien
this.alive = null;
canvas.clearRect(this.positionX,this.positionY,this.width,this.height);
Game.refreshScore(this.points);
}
};
Gun = {
position: 220,
toleft:false,
toright:false,
init: function(){ //initialize the gun and his move
this.draw();
this.toLeft();
this.toRight();
setInterval("Gun.toLeft()",30);
setInterval("Gun.toRight()",30);
},
draw: function() { //draws the gun
canvas.drawImage(pic,85,0,28,20,this.position,470,28,20);
},
fire: function() { //shot
this.ray.create();
},
toLeft: function(){ //moves the gun to left
if(this.toleft){
if(this.position-5>0){
canvas.clearRect(0,472,Game.width,28);
this.position -= 5;
this.draw();
}
}
},
toRight: function(){ //moves the gun to right
if(this.toright){
if(this.position+30<Game.width){
canvas.clearRect(0,472,Game.width,28);
this.position += 5;
this.draw();
}
}
},
ray:{ //gun ray
positionX: 0,
positionY: 465,
length: 5,
speed:15,
animation: null,
active: false,
create: function(){ //created the ray if it does not exist
if(!this.active){
this.positionX = Gun.position+14;
this.active = true;
this.animation = setInterval("Gun.ray.animate()",this.speed);
}
},
animate: function(){ //animate the ray
this.positionY -= this.length;
if(this.positionY<=5) this.destroy();
else{
Game.drawAliens();
this.draw();
}
},
draw: function(){ //draw the ray and check collision with aliens
if(this.active){
canvas.beginPath();
canvas.strokeStyle='white';
canvas.lineWidth=2;
canvas.moveTo(this.positionX,this.positionY);
canvas.lineTo(this.positionX,this.positionY+this.length);
canvas.stroke();
for(i=0;i<5;i++){
for(j=0;j<11;j++){
Game.aliens[i][j].checkCollision();
}
}
}
},
destroy: function(){ //destroy the ray
this.positionY = 465;
this.active = false;
clearInterval(this.animation);
this.animation = null;
Game.drawAliens();
},
}
};
Game = {
types: [1,2,2,3,3], //define kinds of aliens
aliens: [[11],[11],[11],[11],[11]],
height: 0,
width: 0,
interval: 0,
intervalDefault: 1000,
direction: 1,
animation: null,
alives:1,
score:0,
level:1,
init: function(aWidth,aHeight) { //initialize default position and behaviour
for(i=0;i<5;i++){
for(j=0;j<11;j++){
this.aliens[i][j] = new Alien(this.types[i],i,j);
this.alives++;
this.aliens[i][j].draw();
}
}
this.width = aWidth;
this.height = aHeight;
this.play();
Gun.init();
this.refreshScore(0);
document.getElementById('level').innerHTML = this.level;
document.getElementById('inter').innerHTML = this.interval;
},
changeDirection: function(){ //change the direction (left or right)
if(this.direction == 1){
this.direction = -1;
}else{
this.direction = 1;
}
},
clearCanvas: function(){ //clear the canvas (but not the gun)
canvas.clearRect(0,0,this.width,this.height-28);
},
closeToLeft: function(){ //check if the aliens reach the left border
return (this.aliens[0][0].positionX - 10 < 0)?true:false;
},
closeToRight: function(){ //check if the aliens reach the right border
return (this.aliens[4][10].positionX + 35 > this.width)?true:false;
},
drawAliens: function(){ //draw the aliens
this.clearCanvas();
for(i=0;i<5;i++){
for(j=0;j<11;j++){
this.aliens[i][j].draw();
}
}
},
animate: function(){ //move the aliens
this.clearCanvas();
Gun.ray.draw();
this.checkAliens();
for(i=0;i<5;i++){
for(j=0;j<11;j++){
this.aliens[i][j].move();
}
}
if(this.closeToLeft() || this.closeToRight()){
this.changeDirection();
for(i=0;i<5;i++){
for(j=0;j<11;j++){
this.aliens[i][j].down();
}
}
this.increaseSpeed();
}
},
play: function(){ //play the game
this.interval = this.intervalDefault;
this.interval = this.interval-(this.level*20);
this.animation = setInterval("Game.animate()",this.interval);
},
increaseSpeed: function(newInterval){ //increase the speed
clearInterval(this.animation);
if(newInterval===undefined) this.interval = this.interval-10;
else this.interval = newInterval;
this.animation = setInterval("Game.animate()",this.interval);
document.getElementById('inter').innerHTML = this.interval;
},
onkeydown: function(ev){ //key down event
if(ev.keyCode == 37) Gun.toleft = true;
else if(ev.keyCode == 39) Gun.toright = true;
else if(ev.keyCode == 32) Gun.fire();
else return;
},
onkeyup: function(ev){ //key up event
if(ev.keyCode == 37) Gun.toleft=false;
else if(ev.keyCode == 39) Gun.toright=false;
else return;
},
over: function(){ //ends the game
clearInterval(this.animation);
canvas.clearRect(0,0,this.width,this.height);
canvas.font = "40pt Calibri,Geneva,Arial";
canvas.strokeStyle = "rgb(FF,0,0)";
canvas.fillStyle = "rgb(0,20,180)";
canvas.strokeText("Game Over", this.width/2-150, this.height/2-10);
},
checkAliens: function(){ //check number of aliens
if(this.alives==0) this.nextLevel();
else if(this.alives==1) this.increaseSpeed(150-(this.level*10));
else if(this.alives<=10) this.increaseSpeed(200-(this.level*10));
else if(this.alives<=10) this.increaseSpeed(300-(this.level*10));
else if(this.alives<=25) this.increaseSpeed(500-(this.level*10));
},
refreshScore: function(points){ //display the score
this.alives--;
this.score += points;
document.getElementById('score').innerHTML = this.score;
document.getElementById('alives').innerHTML = this.alives;
},
nextLevel: function(){
//resurect aliens
for(i=0;i<5;i++){
for(j=0;j<11;j++){
this.aliens[i][j].alive = true;
this.alives++;
}
}
clearInterval(this.animation);
this.level++;
document.getElementById('level').innerHTML = this.level;
this.play();
this.increaseSpeed(this.interval);
document.getElementById('inter').innerHTML = this.interval;
}
};
//define the global context of the game
var element = document.getElementById('aliensCanvas');
if (element.getContext) {
var canvas = element.getContext('2d');
var pic = new Image();
pic.src = 'sprite.png';
Game.init(530,500);
document.body.onkeydown = function(ev) { Game.onkeydown(ev); };
document.body.onkeyup = function(ev) { Game.onkeyup(ev); };
}