-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel2.js
484 lines (480 loc) · 27.6 KB
/
level2.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
let canvas;
let context;
let now;
// Outcome messages
let outcome_element_l = document.querySelector('#loss');
let outcome_element_nl = document.querySelector('#nextLevel');
// Fps declarations
let fpsInterval = 1000 / 30;
let then = Date.now();
// These are declarations for the game loop, or the timer.
let startTime = 0;
let gameDuration = 20000; // 20 seconds in milliseconds
let gameRunning = false; // May not need this.
// These are declarations for the player and mob.
let player = {
x: 0,
y: 0,
width: 24,
height: 32,
frameX: 0,
frameY: 0,
xChange: 0,
yChange: 0,
};
let mob = {
x: 0,
y: 0,
width: 48,
height: 56,
frameX: 0,
frameY: 0,
speed: 4.5,
};
let mob2 = {
x: 0,
y: 0,
width: 48,
height: 56,
frameX: 0,
frameY: 0,
speed: 5,
};
let mob3 = {
x: 0,
y: 0,
width: 48,
height: 56,
frameX: 0,
frameY: 0,
speed: 4,
};
// *Include other mob declarations here*
// Music
let backgroundMusic = new Audio('battleThemeA.mp3');
// Function to start the game and play music
function startGameWithMusic() {
outcome_element_nl.style.display = 'none';
window.addEventListener('keydown', activate, true);
window.addEventListener('keyup', activate, true);
resetGame();
// Start the game
startGame();
// Play the background music
backgroundMusic.play();
// Spawn mob
generateRandomPosition(); // Set initial position of the mob
draw();
}
// Event listener for keydown
document.addEventListener('keydown', (event) => {
if (event.key === ' ') { // Check for the space key
startGameWithMusic();
}
});
// These two functions are to stop and start game respectively , they may have to be moved from here.
function startGame(){
gameRunning = true;
startTime = Date.now();
}
function stopGame() {
window.removeEventListener('keydown', activate, false);
window.removeEventListener('keyup', activate, false);
window.cancelAnimationFrame(draw);
//let outcome_element = document.querySelector('#outcome');
//outcome_element.innerHTML = outcome;
gameRunning = false; //may not need this
}
function resetGame () {
player.x = canvas.width / 2;
player.y = canvas.height - player.height;
}
// This function is used to spawn the mob in a random location.
function generateRandomPosition() {
mob.x = Math.random() * (canvas.width - mob.width);
mob.y = Math.random() * (canvas.height - mob.height);
mob2.x = Math.random() * (canvas.width - mob2.width);
mob2.y = Math.random() * (canvas.height - mob2.height);
mob3.x = Math.random() * (canvas.width - mob3.width);
mob3.y = Math.random() * (canvas.height - mob3.height);
}
// This function updates the mobs position based on the player's position.
function updateMob() {
// Calculate the direction from the mob to the player
let dx = player.x - mob.x;
let dy = player.y - mob.y;
// Normalize the direction vector
let distance = Math.sqrt(dx * dx + dy * dy);
let directionX = dx / distance;
let directionY = dy / distance;
// Move the mob towards the player by multiplying the direction vector by the mob's speed
mob.x += directionX * mob.speed * 0.7;
mob.y += directionY * mob.speed * 0.6;
// Check if the mob is out of bounds and adjust its position if necessary
if (mob.x < 0) {
mob.x = 0;
} else if (mob.x > canvas.width - mob.width) {
mob.x = canvas.width - mob.width;
}
if (mob.y < 0) {
mob.y = 0;
} else if (mob.y > canvas.height - mob.height) {
mob.y = canvas.height - mob.height;
}
}
function updateMob2() {
// Calculate the direction from the mob to the player
let dx = player.x - mob2.x;
let dy = player.y - mob2.y;
// Normalize the direction vector
let distance = Math.sqrt(dx * dx + dy * dy);
let directionX = dx / distance;
let directionY = dy / distance;
// Move the mob towards the player by multiplying the direction vector by the mob's speed
mob2.x += directionX * mob2.speed * 0.75;
mob2.y += directionY * mob2.speed * 0.65;
// Check if the mob is out of bounds and adjust its position if necessary
if (mob2.x < 0) {
mob2.x = 0;
} else if (mob2.x > canvas.width - mob2.width) {
mob2.x = canvas.width - mob2.width;
}
if (mob2.y < 0) {
mob2.y = 0;
} else if (mob2.y > canvas.height - mob2.height) {
mob2.y = canvas.height - mob2.height;
}
}
function updateMob3() {
// Calculate the direction from the mob to the player
let dx = player.x - mob3.x;
let dy = player.y - mob3.y;
// Normalize the direction vector
let distance = Math.sqrt(dx * dx + dy * dy);
let directionX = dx / distance;
let directionY = dy / distance;
// Move the mob towards the player by multiplying the direction vector by the mob's speed
mob3.x += directionX * mob3.speed * 0.65;
mob3.y += directionY * mob3.speed * 0.55;
// Check if the mob is out of bounds and adjust its position if necessary
if (mob3.x < 0) {
mob3.x = 0;
} else if (mob3.x > canvas.width - mob3.width) {
mob3.x = canvas.width - mob3.width;
}
if (mob3.y < 0) {
mob3.y = 0;
} else if (mob3.y > canvas.height - mob3.height) {
mob3.y = canvas.height - mob3.height;
}
}
// This function will later be used to draw text on the canvas.
function drawText(text, x, y, color, size) {
context.fillStyle = color;
context.font = size + "px Arial";
context.fillText(text, x, y);
}
// Loss message
function showLoseMessage() {
outcome_element_l.style.display = 'block';
// Hide the message after a certain delay (e.g., 3 seconds)
setTimeout(() => {
outcome_element_l.style.display = 'none';
}, 2500); // 2500 milliseconds (2.5 seconds)
}
// These declarations are for the player's movement.
let moveLeft = false;
let moveUp = false;
let moveRight = false;
let moveDown = false;
// These declarions are for the player's animation.
let playerImage = new Image();
let backgroundImage = new Image();
let mobImage = new Image();
let mob2Image = new Image();
let mob3Image = new Image();
// Background array for the map.
let tilesPerRow = 10;
let tileSize = 10;
let background = [
[81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82],
[70, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72],
[80, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82],
[70, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72],
[80, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82],
[70, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72],
[80, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82],
[70, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72],
[80, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82],
[70, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72],
[80, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82],
[70, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72],
[80, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82],
[70, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72],
[80, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82],
[70, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72],
[80, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82],
[70, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72],
[80, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82],
[70, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72],
[80, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82],
[70, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72],
[80, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82],
[70, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72],
[80, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82],
[70, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72],
[80, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82],
[70, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72],
[80, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82],
[70, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72],
[80, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82],
[70, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72],
[80, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82],
[70, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72],
[80, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82],
[70, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72],
[80, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82],
[70, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72],
[80, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82],
[70, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72],
[80, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82],
[70, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72],
[80, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82],
[70, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72],
[80, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82],
[70, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72],
[80, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82],
[70, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72],
[80, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82, 81, 82],
[90, 91, 92, 93, 94, 91, 92, 93, 94, 91, 92, 93, 94, 91, 92, 93, 94, 91, 92, 93, 94, 91, 92, 93, 94, 91, 92, 93, 94, 91, 92, 93, 94, 91, 92, 93, 94, 91, 92, 93, 94, 91, 92, 93, 94, 91, 92, 93, 94, 91, 92, 93, 94, 91, 92, 93, 94, 91, 92, 93, 94, 91, 92, 93, 94]
];
// Initialize the game function
document.addEventListener("DOMContentLoaded", init, false);
function init() {
canvas = document.querySelector("canvas");
context = canvas.getContext("2d");
player.x = canvas.width / 2;
player.y = canvas.height - player.height;
playerImage.src = "devil/devil.png";
backgroundImage.src = "tiles.png";
mobImage.src = "frog.png";
mob2Image.src = "frog.png";
mob3Image.src = "frog.png";
window.addEventListener("keydown", activate, false);
window.addEventListener("keyup", deactivate, false);
load_assets([
{ "var": playerImage, "url": "devil.png" },
{ "var": backgroundImage, "url": "tiles.png" },
{ "var": mobImage, "url": "frog.png" },
{ "var": mob2Image, "url": "frog.png" },
{ "var": mob3Image, "url": "frog.png" }
], draw);
}
// Draw , update function
function draw() {
// Set the fps
window.requestAnimationFrame(draw);
let now = Date.now();
let elapsed = now - then;
if (elapsed <= fpsInterval) {
return;
}
then = now - (elapsed % fpsInterval);
let elapsedTimer = now - startTime
if (elapsedTimer > gameDuration) {
outcome_element_nl.style.display = 'block';
console.log(outcome_element_nl) // Show the 'nextLevel' message after the game duration
setTimeout(() => {
outcome_element_nl.style.display = 'none';
stopGame();
}, 100000); // 100000 milliseconds (10 seconds)
return;
}
//May have to move this invocation
updateMob();
updateMob2();
updateMob3();
// Collisions
if (
player.x < mob.x + mob.width &&
player.x + player.width > mob.x &&
player.y < mob.y + mob.height &&
player.y + player.height > mob.y
) {stopGame(); showLoseMessage(); return;} // If the player collides with the mob, restart the game)}
if (
player.x < mob2.x + mob2.width &&
player.x + player.width > mob2.x &&
player.y < mob2.y + mob2.height &&
player.y + player.height > mob2.y
) {stopGame(); showLoseMessage(); return;}
if (
player.x < mob3.x + mob3.width &&
player.x + player.width > mob3.x &&
player.y < mob3.y + mob3.height &&
player.y + player.height > mob3.y
) {stopGame(); showLoseMessage(); return;}
// Draw background on canvas
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillRect(0, 0, canvas.width, canvas.height);
// Draw background tiles
for (let r = 0; r < 50; r += 1) {
for (let c = 0; c < 65; c += 1) {
let tile = background[r][c];
if (tile >= 0) {
let tileRow = Math.floor(tile / tilesPerRow);
let tileCol = Math.floor(tile % tilesPerRow);
context.drawImage(backgroundImage, tileCol * tileSize, tileRow * tileSize,
tileSize, tileSize, c * tileSize, r * tileSize, tileSize, tileSize);
}
}
}
//Draw player
context.drawImage(playerImage,
player.width * player.frameX, player.height * player.frameY, player.width, player.height,
player.x, player.y, player.width, player.height);
if ((moveLeft || moveRight) &&
!(moveRight && moveLeft) ) {
player.frameX = (player.frameX + 1) % 4;
}
if ((moveUp || moveDown) &&
!(moveDown && moveUp) ) {
player.frameX = (player.frameX + 1) % 4;
}
//Draw Mob
context.drawImage(mobImage, mob.x, mob.y, mob.width, mob.height);
if ((moveLeft || moveRight) &&
!(moveRight && moveLeft) ) {
player.frameX = (player.frameX + 1) % 4;
}
if ((moveUp || moveDown) &&
!(moveDown && moveUp) ) {
player.frameX = (player.frameX + 1) % 4;
}
//Draw Mob2
context.drawImage(mob2Image, mob2.x, mob2.y, mob2.width, mob2.height);
if ((moveLeft || moveRight) &&
!(moveRight && moveLeft) ) {
player.frameX = (player.frameX + 1) % 4;
}
if ((moveUp || moveDown) &&
!(moveDown && moveUp) ) {
player.frameX = (player.frameX + 1) % 4;
}
//Draw Mob3
context.drawImage(mob3Image, mob3.x, mob3.y, mob3.width, mob3.height);
if ((moveLeft || moveRight) &&
!(moveRight && moveLeft) ) {
player.frameX = (player.frameX + 1) % 4;
}
if ((moveUp || moveDown) &&
!(moveDown && moveUp) ) {
player.frameX = (player.frameX + 1) % 4;
}
// Draw the timer
let currentTime = Date.now();
let elapsedTime = (currentTime - startTime) / 1000; // Convert to seconds
context.fillStyle = "white";
context.font = "24px Arial";
context.fillText("Time: " + elapsedTime.toFixed(2) + "s", 10, 30);
// Handle Key presses
if (moveLeft) {
// player.xChange = -2;
player.xChange = player.xChange - 0.5;
player.frameY = 3;
}
if (moveRight) {
// player.xChange = -2;
player.xChange = player.xChange + 0.5;
player.frameY = 1;
}
if (moveUp) {
player.yChange = player.yChange - 0.5;
player.frameY = 4;
}
if (moveDown) {
player.yChange = player.yChange + 0.5;
player.frameY = 2;
}
// Update the player
player.x += player.xChange;
player.y += player.yChange;
// Boundary Checks
// Check left boundary
if (player.x < 0) {
player.x = 0;
}
// Check right boundary
if (player.x + player.width > canvas.width) {
player.x = canvas.width - player.width;
}
// Check top boundary
if (player.y < 0) {
player.y = 0;
}
// Check bottom boundary
if (player.y + player.height > canvas.height) {
player.y = canvas.height - player.height;
}
// Physics
let friction = 0.9;
player.xChange *= friction; // Apply friction to the horizontal movement
player.yChange *= friction; // Apply friction to the vertical movement
//Going left or right , up or down
if (player.x + player.width < 0) {
player.x = canvas.width;
} else if (player.x > canvas.width) {
player.x = -player.width;
}
if (player.y + player.width < 0) {
player.y = canvas.width;
} else if (player.y > canvas.width) {
player.y = -player.width;
}
if (gameRunning) {
requestAnimationFrame(draw);
}
}
function activate(event) {
let key = event.key;
if (key === "ArrowLeft") {
moveLeft = true;
} else if (key === "ArrowUp") {
moveUp = true;
} else if (key === "ArrowRight") {
moveRight = true;
} else if (key === "ArrowDown")
moveDown = true;
}
function deactivate(event) {
let key = event.key;
if (key === "ArrowLeft") {
moveLeft = false;
} else if (key === "ArrowUp") {
moveUp = false;
} else if (key === "ArrowRight") {
moveRight = false;
} else if (key === "ArrowDown")
moveDown = false;
}
function load_assets(assets, callback) {
let num_assets = assets.length;
let loaded = function () {
console.log("loaded");
num_assets = num_assets - 1;
if (num_assets === 0) {
callback();
}
};
for (let asset of assets) {
let element = asset.var;
if (element instanceof HTMLImageElement) {
console.log("img");
element.addEventListener("load", loaded, false);
}
else if (element instanceof HTMLAudioElement) {
console.log("audio");
element.addEventListener("canplaythrough", loaded, false);
}
element.src = asset.url;
}
}