Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
titaniumnetwork-dev
GitHub Repository: titaniumnetwork-dev/Incognito-old
Path: blob/main/static/src/gs/public/doodle-jump/main.js
1338 views
1
// RequestAnimFrame: a browser API for getting smooth animations
2
window.requestAnimFrame = (function() {
3
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
4
function(callback) {
5
window.setTimeout(callback, 1000 / 60);
6
};
7
})();
8
9
var canvas = document.getElementById('canvas'),
10
ctx = canvas.getContext('2d');
11
12
var width = 422,
13
height = 552;
14
15
canvas.width = width;
16
canvas.height = height;
17
18
//Variables for game
19
var platforms = [],
20
image = document.getElementById("sprite"),
21
player, platformCount = 10,
22
position = 0,
23
gravity = 0.2,
24
animloop,
25
flag = 0,
26
menuloop, broken = 0,
27
dir, score = 0, firstRun = true;
28
29
//Base object
30
var Base = function() {
31
this.height = 5;
32
this.width = width;
33
34
//Sprite clipping
35
this.cx = 0;
36
this.cy = 614;
37
this.cwidth = 100;
38
this.cheight = 5;
39
40
this.moved = 0;
41
42
this.x = 0;
43
this.y = height - this.height;
44
45
this.draw = function() {
46
try {
47
ctx.drawImage(image, this.cx, this.cy, this.cwidth, this.cheight, this.x, this.y, this.width, this.height);
48
} catch (e) {}
49
};
50
};
51
52
var base = new Base();
53
54
//Player object
55
var Player = function() {
56
this.vy = 11;
57
this.vx = 0;
58
59
this.isMovingLeft = false;
60
this.isMovingRight = false;
61
this.isDead = false;
62
63
this.width = 55;
64
this.height = 40;
65
66
//Sprite clipping
67
this.cx = 0;
68
this.cy = 0;
69
this.cwidth = 110;
70
this.cheight = 80;
71
72
this.dir = "left";
73
74
this.x = width / 2 - this.width / 2;
75
this.y = height;
76
77
//Function to draw it
78
this.draw = function() {
79
try {
80
if (this.dir == "right") this.cy = 121;
81
else if (this.dir == "left") this.cy = 201;
82
else if (this.dir == "right_land") this.cy = 289;
83
else if (this.dir == "left_land") this.cy = 371;
84
85
ctx.drawImage(image, this.cx, this.cy, this.cwidth, this.cheight, this.x, this.y, this.width, this.height);
86
} catch (e) {}
87
};
88
89
this.jump = function() {
90
this.vy = -8;
91
};
92
93
this.jumpHigh = function() {
94
this.vy = -16;
95
};
96
97
};
98
99
player = new Player();
100
101
//Platform class
102
103
function Platform() {
104
this.width = 70;
105
this.height = 17;
106
107
this.x = Math.random() * (width - this.width);
108
this.y = position;
109
110
position += (height / platformCount);
111
112
this.flag = 0;
113
this.state = 0;
114
115
//Sprite clipping
116
this.cx = 0;
117
this.cy = 0;
118
this.cwidth = 105;
119
this.cheight = 31;
120
121
//Function to draw it
122
this.draw = function() {
123
try {
124
125
if (this.type == 1) this.cy = 0;
126
else if (this.type == 2) this.cy = 61;
127
else if (this.type == 3 && this.flag === 0) this.cy = 31;
128
else if (this.type == 3 && this.flag == 1) this.cy = 1000;
129
else if (this.type == 4 && this.state === 0) this.cy = 90;
130
else if (this.type == 4 && this.state == 1) this.cy = 1000;
131
132
ctx.drawImage(image, this.cx, this.cy, this.cwidth, this.cheight, this.x, this.y, this.width, this.height);
133
} catch (e) {}
134
};
135
136
//Platform types
137
//1: Normal
138
//2: Moving
139
//3: Breakable (Go through)
140
//4: Vanishable
141
//Setting the probability of which type of platforms should be shown at what score
142
if (score >= 5000) this.types = [2, 3, 3, 3, 4, 4, 4, 4];
143
else if (score >= 2000 && score < 5000) this.types = [2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4];
144
else if (score >= 1000 && score < 2000) this.types = [2, 2, 2, 3, 3, 3, 3, 3];
145
else if (score >= 500 && score < 1000) this.types = [1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3];
146
else if (score >= 100 && score < 500) this.types = [1, 1, 1, 1, 2, 2];
147
else this.types = [1];
148
149
this.type = this.types[Math.floor(Math.random() * this.types.length)];
150
151
//We can't have two consecutive breakable platforms otherwise it will be impossible to reach another platform sometimes!
152
if (this.type == 3 && broken < 1) {
153
broken++;
154
} else if (this.type == 3 && broken >= 1) {
155
this.type = 1;
156
broken = 0;
157
}
158
159
this.moved = 0;
160
this.vx = 1;
161
}
162
163
for (var i = 0; i < platformCount; i++) {
164
platforms.push(new Platform());
165
}
166
167
//Broken platform object
168
var Platform_broken_substitute = function() {
169
this.height = 30;
170
this.width = 70;
171
172
this.x = 0;
173
this.y = 0;
174
175
//Sprite clipping
176
this.cx = 0;
177
this.cy = 554;
178
this.cwidth = 105;
179
this.cheight = 60;
180
181
this.appearance = false;
182
183
this.draw = function() {
184
try {
185
if (this.appearance === true) ctx.drawImage(image, this.cx, this.cy, this.cwidth, this.cheight, this.x, this.y, this.width, this.height);
186
else return;
187
} catch (e) {}
188
};
189
};
190
191
var platform_broken_substitute = new Platform_broken_substitute();
192
193
//Spring Class
194
var spring = function() {
195
this.x = 0;
196
this.y = 0;
197
198
this.width = 26;
199
this.height = 30;
200
201
//Sprite clipping
202
this.cx = 0;
203
this.cy = 0;
204
this.cwidth = 45;
205
this.cheight = 53;
206
207
this.state = 0;
208
209
this.draw = function() {
210
try {
211
if (this.state === 0) this.cy = 445;
212
else if (this.state == 1) this.cy = 501;
213
214
ctx.drawImage(image, this.cx, this.cy, this.cwidth, this.cheight, this.x, this.y, this.width, this.height);
215
} catch (e) {}
216
};
217
};
218
219
var Spring = new spring();
220
221
function init() {
222
//Variables for the game
223
var dir = "left",
224
jumpCount = 0;
225
226
firstRun = false;
227
228
//Function for clearing canvas in each consecutive frame
229
230
function paintCanvas() {
231
ctx.clearRect(0, 0, width, height);
232
}
233
234
//Player related calculations and functions
235
236
function playerCalc() {
237
if (dir == "left") {
238
player.dir = "left";
239
if (player.vy < -7 && player.vy > -15) player.dir = "left_land";
240
} else if (dir == "right") {
241
player.dir = "right";
242
if (player.vy < -7 && player.vy > -15) player.dir = "right_land";
243
}
244
245
//Adding keyboard controls
246
document.onkeydown = function(e) {
247
var key = e.keyCode;
248
249
if (key == 37) {
250
dir = "left";
251
player.isMovingLeft = true;
252
} else if (key == 39) {
253
dir = "right";
254
player.isMovingRight = true;
255
}
256
257
if(key == 32) {
258
if(firstRun === true)
259
init();
260
else
261
reset();
262
}
263
};
264
265
document.onkeyup = function(e) {
266
var key = e.keyCode;
267
268
if (key == 37) {
269
dir = "left";
270
player.isMovingLeft = false;
271
} else if (key == 39) {
272
dir = "right";
273
player.isMovingRight = false;
274
}
275
};
276
277
//Accelerations produces when the user hold the keys
278
if (player.isMovingLeft === true) {
279
player.x += player.vx;
280
player.vx -= 0.15;
281
} else {
282
player.x += player.vx;
283
if (player.vx < 0) player.vx += 0.1;
284
}
285
286
if (player.isMovingRight === true) {
287
player.x += player.vx;
288
player.vx += 0.15;
289
} else {
290
player.x += player.vx;
291
if (player.vx > 0) player.vx -= 0.1;
292
}
293
294
// Speed limits!
295
if(player.vx > 8)
296
player.vx = 8;
297
else if(player.vx < -8)
298
player.vx = -8;
299
300
//console.log(player.vx);
301
302
//Jump the player when it hits the base
303
if ((player.y + player.height) > base.y && base.y < height) player.jump();
304
305
//Gameover if it hits the bottom
306
if (base.y > height && (player.y + player.height) > height && player.isDead != "lol") player.isDead = true;
307
308
//Make the player move through walls
309
if (player.x > width) player.x = 0 - player.width;
310
else if (player.x < 0 - player.width) player.x = width;
311
312
//Movement of player affected by gravity
313
if (player.y >= (height / 2) - (player.height / 2)) {
314
player.y += player.vy;
315
player.vy += gravity;
316
}
317
318
//When the player reaches half height, move the platforms to create the illusion of scrolling and recreate the platforms that are out of viewport...
319
else {
320
platforms.forEach(function(p, i) {
321
322
if (player.vy < 0) {
323
p.y -= player.vy;
324
}
325
326
if (p.y > height) {
327
platforms[i] = new Platform();
328
platforms[i].y = p.y - height;
329
}
330
331
});
332
333
base.y -= player.vy;
334
player.vy += gravity;
335
336
if (player.vy >= 0) {
337
player.y += player.vy;
338
player.vy += gravity;
339
}
340
341
score++;
342
}
343
344
//Make the player jump when it collides with platforms
345
collides();
346
347
if (player.isDead === true) gameOver();
348
}
349
350
//Spring algorithms
351
352
function springCalc() {
353
var s = Spring;
354
var p = platforms[0];
355
356
if (p.type == 1 || p.type == 2) {
357
s.x = p.x + p.width / 2 - s.width / 2;
358
s.y = p.y - p.height - 10;
359
360
if (s.y > height / 1.1) s.state = 0;
361
362
s.draw();
363
} else {
364
s.x = 0 - s.width;
365
s.y = 0 - s.height;
366
}
367
}
368
369
//Platform's horizontal movement (and falling) algo
370
371
function platformCalc() {
372
var subs = platform_broken_substitute;
373
374
platforms.forEach(function(p, i) {
375
if (p.type == 2) {
376
if (p.x < 0 || p.x + p.width > width) p.vx *= -1;
377
378
p.x += p.vx;
379
}
380
381
if (p.flag == 1 && subs.appearance === false && jumpCount === 0) {
382
subs.x = p.x;
383
subs.y = p.y;
384
subs.appearance = true;
385
386
jumpCount++;
387
}
388
389
p.draw();
390
});
391
392
if (subs.appearance === true) {
393
subs.draw();
394
subs.y += 8;
395
}
396
397
if (subs.y > height) subs.appearance = false;
398
}
399
400
function collides() {
401
//Platforms
402
platforms.forEach(function(p, i) {
403
if (player.vy > 0 && p.state === 0 && (player.x + 15 < p.x + p.width) && (player.x + player.width - 15 > p.x) && (player.y + player.height > p.y) && (player.y + player.height < p.y + p.height)) {
404
405
if (p.type == 3 && p.flag === 0) {
406
p.flag = 1;
407
jumpCount = 0;
408
return;
409
} else if (p.type == 4 && p.state === 0) {
410
player.jump();
411
p.state = 1;
412
} else if (p.flag == 1) return;
413
else {
414
player.jump();
415
}
416
}
417
});
418
419
//Springs
420
var s = Spring;
421
if (player.vy > 0 && (s.state === 0) && (player.x + 15 < s.x + s.width) && (player.x + player.width - 15 > s.x) && (player.y + player.height > s.y) && (player.y + player.height < s.y + s.height)) {
422
s.state = 1;
423
player.jumpHigh();
424
}
425
426
}
427
428
function updateScore() {
429
var scoreText = document.getElementById("score");
430
scoreText.innerHTML = score;
431
}
432
433
function gameOver() {
434
platforms.forEach(function(p, i) {
435
p.y -= 12;
436
});
437
438
if(player.y > height/2 && flag === 0) {
439
player.y -= 8;
440
player.vy = 0;
441
}
442
else if(player.y < height / 2) flag = 1;
443
else if(player.y + player.height > height) {
444
showGoMenu();
445
hideScore();
446
player.isDead = "lol";
447
448
var tweet = document.getElementById("tweetBtn");
449
tweet.href='http://twitter.com/share?url=http://is.gd/PnFFzu&text=I just scored ' +score+ ' points in the HTML5 Doodle Jump game!&count=horiztonal&via=cssdeck&related=solitarydesigns';
450
451
var facebook = document.getElementById("fbBtn");
452
facebook.href='http://facebook.com/sharer.php?s=100&p[url]=http://cssdeck.com/labs/html5-doodle-jump/8&p[title]=I just scored ' +score+ ' points in the HTML5 Doodle Jump game!&p[summary]=Can you beat me in this awesome recreation of Doodle Jump created in HTML5?';
453
454
}
455
}
456
457
//Function to update everything
458
459
function update() {
460
paintCanvas();
461
platformCalc();
462
463
springCalc();
464
465
playerCalc();
466
player.draw();
467
468
base.draw();
469
470
updateScore();
471
}
472
473
menuLoop = function(){return;};
474
animloop = function() {
475
update();
476
requestAnimFrame(animloop);
477
};
478
479
animloop();
480
481
hideMenu();
482
showScore();
483
}
484
485
function reset() {
486
hideGoMenu();
487
showScore();
488
player.isDead = false;
489
490
flag = 0;
491
position = 0;
492
score = 0;
493
494
base = new Base();
495
player = new Player();
496
Spring = new spring();
497
platform_broken_substitute = new Platform_broken_substitute();
498
499
platforms = [];
500
for (var i = 0; i < platformCount; i++) {
501
platforms.push(new Platform());
502
}
503
}
504
505
//Hides the menu
506
function hideMenu() {
507
var menu = document.getElementById("mainMenu");
508
menu.style.zIndex = -1;
509
}
510
511
//Shows the game over menu
512
function showGoMenu() {
513
var menu = document.getElementById("gameOverMenu");
514
menu.style.zIndex = 1;
515
menu.style.visibility = "visible";
516
517
var scoreText = document.getElementById("go_score");
518
scoreText.innerHTML = "You scored " + score + " points!";
519
}
520
521
//Hides the game over menu
522
function hideGoMenu() {
523
var menu = document.getElementById("gameOverMenu");
524
menu.style.zIndex = -1;
525
menu.style.visibility = "hidden";
526
}
527
528
//Show ScoreBoard
529
function showScore() {
530
var menu = document.getElementById("scoreBoard");
531
menu.style.zIndex = 1;
532
}
533
534
//Hide ScoreBoard
535
function hideScore() {
536
var menu = document.getElementById("scoreBoard");
537
menu.style.zIndex = -1;
538
}
539
540
function playerJump() {
541
player.y += player.vy;
542
player.vy += gravity;
543
544
if (player.vy > 0 &&
545
(player.x + 15 < 260) &&
546
(player.x + player.width - 15 > 155) &&
547
(player.y + player.height > 475) &&
548
(player.y + player.height < 500))
549
player.jump();
550
551
if (dir == "left") {
552
player.dir = "left";
553
if (player.vy < -7 && player.vy > -15) player.dir = "left_land";
554
} else if (dir == "right") {
555
player.dir = "right";
556
if (player.vy < -7 && player.vy > -15) player.dir = "right_land";
557
}
558
559
//Adding keyboard controls
560
document.onkeydown = function(e) {
561
var key = e.keyCode;
562
563
if (key == 37) {
564
dir = "left";
565
player.isMovingLeft = true;
566
} else if (key == 39) {
567
dir = "right";
568
player.isMovingRight = true;
569
}
570
571
if(key == 32) {
572
if(firstRun === true) {
573
init();
574
firstRun = false;
575
}
576
else
577
reset();
578
}
579
};
580
581
document.onkeyup = function(e) {
582
var key = e.keyCode;
583
584
if (key == 37) {
585
dir = "left";
586
player.isMovingLeft = false;
587
} else if (key == 39) {
588
dir = "right";
589
player.isMovingRight = false;
590
}
591
};
592
593
//Accelerations produces when the user hold the keys
594
if (player.isMovingLeft === true) {
595
player.x += player.vx;
596
player.vx -= 0.15;
597
} else {
598
player.x += player.vx;
599
if (player.vx < 0) player.vx += 0.1;
600
}
601
602
if (player.isMovingRight === true) {
603
player.x += player.vx;
604
player.vx += 0.15;
605
} else {
606
player.x += player.vx;
607
if (player.vx > 0) player.vx -= 0.1;
608
}
609
610
//Jump the player when it hits the base
611
if ((player.y + player.height) > base.y && base.y < height) player.jump();
612
613
//Make the player move through walls
614
if (player.x > width) player.x = 0 - player.width;
615
else if (player.x < 0 - player.width) player.x = width;
616
617
player.draw();
618
}
619
620
function update() {
621
ctx.clearRect(0, 0, width, height);
622
playerJump();
623
}
624
625
menuLoop = function() {
626
update();
627
requestAnimFrame(menuLoop);
628
};
629
630
menuLoop();
631