-
Notifications
You must be signed in to change notification settings - Fork 0
/
JumboJump.html
689 lines (499 loc) · 13.2 KB
/
JumboJump.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
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style>
body {
margin: 0;
color: #fff;
}
canvas
{
position: absolute;
z-index: 0;
}
#point_div
{
position: absolute;
z-index: 1000;
height: 35px;
width: 100px;
//background: red;
text-align: right;
display: flex;
//justify-content: center;
align-items: center;
margin: 10px;
font-size: 18pt;
display: none;
color: #000;
}
img
{
height: 30px;
width: 30px;
margin: 0 10px 0 0;
}
#restart
{
height: 30px;
width: 100px;
background: #262626;
border: none;
color: #fff;
text-transform: uppercase;
position: absolute;
z-index: 1001;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: none;
}
button:focus
{
outline: none;
}
#load
{
height: 100vh;
width : 100vw;
z-index: 1005;
display: flex;
align-items: center;
justify-content: center;
color: #000;
font-size: 12px;
}
#splash
{
height: 100vh;
width: 100vw;
position: absolute;
z-index: 1006;
display: flex;
align-items: center;
justify-content: center;
font-size: 18px;
background: rgba(0,0,0,0.6);
text-transform: uppercase;
}
#tip
{
position: absolute;
width: 100%;
text-align: center;
top: 70%;
font-size: 10px;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.js"></script>
</head>
<body>
<div id="point_div">
<img src="https://i.imgur.com/QEWIhUM.png"><span id="score">0</span>
</div>
<button id="restart">Restart</button>
<div id="load">
<p id="loading">Loading...</p>
</div>
<div id="splash">
Tap to start
<p id="tip">
<u>Tip</u> : Swipe up to jump :)
</p>
</div>
<script>
/*
Author : Arb
Name : LEAP TO LIVE [ GAME ]
Date : 28 - 03 - 2019
*/
// Game assests are collected from Opengameart.com
/*
V1.5
---------------------------------------------
* Double tap added !
* Speed Increases 😉
*/
// Gmae coding starts.....
// Game Comp.
let isJumped = false;
let isGrounded = true;
let game_over = true;
let dom_show = false;
let ground = 60;
let speed = - 0.001;
let land_img;
let gap = 50;
let h,w;
let bg;
let scores = 0;
// Player
let PLAYER_MASS = 3;
let FORCE = 8;
let ACCELERATION = 4.5;
let GRAVITY = 0.6;
let PLAYER_HEIGHT = 55;
let PLAYER_WIDTH = 35;
let player_gap;
let player_images = [];
let walking_speed = 0.25;
// Enemy
let enemies = [];
let ENEMY_HEIGHT = 50;
let ENEMY_WIDTH = 35;
let VELOCITY = -5;
let enemy_num = 3;
let enemy_images = [];
let enemy_gap;
// Lands
let lands = [];
let LAND_HEIGHT = ground;
let LAND_WIDTH = window.innerWidth * 2;
let land_vel = -3;
let land_number = 1;
// Points
let points = [];
let point_taken = false;
let POINT_HEIGHT = 18;
let POINT_WIDTH = 18;
let point_num = 10;
let point_vel = VELOCITY;
let point_img;
// Setup
function setup(){
h = window.innerHeight;
w = window.innerWidth;
//player_gap = h - (ground + (PLAYER_HEIGHT - gap));
player_gap = h - (ground + gap);
//enemy_gap = h - (ground + (ENEMY_HEIGHT - gap));
enemy_gap = h - (ground + gap);
//console.log(LAND_WIDTH);
createCanvas(w, h);
// Dom
if (dom_show == true){
point_div.style.display = 'flex';
load.style.display = 'none';
}
// Player
player = new Player(50, player_gap, PLAYER_WIDTH, PLAYER_HEIGHT);
// Enemy
for ( a = 0; a < enemy_num; a++){
enemies[a] = new Enemy( w + random(40, 140), enemy_gap, ENEMY_WIDTH, ENEMY_HEIGHT, VELOCITY);
}
// Lands
lands = [
new Land(0, h - ground, LAND_WIDTH, LAND_HEIGHT, VELOCITY),
/new Land(w + 50, h - ground, LAND_WIDTH, LAND_HEIGHT)/
];
// Points
for (c = 0; c < point_num; c++){
points[c] = new Point( random(w, w * 2), random((w / 2) + 20, h - (ground + ENEMY_HEIGHT)), POINT_WIDTH, POINT_HEIGHT);
}
// Game over
if (game_over == true){
speed = 0;
}
}
function draw(){
background(240);
image(bg, 0 - 50, 0, w + 50, h - 10);
//fill(255);
for (b = 0; b < lands.length; b++){
lands[b].print();
lands[b].go();
}
for (c = 0; c < points.length; c++){
points[c].print();
points[c].go();
points[c].collidedWithPoint(player);
//points[c].get_point();
}
// Enemy
for (a = 0; a < enemies.length; a++){
enemies[a].print();
enemies[a].go();
player.collidedWith(enemies[a]);
}
// Player
player.print();
player.go();
player.handle();
// Game over
if (game_over == true){
}
restart.addEventListener('click', e => {
restart.style.display = 'none';
game_over = false;
scores = 0;
score.innerText = scores;
for (i = 0; i < enemies.length; i++){
enemies[i].x = w;
enemies[i].velocity = -5;
}
for (j = 0; j < points.length; j++){
points[j].x = random((w / 2) + 20, h - (ground + ENEMY_HEIGHT));
}
for (k = 0; k < lands.length; k++){
lands[k].vel = -5;
}
speed = - 0.001;
isGrounded = true;
});
splash.addEventListener('click', e => {
splash.style.display = 'none';
game_over = false;
});
}
// Player Builder
class Player{
constructor(x, y, w, h){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.img = player_images;
this.mass = PLAYER_MASS;
this.gravity = GRAVITY;
this.force = FORCE;
this.acceleration = ACCELERATION;
this.walking_speed = walking_speed;
this.index = 0;
}
print(){
image(this.img[floor(this.index) % this.img.length], this.x,this.y, this.w, this.h);
if (isGrounded == true){
if (game_over == false)
{
if (isJumped == false){
this.index += this.walking_speed;
}
}
else
{
this.index = 2;
}
}
}
go(){
if (isJumped == true){
this.mass += this.gravity;
this.y += this.mass;
}
if (this.y > player_gap){
isJumped = false;
isGrounded = true;
this.y = player_gap;
}
}
// Collision with enemy
collidedWith(op){
let w1 = this.x + this.w;
let w2 = (op.x + op.w) - 20;
let h1 = this.y + this.h;
let h2 = op.y + op.h;
if (w1 < w2 && w1 > op.x + 20 && h1 < h2 && h1 > op.y){
collided();
}
if (w1 > w2 && w2 > this.x && h1 < h2 && h1 > op.y){
collided();
}
if (w1 > w2 && w2 > this.x && h1 > h2 && h2 > this.y){
collided();
}
if (w1 < w2 && w1 > op.x + 20 && h2 < h1 && h2 > this.y){
collided();
}
}
// collsion With point
handle(){
let c = document.querySelector('canvas');
c.addEventListener('touchstart', e => {
//
isJumped = true;
if (this.y < w / 2 + 130){
this.force = 0;
isGrounded = false;
}
else
{
this.force = FORCE;
this.force += this.acceleration;
if (isGrounded == true){
if (game_over == false){
this.mass = -this.force;
}
}
//isGrounded = false;
}
});
/*c.addEventListener('touchend', e => {
if (isGrounded == true){
isGrounded = false;
}
});*/
}
}
// Enemy Builder
class Enemy{
constructor(x, y, w, h, vel){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.velocity = vel;
this.img = enemy_images;
this.index = 0;
}
print(){
image(this.img[this.index], this.x,this.y, this.w, this.h);
}
go(){
if (this.x > - this.w){
if (game_over == false){
this.velocity += speed;
this.x += this.velocity;
}
}
else
{
this.x = w + this.w;
this.x = w + random(w / 2, w * 3);
this.index = floor(random(0, this.img.length));
//this.x = random(0, w);
}
}
}
// Land Builder
class Land{
constructor(x, y, w, h, vel){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.land_img = land_img;
this.vel = vel;
}
print(){
image(this.land_img, this.x, this.y, this.w, this.h);
}
go(){
if (this.x > - this.w / 2){
if (game_over == false){
this.vel += speed;
this.x += this.vel;
}
}
else
{
this.x = - w / 2;
//this.x = w;
//this.y = random(h / 2 , h - BOTTOM);
}
}
}
// Point
class Point{
constructor(x, y, w, h){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.point_vel = point_vel;
this.point_img = point_img;
}
print(){
image(this.point_img, this.x, this.y, this.w, this.h);
}
go(){
if (this.x > - this.w){
if (game_over == false){
this.x += this.point_vel;
}
}
else
{
this.x = w;
this.print = function (){
image(this.point_img, this.x, this.y, this.w, this.h);
}
this.y = random((w / 2) + 20, h - (ground + ENEMY_HEIGHT));
}
}
// coin collision
collidedWithPoint(op){
let w1 = this.x + this.w;
let w2 = op.x + op.w;
let h1 = this.y + this.h;
let h2 = op.y + op.h;
if (w1 < w2 && w1 > op.x && h1 < h2 && h1 > op.y){
this.print = function(){}
point_taken = true;
this.get_point();
}
else
{
point_taken = false;
}
if (w1 > w2 && w2 > this.x && h1 < h2 && h1 > op.y){
this.print = function(){}
point_taken = true;
this.get_point();
}
else
{
point_taken = false;
}
if (w1 > w2 && w2 > this.x && h1 > h2 && h2 > this.y){
this.print = function(){}
point_taken = true;
this.get_point();
}
else
{
point_taken = false;
}
if (w1 < w2 && w1 > op.x && h2 < h1 && h2 > this.y){
this.print = function(){}
point_taken = true;
this.get_point();
}
else
{
point_taken = false;
}
}
get_point(){
if (game_over == false){
scores++;
}
score.innerText = scores;
if (scores > 10){
VELOCITY += -1;
}
}
}
// Game over
function collided(){
//console.log("Game over!");
game_over = true;
restart.style.display = 'inline';
}
// Preload Images
function preload(){
bg = loadImage('https://i.imgur.com/vH8Z10Xh.jpg');
land_img = loadImage('https://i.imgur.com/h03Te4f.jpg');
player_images[0] = loadImage('https://i.imgur.com/zCXCGBm.jpg');
player_images[1] = loadImage('https://i.imgur.com/DCzLIQc.jpg');
player_images[2] = loadImage('https://i.imgur.com/m7m19va.png');
player_images[3] = loadImage('https://i.imgur.com/a3fQyRW.jpg');
player_images[4] = loadImage('https://i.imgur.com/nYoYe9e.jpg');
player_images[5] = loadImage('https://i.imgur.com/8ADaYGr.jpg');
enemy_images[0] = loadImage('https://i.imgur.com/9Coxu68.jpg');
enemy_images[1] = loadImage('https://i.imgur.com/lT0PA01.jpg');
point_img = loadImage('https://i.imgur.com/QEWIhUM.png');
dom_show = true;
}
</script>
</body>
</html>