-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
270 lines (223 loc) · 7.99 KB
/
index.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
<!DOCTYPE html>
<html lang='br'>
<head> <!-ctrl + alt + C open browser-->
<meta charset = "UTF-8">
<title>Space Invaders</title>
<script src="js/helpers.js">
</script>
<style>
/* background of the game */
canvas{
background-color: #000;
display: block;
position: absolute;
margin: auto;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
</style>
</head>
<body>
</body>
<script>
/* Variables enable in the game */
var screen, input, frames, spFrame, lvFrame; // display
var alSprite, taSprite, ciSprite; // Sprites three category
var aliens, dir, tank, bullets, cities; // dir is and bullets is projectiles
function main() {
screen = new Screen(504, 600);
input = new InputHandeler();
var img = new Image(); // this API Image() constructor is equivalent to document.createElement('img'). Variable local.
img.addEventListener("load", function(){ // this function split the image getting the sprites
alSprite = [
[new Sprite(this, 0 , 0, 22, 16) , new Sprite(this,0 , 16, 22,16)], // each square is 8 px
[new Sprite(this, 22, 0, 16, 16), new Sprite(this,22, 16, 16, 16)],
[new Sprite(this, 38, 0, 24, 16), new Sprite(this, 38, 16, 24, 16)]
];
taSprite = new Sprite(this, 62, 0 , 22, 16);
ciSprite = new Sprite(this, 84, 8, 36, 24);
init();
run();
});
img.src = "image/invaders.png";
};
function init(){ // queuing the aliens
frames = 0; // inicialization
spFrame = 0;
lvFrame = 60;
dir = 1;
tank = {
sprite: taSprite,
x: (screen.width - taSprite.w)/2,
y: screen.height - (30 + taSprite.h)
}
bullets = [];
cities = {
ctx: null,
canvas: null,
y: tank.y -( 90 - ciSprite.h), // position cities relative tank
h:ciSprite.h,
init: function(){
this.canvas = document.createElement("canvas");
this.canvas.width = screen.width;
this.canvas.height = this.h;
this.ctx = this.canvas.getContext("2d");
for( var i = 0; i< 4; i++){
this.ctx.drawImage( ciSprite.img, ciSprite.x, ciSprite.y, ciSprite.w, ciSprite.h, 68 + 111*i, 0 , ciSprite.w, ciSprite.h );
} //provades the context of 2d renderization
},
generateDamage:function(x,y){
x = Math.floor(x/2)*2;
y = Math.floor(y/2)*2;
this.ctx.clearRect(x-2, y-2, 4, 4);
this.ctx.clearRect(x+2, y-4, 2, 4);
this.ctx.clearRect(x+4, y, 2, 2);
this.ctx.clearRect(x+2, y+2, 2, 2);
this.ctx.clearRect(x-4, y+2, 2, 2);
this.ctx.clearRect(x-6, y, 2, 2);
this.ctx.clearRect(x-4, y-4, 2, 2);
this.ctx.clearRect(x-2, y-6, 2, 2);
},
hits: function(x, y){
y -= this.y;
var data = this.ctx.getImageData(x, y, 1, 1);
if(data.data[3] !== 0){
this.generateDamage(x,y);
return true;
}
return false;
}
}
cities.init();
aliens = [];
var rows = [1, 0, 0, 2, 2]; // defination tipy of alien
for(var i = 0, len = rows.length; i<len; i++){
for(var j=0; j<10; j++){
var a = rows[i];
aliens.push({
sprite: alSprite[a],
x:30 + j*30 + [0,5,0][a], // I don't understand [0,5,0]
y:30 + i*30, // vertical space
w:alSprite[a][0].w, // I don't understand this pass don't change anything.
h:alSprite[a][0].h // basec if you del line 72 and 73 the program run normaly.
});
}
}
};
function run(){
var loop = function() {
update();
render();
window.requestAnimationFrame(loop, screen.canvas);
};
window.requestAnimationFrame(loop, screen.canvas);
};
function update(){
if(input.isDown(37)){ // left
tank.x -= 4;
};
if(input.isDown(39)){ // right
tank.x += 4;
};
tank.x = Math.max(Math.min(tank.x, screen.width - (30 + taSprite.w)),30); // restrict left and right screen for the tank.
if(input.isDown(32)){ //space
bullets.push(new Bullet(tank.x+10, tank.y, -8, 2, 6, "#fff")) // this parameters at of constructor modify position of bullet.
}
for(var i= 0, len = bullets.length; i<len; i++){
var b = bullets[i];
b.update();
// This algorithm avoid the bullet perpetuate in infity ocupaty space in memory
if(b.y + b.height < 0 || b.y > screen.height){ // Limit distance of the bullet
bullets.splice(i,1); // remove 1 element present in index i of the array bullet.
i--;
len--;
continue;
}
var h2 = b.heigth * 0.5;
if( cities.y < b.y+h2 && b.y+h2 < cities.y + cities.h){
if( cities.hits(b.x, b.y+h2)){
bullets.slice(i,1);
i--;
len--;
continue;
}
}
// Problem in this algoritm
for (var j = 0, len2 = aliens.length; j < len2; j++) {
var a = aliens[j];
if (AABBIntersect(b.x, b.y, b.width, b.height, a.x, a.y, a.w, a.h)) {
aliens.splice(j, 1);
j--;
len2--;
bullets.splice(i, 1);
i--;
len--;
// increase the movement frequence of the aliens
// when there are less of them
switch (len2) {
case 30:
this.lvFrame = 40;
break;
case 10:
this.lvFrame = 30;
break;
case 5:
this.lvFrame = 15;
break;
case 1:
this.lvFrame = 6;
break;
}
}
}
}
// Aliens Bullets
if(Math.random()<0.03 && aliens.length > 0){
var a = aliens[Math.round(Math.random() * ( aliens.length - 1))];
for( var i = 0, len = aliens.length; i< len ; i++){
var b = aliens[i];
if(AABBIntersect(a.x, a.y, a.w, 100, b.x, b.y, b.w, b.h)){
a = b;
}
}
// creat and append new bullet
bullets.push( new Bullet(a.x + a.w*0.5, a.y+a.h, 4 , 2, 4, "#fff"));
}
frames++;
if(frames% lvFrame === 0){
spFrame = (spFrame + 1) % 2; // I don't understand this pass
var _max = 0, _min = screen.width ; // max position in the screen and min position;
for(var i= 0 , len = aliens.length; i<len; i++){
var a = aliens[i];
a.x += 30*dir;
_max = Math.max(_max, a.x + a.w ); // I can't interpret this pass.
_min = Math.min(_min,a.x);
}
if(_max > screen.width - 30 || _min < 30){ // When the aliens reach the right wall (less 30px) back and or when it comes to the beginning back
dir*= - 1 ;
for( var i = 0, len = aliens.length; i<len; i++){
aliens[i].x += 30*dir; //jumps controls for other lines space
aliens[i].y += 20; // vertical jumps
}
}
}
}
function render(){ // aliens is global variable
screen.clear();
for( var i=0, len = aliens.length; i<len; i++){
var a = aliens[i];
screen.drawSprite(a.sprite[spFrame],a.x,a.y); // a.sprite[0] is position of recharge and a.sprite[1] is position of attack
};
screen.ctx.save(); // ?
for(var i = 0, len = bullets.length; i<len; i++){
screen.drawBullet(bullets[i]);
};
screen.ctx.restore();
screen.ctx.drawImage(cities.canvas, 0, cities.y);
screen.drawSprite(tank.sprite, tank.x, tank.y);
};
main();
</script>
</html>