Ajax Loader
×
HTML
<!DOCTYPE HTML>
1
<!DOCTYPE HTML>
2
<html lang="en-US">
3
<head>
4
  <title>Jump</title>
5
</head>
6
<body>
7
  <div class="container">
8
    <canvas id="canvas">
9
      Aww, your browser doesn't support HTML5!
10
    </canvas>
11
 
12
    <div id="mainMenu">
13
      <h1>doodle jump</h1>
14
      <h3>using HTML5,</h3>
15
      <h3>...by <a href="http://twitter.com/solitarydesigns/" target="_blank">Kushagra Agarwal</a></h3>
16
      <h3>and <a href="http://cssdeck.com" target="_blank">CSSDeck</a></h3>
17
 
18
      <p class="info">
19
        use
20
        <span class="key left"></span>
21
        <span class="key right"></span>
22
        to move and space to (re) start...
23
      </p>
24
      <a class="button" href="javascript:init()">Play</a>
25
    </div>
26
    
27
    <div id="gameOverMenu">
28
      <h1>game over!</h1>
29
      <h3 id="go_score">you scored 0 points</h3>
30
 
31
      <a class="button" href="javascript:reset()">Restart</a>
32
      <a id="tweetBtn" target="_blank" class="button tweet" href="#">Tweet score</a>
33
 
34
      <a id="fbBtn" target="_blank" class="button fb" href="#">Post on FB</a>
35
    </div>
36
    
37
    
38
    <!-- Preloading image ;) -->
39
    <img id="sprite" src="http://i.imgur.com/2WEhF.png"/>
40
 
41
    <div id="scoreBoard">
42
      <p id="score">0</p>
43
    </div>
44
 
45
  </div>
46
</body>
47
</html> 
 
CSS
@import url(http://fonts.googleapis.com/css?family=Gloria+Hallelujah);
1
@import url(http://fonts.googleapis.com/css?family=Gloria+Hallelujah);
2
 
3
*{box-sizing: border-box;}
4
 
5
body {
6
  margin: 0; padding: 0;
7
  font-family: 'Gloria Hallelujah', cursive;
8
}
9
 
10
.container {
11
  height: 552px;
12
  width: 422px;
13
  position: relative;
14
  margin: 20px auto; 
15
  overflow: hidden;
16
}
17
 
18
canvas {
19
  height: 552px;
20
  width: 422px;
21
  display: block;
22
  background: url(http://i.imgur.com/Y0BMP.png) top left;
23
}
24
 
25
#scoreBoard {
26
  width: 420px;
27
  height: 50px;
28
  background: rgba(182, 200, 220, 0.7);
29
  position: absolute;
30
  top: -3px;
31
  left: 0;
32
  z-index: -1;  
33
  border-image: url(http://i.imgur.com/5BBsR.png) 100 5 round;
34
}
35
 
36
#scoreBoard p {
37
  font-size: 20px;
38
  padding: 0;
39
  line-height: 47px;
40
  margin: 0px 0 0 5px;
41
}
42
 
43
img {display: none}
44
 
45
#mainMenu, #gameOverMenu {
46
  height: 100%;
47
  width: 100%;
48
  text-align: center;
49
  position: absolute;
50
  top: 0;
51
  left: 0;
52
  z-index: 2;
53
}
54
 
55
#gameOverMenu {
56
  visibility: hidden;
57
}
58
 
59
h2, h3, h1 {font-weight: normal}
60
h1 {
61
  font-size: 60px; 
62
  color: #5a5816; 
63
  transform: rotate(-10deg);
64
  margin: 0px;
65
}
66
 
67
h3 {text-align: right; margin: -10px 20px 0 0; color: #5e96be}
68
h3 a {color: #5a5816}
69
 
70
.button {
71
  width: 105px;
72
  height: 31px;
73
  background: url(http://i.imgur.com/2WEhF.png) 0 0 no-repeat;
74
  display: block;
75
  color:  #000;
76
  font-size: 12px;
77
  line-height: 31px;
78
  text-decoration: none;
79
  position: absolute;
80
  left: 50%;
81
  bottom: 50px;
82
  margin-left: -53px;
83
}
84
 
85
.button.tweet {bottom: 100px; background-position: 0 -90px}
86
.button.fb {bottom: 150px; background-position: 0 -60px}
87
 
88
.info {position: absolute; right: 20px; bottom: 00px; margin: 0; color: green}
89
 
90
.info .key {
91
  width: 16px;
92
  height: 16px;
93
  background: url(http://i.imgur.com/2WEhF.png) no-repeat;
94
  text-indent: -9999px;
95
  display: inline-block;
96
}
97
 
98
.info .key.left {background-position: -92px -621px;}
99
.info .key.right {background-position: -92px -641px;}
100
 
 
JavaScript
// RequestAnimFrame: a browser API for getting smooth animations
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
    //Jump the player when it hits the base
295
    if ((player.y + player.height) > base.y && base.y < height) player.jump();
296
 
297
    //Gameover if it hits the bottom 
298
    if (base.y > height && (player.y + player.height) > height && player.isDead != "lol") player.isDead = true;
299
 
300
    //Make the player move through walls
301
    if (player.x > width) player.x = 0 - player.width;
302
    else if (player.x < 0 - player.width) player.x = width;
303
 
304
    //Movement of player affected by gravity
305
    if (player.y >= (height / 2) - (player.height / 2)) {
306
      player.y += player.vy;
307
      player.vy += gravity;
308
    }
309
 
310
    //When the player reaches half height, move the platforms to create the illusion of scrolling and recreate the platforms that are out of viewport...
311
    else {
312
      platforms.forEach(function(p, i) {
313
 
314
        if (player.vy < 0) {
315
          p.y -= player.vy;
316
        }
317
 
318
        if (p.y > height) {
319
          platforms[i] = new Platform();
320
          platforms[i].y = p.y - height;
321
        }
322
 
323
      });
324
 
325
      base.y -= player.vy;
326
      player.vy += gravity;
327
 
328
      if (player.vy >= 0) {
329
        player.y += player.vy;
330
        player.vy += gravity;
331
      }
332
 
333
      score++;
334
    }
335
 
336
    //Make the player jump when it collides with platforms
337
    collides();
338
 
339
    if (player.isDead === true) gameOver();
340
  }
341
 
342
  //Spring algorithms
343
 
344
  function springCalc() {
345
    var s = Spring;
346
    var p = platforms[0];
347
 
348
    if (p.type == 1 || p.type == 2) {
349
      s.x = p.x + p.width / 2 - s.width / 2;
350
      s.y = p.y - p.height - 10;
351
 
352
      if (s.y > height / 1.1) s.state = 0;
353
 
354
      s.draw();
355
    } else {
356
      s.x = 0 - s.width;
357
      s.y = 0 - s.height;
358
    }
359
  }
360
 
361
  //Platform's horizontal movement (and falling) algo
362
 
363
  function platformCalc() {
364
    var subs = platform_broken_substitute;
365
 
366
    platforms.forEach(function(p, i) {
367
      if (p.type == 2) {
368
        if (p.x < 0 || p.x + p.width > width) p.vx *= -1;
369
 
370
        p.x += p.vx;
371
      }
372
 
373
      if (p.flag == 1 && subs.appearance === false && jumpCount === 0) {
374
        subs.x = p.x;
375
        subs.y = p.y;
376
        subs.appearance = true;
377
 
378
        jumpCount++;
379
      }
380
 
381
      p.draw();
382
    });
383
 
384
    if (subs.appearance === true) {
385
      subs.draw();
386
      subs.y += 8;
387
    }
388
 
389
    if (subs.y > height) subs.appearance = false;
390
  }
391
 
392
  function collides() {
393
    //Platforms
394
    platforms.forEach(function(p, i) {
395
      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)) {
396
 
397
        if (p.type == 3 && p.flag === 0) {
398
          p.flag = 1;
399
          jumpCount = 0;
400
          return;
401
        } else if (p.type == 4 && p.state === 0) {
402
          player.jump();
403
          p.state = 1;
404
        } else if (p.flag == 1) return;
405
        else {
406
          player.jump();
407
        }
408
      }
409
    });
410
 
411
    //Springs
412
    var s = Spring;
413
    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)) {
414
      s.state = 1;
415
      player.jumpHigh();
416
    }
417
 
418
  }
419
 
420
  function updateScore() {
421
    var scoreText = document.getElementById("score");
422
    scoreText.innerHTML = score;
423
  }
424
 
425
  function gameOver() {
426
    platforms.forEach(function(p, i) {
427
      p.y -= 12;
428
    });
429
 
430
    if(player.y > height/2 && flag === 0) {
431
      player.y -= 8;
432
      player.vy = 0;
433
    } 
434
    else if(player.y < height / 2) flag = 1;
435
    else if(player.y + player.height > height) {
436
      showGoMenu();
437
      hideScore();
438
      player.isDead = "lol";
439
 
440
      var tweet = document.getElementById("tweetBtn");
441
      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';
442
    
443
      var facebook = document.getElementById("fbBtn");
444
      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?';
445
 
446
    }
447
  }
448
 
449
  //Function to update everything
450
 
451
  function update() {
452
    paintCanvas();
453
    platformCalc();
454
 
455
    springCalc();
456
 
457
    playerCalc();
458
    player.draw();
459
 
460
    base.draw();
461
 
462
    updateScore();
463
  }
464
 
465
  menuLoop = function(){return;};
466
  animloop = function() {
467
    update();
468
    requestAnimFrame(animloop);
469
  };
470
 
471
  animloop();
472
 
473
  hideMenu();
474
  showScore();
475
}
476
 
477
function reset() {
478
  hideGoMenu();
479
  showScore();
480
  player.isDead = false;
481
  
482
  flag = 0;
483
  position = 0;
484
  score = 0;
485
 
486
  base = new Base();
487
  player = new Player();
488
  Spring = new spring();
489
  platform_broken_substitute = new Platform_broken_substitute();
490
 
491
  platforms = [];
492
  for (var i = 0; i < platformCount; i++) {
493
    platforms.push(new Platform());
494
  }
495
}
496
 
497
//Hides the menu
498
function hideMenu() {
499
  var menu = document.getElementById("mainMenu");
500
  menu.style.zIndex = -1;
501
}
502
 
503
//Shows the game over menu
504
function showGoMenu() {
505
  var menu = document.getElementById("gameOverMenu");
506
  menu.style.zIndex = 1;
507
  menu.style.visibility = "visible";
508
 
509
  var scoreText = document.getElementById("go_score");
510
  scoreText.innerHTML = "You scored " + score + " points!";
511
}
512
 
513
//Hides the game over menu
514
function hideGoMenu() {
515
  var menu = document.getElementById("gameOverMenu");
516
  menu.style.zIndex = -1;
517
  menu.style.visibility = "hidden";
518
}
519
 
520
//Show ScoreBoard
521
function showScore() {
522
  var menu = document.getElementById("scoreBoard");
523
  menu.style.zIndex = 1;
524
}
525
 
526
//Hide ScoreBoard
527
function hideScore() {
528
  var menu = document.getElementById("scoreBoard");
529
  menu.style.zIndex = -1;
530
}
531
 
532
function playerJump() {
533
  player.y += player.vy;
534
  player.vy += gravity;
535
 
536
  if (player.vy > 0 && 
537
    (player.x + 15 < 260) && 
538
    (player.x + player.width - 15 > 155) && 
539
    (player.y + player.height > 475) && 
540
    (player.y + player.height < 500))
541
    player.jump();
542
 
543
  if (dir == "left") {
544
    player.dir = "left";
545
    if (player.vy < -7 && player.vy > -15) player.dir = "left_land";
546
  } else if (dir == "right") {
547
    player.dir = "right";
548
    if (player.vy < -7 && player.vy > -15) player.dir = "right_land";
549
  }
550
 
551
  //Adding keyboard controls
552
  document.onkeydown = function(e) {
553
    var key = e.keyCode;
554
 
555
    if (key == 37) {
556
      dir = "left";
557
      player.isMovingLeft = true;
558
    } else if (key == 39) {
559
      dir = "right";
560
      player.isMovingRight = true;
561
    }
562
  
563
    if(key == 32) {
564
      if(firstRun === true) {
565
        init();
566
        firstRun = false;
567
      }
568
      else 
569
        reset();
570
    }
571
  };
572
 
573
  document.onkeyup = function(e) {
574
    var key = e.keyCode;
575
 
576
    if (key == 37) {
577
      dir = "left";
578
      player.isMovingLeft = false;
579
    } else if (key == 39) {
580
      dir = "right";
581
      player.isMovingRight = false;
582
    }
583
  };
584
 
585
  //Accelerations produces when the user hold the keys
586
  if (player.isMovingLeft === true) {
587
    player.x += player.vx;
588
    player.vx -= 0.15;
589
  } else {
590
    player.x += player.vx;
591
    if (player.vx < 0) player.vx += 0.1;
592
  }
593
 
594
  if (player.isMovingRight === 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
  //Jump the player when it hits the base
603
  if ((player.y + player.height) > base.y && base.y < height) player.jump();
604
 
605
  //Make the player move through walls
606
  if (player.x > width) player.x = 0 - player.width;
607
  else if (player.x < 0 - player.width) player.x = width;
608
 
609
  player.draw();
610
}
611
 
612
function update() {
613
  ctx.clearRect(0, 0, width, height);
614
  playerJump();
615
}   
616
 
617
menuLoop = function() {
618
  update();
619
  requestAnimFrame(menuLoop);
620
};
621
 
622
menuLoop();
 

Untitled

CSSDeck G+