-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
294 lines (247 loc) · 9.34 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
<!DOCTYPE html>
<html lang="en">
<!-- dojo -->
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>JavaScript Project 01 - Space Invaders</title>
<link rel="stylesheet" href="css/styles.css">
<!-- <script src="js/spaceinvaders.js"></script> -->
<script src="js/starfield.js"></script>
</head>
<!--
BUG LIST:
- Multiple missile hits on bottom right enemy causes game to freeze. Error:
Uncaught TypeError: Cannot read property 'top' of undefined
at checkCollisions (index.html:212)
at gameLoop (index.html:240)
- Sort out missiles array and elements
- Missile hits are not entirely accurate
FEATURES TO ADD:
- End game if enemies reach bottom
- End game if enemies hit invader (or have lives?)
- Score
- Enemies move left and right
- No "rapid fire" on missiles
- Enemies drop bombs
-->
<body>
<script>
// KeyCodes:
var LEFT_KEY = 37;
var RIGHT_KEY = 39;
var SPACE_KEY = 32;
var ENTER_KEY = 13;
// Constants:
var SHIP_MOVEMENT = 5;
var MISSILE_MOVEMENT = 12;
var INVADER_MOVEMENT = 1.5;
// Misc variables:
var firing;
var lastGameLoop = 0;
var readytostart = true;
// Create the ship controls:
var shipControl = new Object();
// Create the starfield
var starfield = document.createElement('starfield');
starfield.id = 'starfield';
document.body.appendChild(starfield);
var background = document.getElementById('starfield');
var starfield = new Starfield();
starfield.initialize(background);
starfield.start();
// Display the 'game start' message:
var myh1 = document.createElement('h1');
var mytextnode = document.createTextNode('PRESS ENTER TO START');
myh1.appendChild(mytextnode);
document.body.appendChild(myh1);
// Create the defender:
var defender = document.createElement('defender');
defender.id = 'defender';
document.body.appendChild(defender);
var defender = new Object();
defender.left = 500;
defender.top = 700;
// Create the invaders:
var invaders = document.createElement('invaders');
invaders.id = 'invaders';
document.body.appendChild(invaders);
var invader = new Object();
//invader.element = 'invader';
// Create the missiles
var missiles = document.createElement('missiles');
missiles.id = 'missiles';
document.body.appendChild(missiles);
var missile = new Object();
missile.element = 'missile';
missile.width = 16;
missile.height = 16;
// Create the missiles:
var missiles = [];
// Create the enemies:
var invaders = [
{ left: 200, top: 100 },
{ left: 300, top: 100 },
{ left: 400, top: 100 },
{ left: 500, top: 100 },
{ left: 600, top: 100 },
{ left: 700, top: 100 },
{ left: 200, top: 175 },
{ left: 300, top: 175 },
{ left: 400, top: 175 },
{ left: 500, top: 175 },
{ left: 600, top: 175 },
{ left: 700, top: 175 }
];
// Control ship based on key pressed
function toggleKey(keyCode, isPressed) {
if (keyCode == LEFT_KEY) {
shipControl.left = isPressed;
}
if (keyCode == RIGHT_KEY) {
shipControl.right = isPressed;
}
if (keyCode == SPACE_KEY) {
shipControl.fire = isPressed;
}
// Checks whether the pressed key is "Enter", to start game:
if (keyCode === ENTER_KEY && readytostart) {
readytostart = false;
document.body.removeChild(myh1);
gameLoop();
}
}
// Draw the characters
function showCharacters() {
drawDefender(defender);
drawInvaders();
}
function controlPosition() {
var defender = document.getElementById('defender');
if (defender.left < 200) {
defender.left = 200;
}
if (defender.top < 600 || defender.top > 600) {
defender.top = 600;
}
if (defender.left + defender.width > 820) {
defender.left = 820 - defender.width;
}
if (defender.top + defender.height > 820) {
defender.top = 820 - defender.height;
}
}
function drawDefender(defender) {
var e = document.getElementById('defender');
e.style.left = defender.left + 'px';
e.style.top = defender.top + 'px';
}
function drawInvaders() {
document.getElementById('invaders').innerHTML = "";
for (var invader = 0; invader < invaders.length; invader++) {
// (${} is used as a pattern to show JS what to look at and evaluate)
document.getElementById('invaders').innerHTML +=
`<div class = 'invader' style =
'left:${invaders[invader].left}px;
top:${invaders[invader].top}px;'></div>`;
}
}
function drawMissiles() {
document.getElementById('missiles').innerHTML = "";
for (var missile = 0; missile < missiles.length; missile++) {
// (${} is used as a pattern to show JS what to look at and evaluate)
document.getElementById('missiles').innerHTML +=
`<div class = 'missile' style =
'left:${missiles[missile].left}px;
top:${missiles[missile].top}px;'></div>`;
}
}
function moveMissiles() {
for (var missile = 0; missile < missiles.length; missile++) {
missiles[missile].top -= MISSILE_MOVEMENT;
drawMissiles();
}
}
function moveInvaders() {
for (var invader = 0; invader < invaders.length; invader++) {
invaders[invader].top += INVADER_MOVEMENT;
drawInvaders();
}
}
function checkControls() {
// Move ship left:
if (shipControl.left) {
defender.left -= SHIP_MOVEMENT;
}
// Move ship right:
if (shipControl.right) {
defender.left += SHIP_MOVEMENT;
}
// Fire laser:
if (shipControl.fire) {
missiles.push({
left: defender.left + 8,
top: defender.top
})
}
controlPosition();
}
function checkCollisions() {
for (var invader = 0; invader < invaders.length; invader++) {
for (var missile = 0; missile < missiles.length; missile++) {
// "destroy" enemies with a direct hit:
if (
(missiles[missile].top <= invaders[invader].top + 32) &&
(missiles[missile].top >= invaders[invader].top) &&
(missiles[missile].left >= invaders[invader].left) &&
(missiles[missile].left <= invaders[invader].left + 32)
) {
//Change invader to explosion
//document.getElementById("invaders[invader]").src = "img/explosion.gif";
missiles.splice(missile, 1);
invaders.splice(invader, 1);
}
// Remove enemies when they leave the screen bottom:
// (Change to end game)
// Remove missiles when they leave the screen top:
else if (missiles[missile].top <= 0) {
missiles.splice(missile, 1);
console.log('missile removed');
}
}
// End game when invaders reach bottom:
if (invaders[invader].top + 32 == 800) {
gameloop = clearInterval(gameloop);
}
}
}
// This will keep the timing consistent, to eliminate odd motion
// https://stackoverflow.com/questions/1955687/best-way-for-simple-game-loop-in-javascript
function gameLoop() {
showCharacters();
checkControls();
moveMissiles();
moveInvaders();
checkCollisions();
setTimeout(gameLoop, 50); // This will call gameLoop every 50ms
}
document.onkeydown = function (event) {
toggleKey(event.keyCode, true);
}
document.onkeyup = function (event) {
toggleKey(event.keyCode, false);
}
</script>
</body>
</html>
<!-- // Press 'enter' to start game:
// var myh1 = document.createElement('h1');
// var mytextnode = document.createTextNode('PRESS Enter TO START GAME');
// myh1.appendChild(mytextnode);
// document.body.appendChild(myh1);
// document.body.onkeyup = function (e) {
// if (e.keyCode == 13) {
// //location.reload();
// gameLoop(); // Run the game
// } -->