1// RequestAnimFrame: a browser API for getting smooth animations 2window.requestAnimFrame = (function () { 3 return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || 4 function (callback) { 5 window.setTimeout(function () { callback(); }, 1000 / 60); 6 }; 7})(); 8 9var canvas = document.getElementById('canvas'), 10 ctx = canvas.getContext('2d'); 11 12var width = 422, 13 height = 552; 14 15canvas.width = width; 16canvas.height = height; 17 18//Audio 19var china = new Audio(); // create the audio object 20china.src = "./files/audio/china.mp3"; // assign the audio file to its src 21// china.play(); // play the music 22var greatWall = new Audio(); // create the audio object 23greatWall.src = "./files/audio/greatWall.mp3"; // assign the audio file to its src 24 25var bing01 = new Audio(); // create the audio object 26bing01.src = "./files/audio/bing01.mp3"; // assign the audio file to its src 27 28var bing02 = new Audio(); // create the audio object 29bing02.src = "./files/audio/bing02.mp3"; // assign the audio file to its src 30 31var bing03 = new Audio(); // create the audio object 32bing03.src = "./files/audio/bing03.mp3"; // assign the audio file to its src 33 34var bing04 = new Audio(); // create the audio object 35bing04.src = "./files/audio/bing04.mp3"; // assign the audio file to its src 36 37var bing05 = new Audio(); // create the audio object 38bing05.src = "./files/audio/bing05.mp3"; // assign the audio file to its src 39 40var bing06 = new Audio(); // create the audio object 41bing06.src = "./files/audio/bing06.mp3"; // assign the audio file to its src 42 43var bing07 = new Audio(); // create the audio object 44bing07.src = "./files/audio/bing07.mp3"; // assign the audio file to its src 45 46var bing08 = new Audio(); // create the audio object 47bing08.src = "./files/audio/bing08.mp3"; // assign the audio file to its src 48 49var bing09 = new Audio(); // create the audio object 50bing09.src = "./files/audio/bing09.mp3"; // assign the audio file to its src 51 52//Variables for game 53var platforms = [], 54 image = document.getElementById("sprite"), 55 player, platformCount = 10, 56 position = 0, 57 gravity = 0.2, 58 animloop, 59 flag = 0, 60 menuloop, broken = 0, 61 dir, score = 0, 62 firstRun = true; 63 64//Base object 65var Base = function () { 66 this.height = 5; 67 this.width = width; 68 69 //Sprite clipping 70 this.cx = 0; 71 this.cy = 614; 72 this.cwidth = 100; 73 this.cheight = 5; 74 75 this.moved = 0; 76 77 this.x = 0; 78 this.y = height - this.height; 79 80 this.draw = function () { 81 try { 82 ctx.drawImage(image, this.cx, this.cy, this.cwidth, this.cheight, this.x, this.y, this.width, this.height); 83 } catch (e) { } 84 }; 85}; 86 87var base = new Base(); 88 89//Player object 90var Player = function () { 91 this.vy = 11; 92 this.vx = 0; 93 94 this.isMovingLeft = false; 95 this.isMovingRight = false; 96 this.isDead = false; 97 98 this.width = 55; 99 this.height = 40; 100 101 //Sprite clipping 102 this.cx = 0; 103 this.cy = 0; 104 this.cwidth = 110; 105 this.cheight = 80; 106 107 this.dir = "left"; 108 109 this.x = width / 2 - this.width / 2; 110 this.y = height; 111 112 //Function to draw it 113 this.draw = function () { 114 try { 115 if (this.dir == "right") this.cy = 121; 116 else if (this.dir == "left") this.cy = 201; 117 else if (this.dir == "right_land") this.cy = 289; 118 else if (this.dir == "left_land") this.cy = 371; 119 120 ctx.drawImage(image, this.cx, this.cy, this.cwidth, this.cheight, this.x, this.y, this.width, this.height); 121 } catch (e) { } 122 }; 123 var counter = 0 124 this.jump = function () { 125 this.vy = -8; 126 counter += 1 127 128 switch (counter) { 129 case 0: 130 break; 131 case 1: 132 bing01.play(); 133 break; 134 case 2: 135 bing02.play(); 136 break; 137 case 3: 138 bing03.play(); 139 break; 140 case 4: 141 bing04.play(); 142 break; 143 case 5: 144 bing05.play(); 145 break; 146 case 6: 147 bing06.play(); 148 break 149 case 7: 150 bing07.play(); 151 break 152 case 8: 153 bing08.play(); 154 break 155 case 9: 156 bing09.play(); 157 counter = 0; 158 break 159 } 160 }; 161 162 this.jumpHigh = function () { 163 this.vy = -16; 164 }; 165 166}; 167 168player = new Player(); 169 170//Platform class 171 172function Platform() { 173 this.width = 70; 174 this.height = 17; 175 176 this.x = Math.random() * (width - this.width); 177 this.y = position; 178 179 position += (height / platformCount); 180 181 this.flag = 0; 182 this.state = 0; 183 184 //Sprite clipping 185 this.cx = 0; 186 this.cy = 0; 187 this.cwidth = 105; 188 this.cheight = 31; 189 190 //Function to draw it 191 this.draw = function () { 192 try { 193
194 if (this.type == 1) this.cy = 0; 195 else if (this.type == 2) this.cy = 61; 196 else if (this.type == 3 && this.flag === 0) this.cy = 31; 197 else if (this.type == 3 && this.flag == 1) this.cy = 1000; 198 else if (this.type == 4 && this.state === 0) this.cy = 90; 199 else if (this.type == 4 && this.state == 1) this.cy = 1000; 200 201 ctx.drawImage(image, this.cx, this.cy, this.cwidth, this.cheight, this.x, this.y, this.width, this.height); 202 } catch (e) { } 203 }; 204 205 //Platform types 206 //1: Normal 207 //2: Moving 208 //3: Breakable (Go through) 209 //4: Vanishable 210 //Setting the probability of which type of platforms should be shown at what score 211 if (score >= 5000) this.types = [2, 3, 3, 3, 4, 4, 4, 4]; 212 else if (score >= 2000 && score < 5000) this.types = [2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4]; 213 else if (score >= 1000 && score < 2000) this.types = [2, 2, 2, 3, 3, 3, 3, 3]; 214 else if (score >= 500 && score < 1000) this.types = [1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3]; 215 else if (score >= 100 && score < 500) this.types = [1, 1, 1, 1, 2, 2]; 216 else this.types = [1]; 217 218 this.type = this.types[Math.floor(Math.random() * this.types.length)]; 219 220 //We can't have two consecutive breakable platforms otherwise it will be impossible to reach another platform sometimes! 221 if (this.type == 3 && broken < 1) { 222 broken++; 223 } else if (this.type == 3 && broken >= 1) { 224 this.type = 1; 225 broken = 0; 226 } 227 228 this.moved = 0; 229 this.vx = 1; 230} 231 232for (var i = 0; i < platformCount; i++) { 233 platforms.push(new Platform()); 234} 235 236//Broken platform object 237var Platform_broken_substitute = function () { 238 this.height = 30; 239 this.width = 70; 240 241 this.x = 0; 242 this.y = 0; 243 244 //Sprite clipping 245 this.cx = 0; 246 this.cy = 554; 247 this.cwidth = 105; 248 this.cheight = 60; 249 250 this.appearance = false; 251 252 this.draw = function () { 253 try { 254 if (this.appearance === true) ctx.drawImage(image, this.cx, this.cy, this.cwidth, this.cheight, this.x, this.y, this.width, this.height); 255 else return; 256 } catch (e) { } 257 }; 258}; 259 260var platform_broken_substitute = new Platform_broken_substitute(); 261 262//Spring Class 263var spring = function () { 264 this.x = 0; 265 this.y = 0; 266 267 this.width = 26; 268 this.height = 30; 269 270 //Sprite clipping 271 this.cx = 0; 272 this.cy = 0; 273 this.cwidth = 45; 274 this.cheight = 53; 275 276 this.state = 0; 277 278 this.draw = function () { 279 try { 280 if (this.state === 0) this.cy = 445; 281 else if (this.state == 1) this.cy = 501; 282 283 ctx.drawImage(image, this.cx, this.cy, this.cwidth, this.cheight, this.x, this.y, this.width, this.height); 284 } catch (e) { } 285 }; 286}; 287 288var Spring = new spring(); 289 290function init() { 291 //Variables for the game 292 var dir = "left", 293 jumpCount = 0; 294 295 firstRun = false; 296 297 //Function for clearing canvas in each consecutive frame 298 299 function paintCanvas() { 300 ctx.clearRect(0, 0, width, height); 301 } 302 303 //Player related calculations and functions 304 305 function playerCalc() { 306 if (dir == "left") { 307 player.dir = "left"; 308 if (player.vy < -7 && player.vy > -15) player.dir = "left_land"; 309 } else if (dir == "right") { 310 player.dir = "right"; 311 if (player.vy < -7 && player.vy > -15) player.dir = "right_land"; 312 } 313 314 //Adding keyboard controls 315 document.onkeydown = function (e) { 316 var key = e.keyCode; 317 318 if (key == 37) { 319 dir = "left"; 320 player.isMovingLeft = true; 321 } else if (key == 39) { 322 dir = "right"; 323 player.isMovingRight = true; 324 } 325 326 if (key == 32) { 327 if (firstRun === true) 328 init(); 329 else 330 reset(); 331 } 332 }; 333 334 document.onkeyup = function (e) { 335 var key = e.keyCode; 336 337 if (key == 37) { 338 dir = "left"; 339 player.isMovingLeft = false; 340 } else if (key == 39) { 341 dir = "right"; 342 player.isMovingRight = false; 343 } 344 }; 345 346 //Accelerations produces when the user hold the keys 347 if (player.isMovingLeft === true) { 348 player.x += player.vx; 349 player.vx -= 0.15; 350 } else { 351 player.x += player.vx; 352 if (player.vx < 0) player.vx += 0.1; 353 } 354 355 if (player.isMovingRight === true) { 356 player.x += player.vx; 357 player.vx += 0.15; 358 } else { 359 player.x += player.vx; 360 if (player.vx > 0) player.vx -= 0.1; 361 } 362 363 // Speed limits! 364 if (player.vx > 8) 365 player.vx = 8; 366 else if (player.vx < -8) 367 player.vx = -8; 368 369 //console.log(player.vx); 370 371 //Jump the player when it hits the base 372 if ((player.y + player.height) > base.y && base.y < height) player.jump(); 373 374 //Gameover if it hits the bottom
375 if (base.y > height && (player.y + player.height) > height && player.isDead != "lol") player.isDead = true; 376 377 //Make the player move through walls 378 if (player.x > width) player.x = 0 - player.width; 379 else if (player.x < 0 - player.width) player.x = width; 380 381 //Movement of player affected by gravity 382 if (player.y >= (height / 2) - (player.height / 2)) { 383 player.y += player.vy; 384 player.vy += gravity; 385 } 386 387 //When the player reaches half height, move the platforms to create the illusion of scrolling and recreate the platforms that are out of viewport... 388 else { 389 platforms.forEach(function (p, i) { 390 391 if (player.vy < 0) { 392 p.y -= player.vy; 393 } 394 395 if (p.y > height) { 396 platforms[i] = new Platform(); 397 platforms[i].y = p.y - height; 398 } 399 400 }); 401 402 base.y -= player.vy; 403 player.vy += gravity; 404 405 if (player.vy >= 0) { 406 player.y += player.vy; 407 player.vy += gravity; 408 } 409 410 score++; 411 } 412 413 //Make the player jump when it collides with platforms 414 collides(); 415 416 if (player.isDead === true) gameOver(); //JUMPING??? 417 } 418 419 //Spring algorithms 420 421 function springCalc() { 422 var s = Spring; 423 var p = platforms[0]; 424 425 if (p.type == 1 || p.type == 2) { 426 s.x = p.x + p.width / 2 - s.width / 2; 427 s.y = p.y - p.height - 10; 428 429 if (s.y > height / 1.1) s.state = 0; 430 431 s.draw(); 432 } else { 433 s.x = 0 - s.width; 434 s.y = 0 - s.height; 435 } 436 } 437 438 //Platform's horizontal movement (and falling) algo 439 440 function platformCalc() { 441 var subs = platform_broken_substitute; 442 443 platforms.forEach(function (p, i) { 444 if (p.type == 2) { 445 if (p.x < 0 || p.x + p.width > width) p.vx *= -1; 446 447 p.x += p.vx; 448 } 449 450 if (p.flag == 1 && subs.appearance === false && jumpCount === 0) { 451 subs.x = p.x; 452 subs.y = p.y; 453 subs.appearance = true; 454 455 jumpCount++; 456 } 457 458 p.draw(); 459 }); 460 461 if (subs.appearance === true) { 462 subs.draw(); 463 subs.y += 8; 464 } 465 466 if (subs.y > height) subs.appearance = false; 467 } 468 469 function collides() { 470 //Platforms 471 platforms.forEach(function (p, i) { 472 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)) { 473 474 if (p.type == 3 && p.flag === 0) { 475 p.flag = 1; 476 jumpCount = 0; 477 return; 478 } else if (p.type == 4 && p.state === 0) { 479 player.jump(); 480 p.state = 1; 481 } else if (p.flag == 1) return; 482 else { 483 player.jump(); 484 } 485 } 486 }); 487 488 //Springs 489 var s = Spring; 490 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)) { 491 s.state = 1; 492 player.jumpHigh(); 493 china.play(); 494 } 495 496 } 497 498 function updateScore() { 499 var scoreText = document.getElementById("score"); 500 scoreText.textContent = score; 501 } 502 503 function gameOver() { 504 platforms.forEach(function (p, i) { 505 p.y -= 12; 506 greatWall.play(); 507 }); 508 509 if (player.y > height / 2 && flag === 0) { 510 player.y -= 8; 511 player.vy = 0; 512 } else if (player.y < height / 2) flag = 1; 513 else if (player.y + player.height > height) { 514 // SocialModuleInstance && SocialModuleInstance.showSocial(); TODO: Implement working socialModule 515 showGoMenu(); 516 hideScore(); 517 player.isDead = "lol"; 518 } 519 } 520 521 //Function to update everything 522 523 function update() { 524 paintCanvas(); 525 platformCalc(); 526 527 springCalc(); 528 529 playerCalc(); 530 player.draw(); 531 532 base.draw(); 533 534 updateScore(); 535 } 536 537 menuLoop = function () { 538 return; 539 }; 540 animloop = function () { 541 update(); 542 requestAnimFrame(animloop); 543 }; 544 545 animloop(); 546 547 hideMenu(); 548 showScore(); 549} 550 551function reset() { 552 hideGoMenu(); 553 showScore(); 554 player.isDead = false; 555 556 flag = 0; 557 position = 0; 558 score = 0; 559 560 base = new Base(); 561 player = new Player(); 562 Spring = new spring(); 563 platform_broken_substitute = new Platform_broken_substitute(); 564 565 platforms = []; 566 for (var i = 0; i < platformCount; i++) { 567 platforms.push(new Platform()); 568 } 569} 570 571//Hides the menu 572function hideMenu() { 573 var menu = document.getElementById("mainMenu"); 574 menu.style.zIndex = -1; 575} 576 577//Shows the game over menu 578function showGoMenu() { 579 var menu = document.getElementById("gameOverMenu"); 580 menu.style.zIndex = 1; 581 menu.style.visibility = "visible"; 582 583 var scoreCount = document.getElementById("go_score"); 584 scoreCount.textContent = score; 585 586 var best_score = parseInt(localStorage['best_score']) || 0; 587 if (best_score < score) { 588 localStorage['best_score'] = score; 589 document.getElementById('go_best_score').textContent = score; 590 } 591} 592 593//Hides the game over menu 594function hideGoMenu() { 595 var menu = document.getElementById("gameOverMenu"); 596 menu.style.zIndex = -1; 597 menu.style.visibility = "hidden"; 598} 599 600//Show ScoreBoard 601function showScore() { 602 var menu = document.getElementById("scoreBoard"); 603 menu.style.zIndex = 1; 604} 605 606//Hide ScoreBoard 607function hideScore() { 608 var menu = document.getElementById("scoreBoard"); 609 menu.style.zIndex = -1; 610} 611 612function playerJump() { 613 player.y += player.vy; 614 player.vy += gravity; 615 616 if (player.vy > 0 && 617 (player.x + 15 < 260) && 618 (player.x + player.width - 15 > 155) && 619 (player.y + player.height > 475) && 620 (player.y + player.height < 500)) 621 player.jump(); 622 623 if (dir == "left") { 624 player.dir = "left"; 625 if (player.vy < -7 && player.vy > -15) player.dir = "left_land"; 626 } else if (dir == "right") { 627 player.dir = "right"; 628 if (player.vy < -7 && player.vy > -15) player.dir = "right_land"; 629 } 630 631 //Adding keyboard controls 632 document.onkeydown = function (e) { 633 var key = e.keyCode; 634 635 if (key == 37) { 636 dir = "left"; 637 player.isMovingLeft = true; 638 } else if (key == 39) { 639 dir = "right"; 640 player.isMovingRight = true; 641 } 642 643 if (key == 32) { 644 if (firstRun === true) { 645 init(); 646 firstRun = false;
647 } else { 648 reset(); 649 } 650 } 651 }; 652 653 document.onkeyup = function (e) { 654 var key = e.keyCode; 655 656 if (key == 37) { 657 dir = "left"; 658 player.isMovingLeft = false; 659 } else if (key == 39) { 660 dir = "right"; 661 player.isMovingRight = false; 662 } 663 }; 664 665 //Accelerations produces when the user hold the keys 666 if (player.isMovingLeft === true) { 667 player.x += player.vx; 668 player.vx -= 0.15; 669 } else { 670 player.x += player.vx; 671 if (player.vx < 0) player.vx += 0.1; 672 } 673 674 if (player.isMovingRight === true) { 675 player.x += player.vx; 676 player.vx += 0.15; 677 } else { 678 player.x += player.vx; 679 if (player.vx > 0) player.vx -= 0.1; 680 } 681 682 //Jump the player when it hits the base 683 if ((player.y + player.height) > base.y && base.y < height) player.jump(); 684 685 //Make the player move through walls 686 if (player.x > width) player.x = 0 - player.width; 687 else if (player.x < 0 - player.width) player.x = width; 688 689 player.draw(); 690} 691 692function update() { 693 ctx.clearRect(0, 0, width, height); 694 playerJump(); 695 // bing.play(); 696} 697 698menuLoop = function () { 699 update(); 700 requestAnimFrame(menuLoop); 701}; 702 703document.addEventListener("DOMContentLoaded", function (event) { 704 var localized_dom_content = document.querySelectorAll('[l_content]'); 705 // for (var i = 0; i < localized_dom_content.length; i++) { 706 // localized_dom_content[i].textContent = chrome.i18n.getMessage(localized_dom_content[i].getAttribute('l_content')); 707 // } 708 var best_score = parseInt(localStorage['best_score']) || 0; 709 document.getElementById('best_score').textContent = best_score; 710 document.getElementById('go_best_score').textContent = best_score; 711 menuLoop(); 712 713 document.getElementById('start_btn').addEventListener('click', function () { 714 init(); 715 }); 716 document.getElementById('restart_btn').addEventListener('click', function () { 717 reset(); 718 }); 719});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.