forked from adelinolobao/Space-Invaders-HTML-5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
458 lines (399 loc) · 9.29 KB
/
index.html
File metadata and controls
458 lines (399 loc) · 9.29 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
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
<!--
File index.html
implemented by Adelino Lobão
09/01/2012
-->
<html>
<head>
<title>Space Invaders</title>
<link rel="stylesheet" href="style/style_space.css" />
<link rel="stylesheet" href="style/normalize.css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>
<canvas id="content"></canvas>
<script type="text/javascript">
;$(function(){
const FRAME_RATE = 50;
var $window = $(window),
w = $window.width(),
h = $window.height(),
gameLoop,
intervalTime = 1000 / FRAME_RATE,
previousAction = 'left',
canvas = document.getElementById("content"),
context = canvas.getContext("2d"),
player = new Player(),
lasers = [],
enemies = [];
canvas.width = w;
canvas.height = h;
/**
* Class Enemy
* @param initPosX - initial position xx
* @param initPosY - initial position yy
*/
function Enemy(initPosX, initPosY) {
//image src
var imgSrc = 'script/images/enemy.png';
//width
this.width = 22;
//height
this.height = 16;
//enemy sprite
var sprite = new Sprite(3);
//set image and size
sprite.initImage(imgSrc, this.width, this.height);
//set initial position of the sprite
sprite.initPosition(initPosX, initPosY);
//initial action
var currentAction = 'left';
/**
* Draws the enemy sprite
*/
this.draw = function() {
sprite.draw();
}
/**
* Simulates one step of the enemy
* @param action - indicates the action of the enemy
*/
this.step = function() {
if(currentAction == 'left') {
sprite.moveLeft();
}else if(currentAction == 'right') {
sprite.moveRight();
}
}
/**
* Simulates one jump forward
*/
this.jump = function() {
if(currentAction == 'left') {
currentAction = 'right';
}else {
currentAction = 'left';
}
sprite.moveFront();
}
/**
* Verifies if the enemies needs to change of action
*/
this.checkStep = function() {
if(sprite.x <= 10 && currentAction == 'left') {
currentAction = 'right';
}else if(sprite.x >= (w - 10) && currentAction == 'right') {
currentAction = 'left';
}
return currentAction;
}
/**
* Return the position xx of the enemy
*/
this.getPositionX = function() {
return sprite.x;
}
/**
* Return the position yy of the enemy
*/
this.getPositionY = function() {
return sprite.y;
}
}
/**
* Class Sprite
* @param increment - is the increment added to the sprite
*/
function Sprite(increment) {
//increment
var increment = increment;
/**
* Initialize sprite image and size
* @param src - image source
* @param widht - image width
* @param height - image height
*/
this.initImage = function(src, width, height) {
//set image sprite
this.image = new Image();
//set image source
this.image.src = src;
//set image width
this.width = width;
//set image height
this.height = height;
}
/**
* Initialize the sprite position
* @param initPosX - initial position xx
* @param initPosY - initial position yy
*/
this.initPosition = function(initPosX, initPosY) {
this.x = initPosX;
this.y = initPosY;
}
/**
* Set sprite position
* @param posX - position xx
* @param posY - position yy
*/
this.setPosition = function(posX, posY) {
if((posX > 0) && (posX < w)) { //check the position boundaries
this.x = posX;
this.y = posY;
}
}
/**
* Draw sprite
*/
this.draw = function() {
//removes image size
var positionX = this.x - (this.width / 2);
var positionY = this.y - (this.height / 2);
//draw image
context.drawImage(this.image, positionX, positionY);
}
/**
* Move sprite to the left
*/
this.moveLeft = function() {
this.setPosition(this.x - increment, this.y);
}
/**
* Move sprite to the right
*/
this.moveRight = function() {
this.setPosition(this.x + increment, this.y);
}
/**
* Move sprite to the front
*/
this.moveFront = function() {
this.setPosition(this.x, this.y + (increment * 3));
}
}
/**
* Class Laser
* @param player - player object
*/
function Laser(player) {
//increment
const INCREMENT = 20;
//laser width
this.width = 5;
//laser height
this.height = 5;
//intial position xx
this.x = player.getPositionX() - 2;
//initial position yy
this.y = player.getPositionY() - 15;
/**
* Draw the laser
*/
this.draw = function() {
//context style
context.fillStyle = "white";
//draw the rectangle laser
context.fillRect(this.x, this.y, this.width, this.height);
}
/**
* Simulate one step of the laser and verifies if it
* already reach the top of the screen
*/
this.step = function() {
this.y = this.y - INCREMENT;
if(this.y <= 0) return true;
return false;
}
/**
* Return the position xx of the laser
*/
this.getPositionX = function() {
return this.x;
}
/**
* Return the position yy of the laser
*/
this.getPositionY = function() {
return this.y;
}
}
/**
* Class Player
*/
function Player() {
//image src
var imgSrc = 'script/images/ship.png';
//width
this.width = 26;
//height
this.height = 16;
//indicates if the sprite is moving left
this.movingLeft = false;
//indicates if the sprite is moving right
this.movingRight = false;
//create the player sprite
var sprite = new Sprite(20);
//set sprite image and size
sprite.initImage('script/images/ship.png', 26, 16);
//set initial position of the sprite
sprite.initPosition(w / 2, h - 100);
/**
* Draw the player
*/
this.draw = function() {
sprite.draw();
}
/**
* Moves the player character to left
*/
this.moveLeft = function() {
sprite.moveLeft();
}
/**
* Moves the player character to right
*/
this.moveRight = function() {
sprite.moveRight();
}
/**
* Return the position xx of the player
*/
this.getPositionX = function() {
return sprite.x;
}
/**
* Return the position yy of the player
*/
this.getPositionY = function() {
return sprite.y;
}
}
var f = {
init: function(){
$window.on({
keydown: function(event){
switch(event.keyCode) {
case 37:
player.movingLeft = true;
break;
case 39:
player.movingRight = true;
break;
case 32:
lasers[lasers.length] = new Laser(player);
break;
}
},
keyup: function(event){
switch(event.keyCode) {
case 37:
player.movingLeft = false;
break;
case 39:
player.movingRight = false;
break;
}
}
});
f.createEnemies(18, 5);
f.run();
},
run: function(){
f.drawWindow();
player.draw();
f.draw();
f.detectColisions();
gameLoop = setTimeout(f.run, intervalTime);
},
draw: function() {
var currentAction = '';
if(player.movingLeft) player.moveLeft();
else if(player.movingRight) player.moveRight();
for(index in lasers) {
lasers[index].draw();
if(lasers[index].step()){
lasers.splice(index, 1);
}
}
for(index in enemies) {
currentAction = enemies[index].checkStep();
if(currentAction != previousAction){
break;
}
}
for(index in enemies) {
enemies[index].draw();
if(currentAction != previousAction){
enemies[index].jump();
}else{
enemies[index].step();
}
}
previousAction = currentAction;
},
detectColisions: function() {
for(indexLaser in lasers) {
for(indexEnemy in enemies) {
if(f.colisionHandler(lasers[indexLaser], enemies[indexEnemy])) {
lasers.splice(indexLaser, 1);
enemies.splice(indexEnemy, 1);
break;
}
}
}
},
colisionHandler: function(obj1, obj2){
if((obj1.getPositionY() + obj1.height) < obj2.getPositionY()) {
return false;
}
else if(obj1.getPositionY() > (obj2.getPositionY() + obj2.height)) {
return false;
}
else if((obj1.getPositionX() + obj1.width) < obj2.getPositionX()) {
return false;
}
else if(obj1.getPositionX() > (obj2.getPositionX() + obj2.width)) {
return false;
}
return true;
},
drawWindow: function() {
context.fillStyle = '#2fbee2';
context.clearRect(0, 0, w, h);
context.beginPath();
context.rect(0, 0, w, h);
context.closePath();
context.fill();
},
createEnemies: function(numRows, numCols) {
var refPosX = (w / 2),
refPosY = (h / 10);
var fof = [
[0, 0], [2, 0], [0, 1], [2, 1], [0, 2], [1, 2], [2, 2], [2, 3], [2, 4], // 4
[4, 0], [5, 0], [6, 0], [4, 1], [6, 1], [4, 2], [6, 2], [4, 3], [6, 3], [4, 4], [5, 4], [6, 4], // 0
[8, 0], [10, 0], [8, 1], [10, 1], [8, 2], [9, 2], [10, 2], [10, 3], [10, 4] // 4
];
for(var i = 0; i < fof.length; i++){
var posX = refPosX + (40 * fof[i][0]);
var posY = refPosY + (40 * fof[i][1]);
enemies[enemies.length] = new Enemy(posX, posY);
}
/*for(var x = 0; x < numRows; ++x) {
for(var y = 0; y < numCols; ++y) {
var posX = refPosX + (40 * x);
var posY = refPosY + (40 * y);
if(x % 2 == 0){
enemies[enemies.length] = new Space(posX, posY);
}else{
enemies[enemies.length] = new Enemy(posX, posY);
}
}
}*/
}
};
f.init();
});
</script>
</body>
</html>