-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
332 lines (329 loc) · 7.95 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
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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Space Invaders</title>
<script src="js/helpers.js"></script>
<style>
canvas {
/*background-image: url(pEI6Uz.jpg);*/
background-color: #000;
display: block;
position: absolute;
margin: auto;
top: 89px;
bottom: 0;
left: 0;
right: 0;
background-color: black;
-webkit-animation-name: example; /* Safari 4.0 - 8.0 */
-webkit-animation-duration: 24s; /* Safari 4.0 - 8.0 */
animation-name: example;
animation-duration: 24s;
}
@-webkit-keyframes example {
from {background-color: black;}
to {background-color: dodgerblue;}
}
/* Standard syntax */
@keyframes example {
from {background-color: black;}
to {background-color: dodgerblue;}
}
h3{
color: cornflowerblue;
font-size: 35px;
}
/*
body{
background-image: url(bgimage.jpg);
background-color: #cccccc;
}
*/
</style>
</head>
<body bgcolor="black">
<h3><center>Space Invaders</center></h3>
<script>
alert("Space Invaders");
var
display,
input,
frames,
spFrame,
lvFrame,
alSprite,
taSprite,
ciSprite,
aliens,
dir,
tank,
bullets,
cities;
function main() {
// create game canvas and inputhandeler
display = new Screen(504, 600);
input = new InputHandeler();
// create all sprites fram assets image
var img = new Image();
img.addEventListener("load", function() {
alSprite = [
[new Sprite(this, 0, 0, 22, 16), new Sprite(this, 0, 16, 22, 16)],
[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);
// initate and run the game
init();
run();
});
img.src = "res/invaders.png";
};
/**
* Initate game objects
*/
function init() {
// set start settings
frames = 0;
spFrame = 0;
lvFrame = 60;
dir = 1;
// create the tank object
tank = {
sprite: taSprite,
x: (display.width - taSprite.w) / 2,
y: display.height - (30 + taSprite.h)
};
// initatie bullet array
bullets = [];
// create the cities object (and canvas)
cities = {
canvas: null,
ctx: null,
y: tank.y - (30 + ciSprite.h),
h: ciSprite.h,
/**
* Create canvas and game graphic context
*/
init: function() {
// create canvas and grab 2d context
this.canvas = document.createElement("canvas");
this.canvas.width = display.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);
}
},
/**
* Create damage effect on city-canvas
*
* @param {number} x x-coordinate
* @param {number} y y-coordinate
*/
generateDamage: function(x, y) {
// round x, y position
x = Math.floor(x/2) * 2;
y = Math.floor(y/2) * 2;
// draw dagame effect to canva
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);
},
/**
* Check if pixel at (x, y) is opaque
*
* @param {number} x x-coordinate
* @param {number} y y-coordinate
* @return {bool} boolean value if pixel opaque
*/
hits: function(x, y) {
// transform y value to local coordinate system
y -= this.y;
// get imagedata and check if opaque
var data = this.ctx.getImageData(x, y, 1, 1);
if (data.data[3] !== 0) {
this.generateDamage(x, y);
return true;
}
return false;
}
};
cities.init(); // initiate the cities
// create and populate alien array
aliens = [];
var rows = [1, 0, 0, 2, 2];
for (var i = 0, len = rows.length; i < len; i++) {
for (var j = 0; j < 10; j++) {
var a = rows[i];
// create right offseted alien and push to alien
// array
aliens.push({
sprite: alSprite[a],
x: 30 + j*30 + [0, 4, 0][a],
y: 30 + i*30,
w: alSprite[a][0].w,
h: alSprite[a][0].h
});
}
}
};
/**
* Wrapper around the game loop function, updates and renders
* the game
*/
function run() {
var loop = function() {
update();
render();
window.requestAnimationFrame(loop, display.canvas);
};
window.requestAnimationFrame(loop, display.canvas);
};
/**
* Update the game logic
*/
function update() {
// update the frame count
frames++;
// update tank position depending on pressed keys
if (input.isDown(37)) { // Left
tank.x -= 4;
}
if (input.isDown(39)) { // Right
tank.x += 4;
}
// keep the tank sprite inside of the canvas
tank.x = Math.max(Math.min(tank.x, display.width - (30 + taSprite.w)), 30);
// append new bullet to the bullet array if spacebar is
// pressed
if (input.isPressed(32)) { // Space
bullets.push(new Bullet(tank.x + 10, tank.y, -8, 2, 6, "#fff"));
}
// update all bullets position and checks
for (var i = 0, len = bullets.length; i < len; i++) {
var b = bullets[i];
b.update();
// remove bullets outside of the canvas
if (b.y + b.height < 0 || b.y > display.height) {
bullets.splice(i, 1);
i--;
len--;
continue;
}
// check if bullet hits any city
var h2 = b.height * 0.5; // half hight is used for
// simplicity
if (cities.y < b.y+h2 && b.y+h2 < cities.y + cities.h) {
if (cities.hits(b.x, b.y+h2)) {
bullets.splice(i, 1);
i--;
len--;
continue;
}
}
// check if bullet hit any aliens
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 = 20;
break;
}
case 5: {
this.lvFrame = 15;
break;
}
case 1: {
this.lvFrame = 6;
break;
}
}
}
}
}
// makes the alien shoot in an random fashion
if (Math.random() < 0.03 && aliens.length > 0) {
var a = aliens[Math.round(Math.random() * (aliens.length - 1))];
// iterate through aliens and check collision to make
// sure only shoot from front line
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;
}
}
// create and append new bullet
bullets.push(new Bullet(a.x + a.w*0.5, a.y + a.h, 4, 2, 4, "#fff"));
}
// update the aliens at the current movement frequence
if (frames % lvFrame === 0) {
spFrame = (spFrame + 1) % 2;
var _max = 0, _min = display.width;
// iterate through aliens and update postition
for (var i = 0, len = aliens.length; i < len; i++) {
var a = aliens[i];
a.x += 30 * dir;
// find min/max values of all aliens for direction
// change test
_max = Math.max(_max, a.x + a.w);
_min = Math.min(_min, a.x);
}
// check if aliens should move down and change direction
if (_max > display.width - 30 || _min < 30) {
// mirror direction and update position
dir *= -1;
for (var i = 0, len = aliens.length; i < len; i++) {
aliens[i].x += 30 * dir;
aliens[i].y += 30;
}
}
}
};
/**
* Render the game state to the canvas
*/
function render() {
display.clear(); // clear the game canvas
// draw all aliens
for (var i = 0, len = aliens.length; i < len; i++) {
var a = aliens[i];
display.drawSprite(a.sprite[spFrame], a.x, a.y);
}
// save contetx and draw bullet then restore
display.ctx.save();
for (var i = 0, len = bullets.length; i < len; i++) {
display.drawBullet(bullets[i]);
}
display.ctx.restore();
// draw the city graphics to the canvas
display.ctx.drawImage(cities.canvas, 0, cities.y);
// draw the tank sprite
display.drawSprite(tank.sprite, tank.x, tank.y);
};
// start and run the game
main();
</script>
</body>
</html>