Path: blob/main/static/src/gs/public/doodle-jump/main.js
1338 views
// RequestAnimFrame: a browser API for getting smooth animations1window.requestAnimFrame = (function() {2return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||3function(callback) {4window.setTimeout(callback, 1000 / 60);5};6})();78var canvas = document.getElementById('canvas'),9ctx = canvas.getContext('2d');1011var width = 422,12height = 552;1314canvas.width = width;15canvas.height = height;1617//Variables for game18var platforms = [],19image = document.getElementById("sprite"),20player, platformCount = 10,21position = 0,22gravity = 0.2,23animloop,24flag = 0,25menuloop, broken = 0,26dir, score = 0, firstRun = true;2728//Base object29var Base = function() {30this.height = 5;31this.width = width;3233//Sprite clipping34this.cx = 0;35this.cy = 614;36this.cwidth = 100;37this.cheight = 5;3839this.moved = 0;4041this.x = 0;42this.y = height - this.height;4344this.draw = function() {45try {46ctx.drawImage(image, this.cx, this.cy, this.cwidth, this.cheight, this.x, this.y, this.width, this.height);47} catch (e) {}48};49};5051var base = new Base();5253//Player object54var Player = function() {55this.vy = 11;56this.vx = 0;5758this.isMovingLeft = false;59this.isMovingRight = false;60this.isDead = false;6162this.width = 55;63this.height = 40;6465//Sprite clipping66this.cx = 0;67this.cy = 0;68this.cwidth = 110;69this.cheight = 80;7071this.dir = "left";7273this.x = width / 2 - this.width / 2;74this.y = height;7576//Function to draw it77this.draw = function() {78try {79if (this.dir == "right") this.cy = 121;80else if (this.dir == "left") this.cy = 201;81else if (this.dir == "right_land") this.cy = 289;82else if (this.dir == "left_land") this.cy = 371;8384ctx.drawImage(image, this.cx, this.cy, this.cwidth, this.cheight, this.x, this.y, this.width, this.height);85} catch (e) {}86};8788this.jump = function() {89this.vy = -8;90};9192this.jumpHigh = function() {93this.vy = -16;94};9596};9798player = new Player();99100//Platform class101102function Platform() {103this.width = 70;104this.height = 17;105106this.x = Math.random() * (width - this.width);107this.y = position;108109position += (height / platformCount);110111this.flag = 0;112this.state = 0;113114//Sprite clipping115this.cx = 0;116this.cy = 0;117this.cwidth = 105;118this.cheight = 31;119120//Function to draw it121this.draw = function() {122try {123124if (this.type == 1) this.cy = 0;125else if (this.type == 2) this.cy = 61;126else if (this.type == 3 && this.flag === 0) this.cy = 31;127else if (this.type == 3 && this.flag == 1) this.cy = 1000;128else if (this.type == 4 && this.state === 0) this.cy = 90;129else if (this.type == 4 && this.state == 1) this.cy = 1000;130131ctx.drawImage(image, this.cx, this.cy, this.cwidth, this.cheight, this.x, this.y, this.width, this.height);132} catch (e) {}133};134135//Platform types136//1: Normal137//2: Moving138//3: Breakable (Go through)139//4: Vanishable140//Setting the probability of which type of platforms should be shown at what score141if (score >= 5000) this.types = [2, 3, 3, 3, 4, 4, 4, 4];142else if (score >= 2000 && score < 5000) this.types = [2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4];143else if (score >= 1000 && score < 2000) this.types = [2, 2, 2, 3, 3, 3, 3, 3];144else if (score >= 500 && score < 1000) this.types = [1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3];145else if (score >= 100 && score < 500) this.types = [1, 1, 1, 1, 2, 2];146else this.types = [1];147148this.type = this.types[Math.floor(Math.random() * this.types.length)];149150//We can't have two consecutive breakable platforms otherwise it will be impossible to reach another platform sometimes!151if (this.type == 3 && broken < 1) {152broken++;153} else if (this.type == 3 && broken >= 1) {154this.type = 1;155broken = 0;156}157158this.moved = 0;159this.vx = 1;160}161162for (var i = 0; i < platformCount; i++) {163platforms.push(new Platform());164}165166//Broken platform object167var Platform_broken_substitute = function() {168this.height = 30;169this.width = 70;170171this.x = 0;172this.y = 0;173174//Sprite clipping175this.cx = 0;176this.cy = 554;177this.cwidth = 105;178this.cheight = 60;179180this.appearance = false;181182this.draw = function() {183try {184if (this.appearance === true) ctx.drawImage(image, this.cx, this.cy, this.cwidth, this.cheight, this.x, this.y, this.width, this.height);185else return;186} catch (e) {}187};188};189190var platform_broken_substitute = new Platform_broken_substitute();191192//Spring Class193var spring = function() {194this.x = 0;195this.y = 0;196197this.width = 26;198this.height = 30;199200//Sprite clipping201this.cx = 0;202this.cy = 0;203this.cwidth = 45;204this.cheight = 53;205206this.state = 0;207208this.draw = function() {209try {210if (this.state === 0) this.cy = 445;211else if (this.state == 1) this.cy = 501;212213ctx.drawImage(image, this.cx, this.cy, this.cwidth, this.cheight, this.x, this.y, this.width, this.height);214} catch (e) {}215};216};217218var Spring = new spring();219220function init() {221//Variables for the game222var dir = "left",223jumpCount = 0;224225firstRun = false;226227//Function for clearing canvas in each consecutive frame228229function paintCanvas() {230ctx.clearRect(0, 0, width, height);231}232233//Player related calculations and functions234235function playerCalc() {236if (dir == "left") {237player.dir = "left";238if (player.vy < -7 && player.vy > -15) player.dir = "left_land";239} else if (dir == "right") {240player.dir = "right";241if (player.vy < -7 && player.vy > -15) player.dir = "right_land";242}243244//Adding keyboard controls245document.onkeydown = function(e) {246var key = e.keyCode;247248if (key == 37) {249dir = "left";250player.isMovingLeft = true;251} else if (key == 39) {252dir = "right";253player.isMovingRight = true;254}255256if(key == 32) {257if(firstRun === true)258init();259else260reset();261}262};263264document.onkeyup = function(e) {265var key = e.keyCode;266267if (key == 37) {268dir = "left";269player.isMovingLeft = false;270} else if (key == 39) {271dir = "right";272player.isMovingRight = false;273}274};275276//Accelerations produces when the user hold the keys277if (player.isMovingLeft === true) {278player.x += player.vx;279player.vx -= 0.15;280} else {281player.x += player.vx;282if (player.vx < 0) player.vx += 0.1;283}284285if (player.isMovingRight === true) {286player.x += player.vx;287player.vx += 0.15;288} else {289player.x += player.vx;290if (player.vx > 0) player.vx -= 0.1;291}292293// Speed limits!294if(player.vx > 8)295player.vx = 8;296else if(player.vx < -8)297player.vx = -8;298299//console.log(player.vx);300301//Jump the player when it hits the base302if ((player.y + player.height) > base.y && base.y < height) player.jump();303304//Gameover if it hits the bottom305if (base.y > height && (player.y + player.height) > height && player.isDead != "lol") player.isDead = true;306307//Make the player move through walls308if (player.x > width) player.x = 0 - player.width;309else if (player.x < 0 - player.width) player.x = width;310311//Movement of player affected by gravity312if (player.y >= (height / 2) - (player.height / 2)) {313player.y += player.vy;314player.vy += gravity;315}316317//When the player reaches half height, move the platforms to create the illusion of scrolling and recreate the platforms that are out of viewport...318else {319platforms.forEach(function(p, i) {320321if (player.vy < 0) {322p.y -= player.vy;323}324325if (p.y > height) {326platforms[i] = new Platform();327platforms[i].y = p.y - height;328}329330});331332base.y -= player.vy;333player.vy += gravity;334335if (player.vy >= 0) {336player.y += player.vy;337player.vy += gravity;338}339340score++;341}342343//Make the player jump when it collides with platforms344collides();345346if (player.isDead === true) gameOver();347}348349//Spring algorithms350351function springCalc() {352var s = Spring;353var p = platforms[0];354355if (p.type == 1 || p.type == 2) {356s.x = p.x + p.width / 2 - s.width / 2;357s.y = p.y - p.height - 10;358359if (s.y > height / 1.1) s.state = 0;360361s.draw();362} else {363s.x = 0 - s.width;364s.y = 0 - s.height;365}366}367368//Platform's horizontal movement (and falling) algo369370function platformCalc() {371var subs = platform_broken_substitute;372373platforms.forEach(function(p, i) {374if (p.type == 2) {375if (p.x < 0 || p.x + p.width > width) p.vx *= -1;376377p.x += p.vx;378}379380if (p.flag == 1 && subs.appearance === false && jumpCount === 0) {381subs.x = p.x;382subs.y = p.y;383subs.appearance = true;384385jumpCount++;386}387388p.draw();389});390391if (subs.appearance === true) {392subs.draw();393subs.y += 8;394}395396if (subs.y > height) subs.appearance = false;397}398399function collides() {400//Platforms401platforms.forEach(function(p, i) {402if (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)) {403404if (p.type == 3 && p.flag === 0) {405p.flag = 1;406jumpCount = 0;407return;408} else if (p.type == 4 && p.state === 0) {409player.jump();410p.state = 1;411} else if (p.flag == 1) return;412else {413player.jump();414}415}416});417418//Springs419var s = Spring;420if (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)) {421s.state = 1;422player.jumpHigh();423}424425}426427function updateScore() {428var scoreText = document.getElementById("score");429scoreText.innerHTML = score;430}431432function gameOver() {433platforms.forEach(function(p, i) {434p.y -= 12;435});436437if(player.y > height/2 && flag === 0) {438player.y -= 8;439player.vy = 0;440}441else if(player.y < height / 2) flag = 1;442else if(player.y + player.height > height) {443showGoMenu();444hideScore();445player.isDead = "lol";446447var tweet = document.getElementById("tweetBtn");448tweet.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';449450var facebook = document.getElementById("fbBtn");451facebook.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?';452453}454}455456//Function to update everything457458function update() {459paintCanvas();460platformCalc();461462springCalc();463464playerCalc();465player.draw();466467base.draw();468469updateScore();470}471472menuLoop = function(){return;};473animloop = function() {474update();475requestAnimFrame(animloop);476};477478animloop();479480hideMenu();481showScore();482}483484function reset() {485hideGoMenu();486showScore();487player.isDead = false;488489flag = 0;490position = 0;491score = 0;492493base = new Base();494player = new Player();495Spring = new spring();496platform_broken_substitute = new Platform_broken_substitute();497498platforms = [];499for (var i = 0; i < platformCount; i++) {500platforms.push(new Platform());501}502}503504//Hides the menu505function hideMenu() {506var menu = document.getElementById("mainMenu");507menu.style.zIndex = -1;508}509510//Shows the game over menu511function showGoMenu() {512var menu = document.getElementById("gameOverMenu");513menu.style.zIndex = 1;514menu.style.visibility = "visible";515516var scoreText = document.getElementById("go_score");517scoreText.innerHTML = "You scored " + score + " points!";518}519520//Hides the game over menu521function hideGoMenu() {522var menu = document.getElementById("gameOverMenu");523menu.style.zIndex = -1;524menu.style.visibility = "hidden";525}526527//Show ScoreBoard528function showScore() {529var menu = document.getElementById("scoreBoard");530menu.style.zIndex = 1;531}532533//Hide ScoreBoard534function hideScore() {535var menu = document.getElementById("scoreBoard");536menu.style.zIndex = -1;537}538539function playerJump() {540player.y += player.vy;541player.vy += gravity;542543if (player.vy > 0 &&544(player.x + 15 < 260) &&545(player.x + player.width - 15 > 155) &&546(player.y + player.height > 475) &&547(player.y + player.height < 500))548player.jump();549550if (dir == "left") {551player.dir = "left";552if (player.vy < -7 && player.vy > -15) player.dir = "left_land";553} else if (dir == "right") {554player.dir = "right";555if (player.vy < -7 && player.vy > -15) player.dir = "right_land";556}557558//Adding keyboard controls559document.onkeydown = function(e) {560var key = e.keyCode;561562if (key == 37) {563dir = "left";564player.isMovingLeft = true;565} else if (key == 39) {566dir = "right";567player.isMovingRight = true;568}569570if(key == 32) {571if(firstRun === true) {572init();573firstRun = false;574}575else576reset();577}578};579580document.onkeyup = function(e) {581var key = e.keyCode;582583if (key == 37) {584dir = "left";585player.isMovingLeft = false;586} else if (key == 39) {587dir = "right";588player.isMovingRight = false;589}590};591592//Accelerations produces when the user hold the keys593if (player.isMovingLeft === true) {594player.x += player.vx;595player.vx -= 0.15;596} else {597player.x += player.vx;598if (player.vx < 0) player.vx += 0.1;599}600601if (player.isMovingRight === true) {602player.x += player.vx;603player.vx += 0.15;604} else {605player.x += player.vx;606if (player.vx > 0) player.vx -= 0.1;607}608609//Jump the player when it hits the base610if ((player.y + player.height) > base.y && base.y < height) player.jump();611612//Make the player move through walls613if (player.x > width) player.x = 0 - player.width;614else if (player.x < 0 - player.width) player.x = width;615616player.draw();617}618619function update() {620ctx.clearRect(0, 0, width, height);621playerJump();622}623624menuLoop = function() {625update();626requestAnimFrame(menuLoop);627};628629menuLoop();630631