-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
486 lines (451 loc) · 13.6 KB
/
index.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
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// internal game array
var board = new Array(10);
for (var i = 0; i < board.length; i++) {
board[i] = new Array(10);
for (var j = 0; j < board[i].length; j++) {
board[i][j] = 0;
}
}
// global variables
// current score
var score_count = 0;
// player life
var player_alive=true;
// current chance of a comet spawning
var chance = 0.1;
// current difficulty in terms of speed adjustment
var difficulty = 0;
// current status of reset
var reset= false;
// current fireball recharge status
var fireball_recharge = 0;
// current player input status (allowed or paused)
var paused = false;
// first load check
var first_load = true;
// function to reset the game back to the initial state
function resetFunction(){
// kill the current player if they're still alive
player_alive = false;
// reset game Array
for (var i = 0; i < board.length; i++) {
for (var j = 0; j < board.length; j++) {
board[i][j] = 0;
}
}
// reset game board
drawToPage();
// new score
score_count = 0;
// new chance of a comet spawning
chance = 0.1;
// new difficulty in terms of speed adjustment
difficulty = 0;
// new status of reset
reset= false;
// new fireball recharge status
fireball_recharge = 0;
// disables the pausing of player input
paused = false;
// place 10 in the board to represent the player
board[9][5] = 10;
// draw the player to the table
var table = document.getElementsByClassName('game_frame')[0].children[0].children;
var row = table[9].children;
row[5].classList.add('player');
// update score_display
document.getElementById('score_display').innerHTML = '';
document.getElementById('reset').innerHTML = '';
document.getElementById('new_game').innerHTML = 'START';
document.getElementById('message').innerHTML = '';
document.getElementById('menu').innerHTML = 'MENU';
// mark reset as completed
reset = true;
if(first_load==true){
first_load = false;
newGame();
}
}
// new game
function newGame(){
// start a new game only if the game has just been reset
if(reset == true){
// adjust score display
document.getElementById('score_display').innerHTML = 0;
document.getElementById('reset').innerHTML = 'RESET';
document.getElementById('new_game').innerHTML = '';
document.getElementById('message').innerHTML = '';
document.getElementById('menu').innerHTML = '';
// new life
player_alive = true;
gameLoop();
reset = false;
}
}
// game loop - this runs repeatedly
function gameLoop(){
// stop the loop is player dead
if(player_alive == true){
// pull values from the page board
drawToArray();
// check if the player has died since the last cycle
checkForDeath();
// update the score and score display
updateScore();
// raise any fireballs up the screen
raiseFireballs();
// small chance of having a complete wall of comets
// this will automatically refill your fireball recharge so you have a chance to make it through
// if you had already used your fireball and it was recharging
if(chance/20 > Math.random()){
// spawn in new blocks
for (var i = 0; i < board.length; i++) {
board[0][i] = 1;
fireball_recharge = 0;
}
}
// drop blocks down the page
// and spawn new blocks at the top
dropBlocks();
// draw the contents of the game array to the screen now
drawToPage();
// increase the difficulty level
incrementDifficulty();
// after some amount of time call this loop again
setTimeout(gameLoop, 280 - difficulty*100);
}
}
// draws the page to the board array
function drawToArray(){
// clear the array
for (var i = 0; i < board.length; i++) {
for (var j = 0; j < board.length; j++) {
board[i][j] = 0;
}
}
// get table from html page and add to array
var table = document.getElementsByClassName('game_frame')[0].children[0].children;
for (var i = 0; i < table.length; i++) {
var row = table[i].children;
for (var j = 0; j < row.length; j++) {
// if the board contains a block
if (row[j].classList.contains('block') ) {
board[i][j] = 1;
}
// if the board contains a player
if (row[j].classList.contains('player')) {
board[i][j] = 10;
}
// if the board contains a fireball
if(row[j].classList.contains('fireball')){
board[i][j] = 2;
}
// if the board contains a large comet
if(row[j].classList.contains('large_comet')){
board[i][j] = 3;
}
}
}
}
// function to check if the player is about to die
function checkForDeath(){
// get player coordinates
var player = document.getElementsByClassName('player');
player=player[0];
if(player){
// do nothing
}
else {
// end game
document.getElementById('message').innerHTML = 'YOU DIED';
document.getElementById('menu').innerHTML = 'MENU';
player_alive = false;
}
}
// function to update the score
function updateScore(){
// catch the score_display DOM object
var score_display = document.getElementById('score_display');
// get new score
for (var i = 0; i < board.length; i++) {
if (board[9][i] == 1 || board[9][i] == 3) {
// increase the score
score_count++;
// if the fireball function needs recharging then recharge it here
if(fireball_recharge>0){
score_display.style.color ='#824dff';
fireball_recharge--;
}
if (fireball_recharge == 0) {
score_display.style.color='#4d94ff';
}
}
}
// display the new score
score_display.innerHTML = score_count;
}
// function to raise fireballs up the screen
function raiseFireballs(){
// clear top row
for (var i = 0; i < board.length; i++) {
if (board[0][i] == 2) {
board[0][i] = 0;
}
}
// raise each fireball
for (var i = 1; i < board.length ; i++) {
for (var j = 0; j < board.length; j++) {
if (board[i][j]==2) {
if(board[i-1][j] == 3){
board[i][j] = 0;
}
else{
board[i-1][j] = 2;
board[i][j] = 0;
}
drawToPage();
}
}
}
}
// drops the blocks on the top of the page
function dropBlocks(){
// clear bottom row
for (var i = 0; i < board.length; i++) {
// case for small comets
if (board[9][i] == 1) {
board[9][i] = 0;
}
// case for large comets
if(board[9][i] == 3){
board[9][i] = 0;
}
}
// drop old blocks
for (var i = board.length-2; i >=0 ; i--) {
for (var j = 0; j < board.length; j++) {
// case for small comets
if (board[i][j]==1) {
// if dropping into a fireball then leave it as a fireball
if(board[i+1][j] == 2){
// do nothing
}
else {
board[i+1][j] = 1;
}
board[i][j] = 0;
}
// case for large comets
if(board[i][j] == 3){
if(board[i+1][j] == 2){
board[i+1][j] = 3;
}
else {
board[i+1][j] = 3;
}
board[i][j] = 0;
}
}
}
// spawn in new blocks
for (var i = 0; i < board.length; i++) {
var n = Math.random();
if (n < chance*4/5) {
board[0][i] = 1;
}
if(n < chance && chance*4/5 < n){
board[0][i] = 3;
}
}
}
// draws the board array to the page
function drawToPage(){
// get table information
var table = document.getElementsByClassName('game_frame')[0].children[0].children;
// clear the page
for (var i = 0; i < board.length; i++) {
var row = table[i].children;
for (var j = 0; j < row.length; j++) {
row[j].classList.remove('block');
row[j].classList.remove('fireball');
row[j].classList.remove('large_comet');
row[j].classList.remove('player');
}
}
// draw to the page
// for all rows
for (var i = 0; i < board.length; i++) {
var row = table[i].children;
// for all columns
for (var j = 0; j < row.length; j++) {
// if it contains a block
if(board[i][j] == 1){
row[j].classList.add('block');
}
// if it contains a fireball
if (board[i][j] == 2) {
row[j].classList.add('fireball');
}
// if it contains the player
if (board[i][j] == 10) {
row[j].classList.add('player');
}
// if it contains a large_comet
if (board[i][j] == 3) {
row[j].classList.add('large_comet');
}
}
}
}
// increments difficulty and chance
function incrementDifficulty(){
// this determines how fast the game gets more difficulty
var difficulty_rate = 200;
// increment equations
difficulty = difficulty + (1-difficulty)/difficulty_rate;
// this determines the upper limit on the values of chance
var chance_max = 0.3;
chance = chance + (chance_max-chance)/difficulty_rate;
}
// function to add an event listener to the whole page for key presses
var addEvent = document.addEventListener ? function(target,type,action){
if(target){
target.addEventListener(type, action, false);
}
} : function(target,type,action){
if(target){
target.attachEvent('on' + type, action, false);
}
}
// add functionality for each key pressed (key down and key up)
addEvent(document,'keydown',function(e) {
// grab the event
e = e || window.event;
var key = e.which || e.keyCode;
if(paused == false){
// switch case for which key was pressed
switch (key) {
// case for W
case 87:
// getting player coordinates and adjusting them
var player = document.getElementsByClassName('player');
player = player[0];
var x = player.cellIndex;
var y = player.parentElement.rowIndex;
y--;
if(y<=1) y = 1;
// get new coordinates
var table = document.getElementsByClassName('game_frame')[0].children;
var row = table[0].children[y].children;
var col = row[x];
// if you can move into the square then do so
if(col.classList == ''){
player.classList.remove('player');
col.classList.add('player');
}
break;
// case for A
case 65:
// getting player coordinates and adjusting them
var player = document.getElementsByClassName('player')[0];
var x = player.cellIndex;
var y = player.parentElement.rowIndex;
x--;
if(x<=0) x = 0;
// get new coordinates
var table = document.getElementsByClassName('game_frame')[0].children;
var row = table[0].children[y].children;
var col = row[x];
// if you can move into the square then do so
if(col.classList == ''){
player.classList.remove('player');
col.classList.add('player');
}
break;
// case for S
case 83:
// getting player coordinates and adjusting them
var player = document.getElementsByClassName('player');
player = player[0];
var x = player.cellIndex;
var y = player.parentElement.rowIndex;
y++;
if(y>=9) y = 9;
// get new coordinates
var table = document.getElementsByClassName('game_frame')[0].children;
var row = table[0].children[y].children;
var col = row[x];
// if you can move into the square then do so
if(col.classList == ''){
player.classList.remove('player');
col.classList.add('player');
}
break;
// case for D
case 68:
// getting player coordinates and adjusting them
var player = document.getElementsByClassName('player');
player = player[0];
var x = player.cellIndex;
var y = player.parentElement.rowIndex;
x++;
if(x>=9) x = 9;
// get new coordinates
var table = document.getElementsByClassName('game_frame')[0].children;
var row = table[0].children[y].children;
var col = row[x];
// if you can move into the square then do so
if(col.classList == ''){
player.classList.remove('player');
col.classList.add('player');
}
break;
// case for space
case 32:
// if the player isn't shooting a fireball, is alive and has recharged their fireball timer
if(player_alive == true && fireball_recharge == 0){
var player = document.getElementsByClassName('player');
player = player[0];
var x = player.cellIndex;
var y = player.parentElement.rowIndex;
// adjust coordinates and set movement to paused
y = y-1;
paused = true;
setTimeout(unpause, 180);
// function to unpause the pause on movement
function unpause(){
paused = false;
}
// get the cell above the player
var table = document.getElementsByClassName('game_frame')[0].children;
var row = table[0].children[y].children;
var col = row[x];
// function which shoots the fireball
shootFireball();
// function to shoot fireball
function shootFireball(){
// this determines how many points to recharge the fireball
var recharge_time = 50;
if(col.classList == ''){
col.classList.add('fireball');
board[y][x] = 2;
// set the fireball recharge to 20
fireball_recharge= recharge_time;
}
}
}
break;
}
}
}
)
// function to play the game theme
function playTheme(){
var audio = new Audio('music/game_theme.flac');
audio.volume = 0.1;
audio.play();
}
// run the reset on load
resetFunction();
// play the theme on load
playTheme();