Advertisement
Transformator

Runner

Apr 25th, 2015
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE HTML>
  2. <head>
  3.     <title>Runner</title>
  4.     <meta charset="utf-8">
  5.  
  6.     <style>
  7.     canvas {
  8.         margin: auto;
  9.         position: absolute;
  10.  
  11.         top: 0;
  12.         bottom: 0;
  13.         left: 0;
  14.         right: 0;
  15.  
  16.         border: 2px solid #222;
  17.     }
  18.     </style>
  19. </head>
  20. <body>
  21.  
  22. <img src="the_run0.png" id="img0" hidden>
  23. <img src="the_run1.png" id="img1" hidden>
  24. <img src="the_run2.png" id="img2" hidden>
  25. <img src="the_run3.png" id="img3" hidden>
  26. <img src="the_run4.png" id="img4" hidden>
  27. <img src="the_run5.png" id="img5" hidden>
  28. <img src="the_run6.png" id="img6" hidden>
  29.  
  30. <script>
  31.  
  32. var WIDTH, HEIGHT;
  33.  
  34. var canvas, ctx, keystate, player_status, player_pos, t0, t1;
  35.  
  36. var img0, img1, img2, img3, img4, img5, img6, vel;
  37.  
  38. var b_jump, on_air, hitboxes;
  39.  
  40. function main() {
  41.     init();
  42.  
  43.     canvas = document.createElement("canvas");
  44.     canvas.width = WIDTH;
  45.     canvas.height = HEIGHT;
  46.     ctx = canvas.getContext("2d");
  47.     document.body.appendChild(canvas);
  48.  
  49.     document.addEventListener("keydown", function(evt) {
  50.         keystate[evt.keyCode] = true;
  51.     });
  52.  
  53.     var loop = function() {
  54.         update();
  55.         draw();
  56.  
  57.         requestAnimationFrame(loop);
  58.     }
  59.  
  60.     requestAnimationFrame(loop);
  61. }
  62.  
  63. function init() {
  64.     keystate = {};
  65.     hitboxes = [];
  66.  
  67.     // regular hitboxes
  68.     hitboxes.push([21, 308, 0]);
  69.    
  70.     // normal hitboxes
  71.     hitboxes.push([300,  290, 200]);
  72.     hitboxes.push([600,  270, 200]);
  73.  
  74.     WIDTH = 1200;
  75.     HEIGHT = 400;
  76.  
  77.     player_status = 0;
  78.     vel = 0;
  79.  
  80.     on_air = false;
  81.  
  82.     player_pos = HEIGHT-92;
  83.     b_jump = player_pos;
  84.  
  85.     img0=document.getElementById("img0");
  86.     img1=document.getElementById("img1");
  87.     img2=document.getElementById("img2");
  88.     img3=document.getElementById("img3");
  89.     img4=document.getElementById("img4");
  90.     img5=document.getElementById("img5");
  91.     img6=document.getElementById("img6");
  92.  
  93.     t0 = window.performance.now();
  94. }
  95.  
  96. function update() {
  97.     t1 = window.performance.now();
  98.  
  99.     for(var i=1; i<hitboxes.length; i++) {
  100.         hitboxes[i][0] -= 2;
  101.     }
  102.  
  103.     if(t1-t0 > 50) {
  104.         player_status++;
  105.         if(player_status > 6)
  106.             player_status = 0;
  107.  
  108.         if(keystate[32] || keystate[38]) {
  109.             if(!on_air) {
  110.                 vel = 1;
  111.                 b_jump = player_pos;
  112.                 on_air = true;
  113.             }
  114.         }
  115.  
  116.         keystate = {};
  117.  
  118.         var hbx = false;
  119.  
  120.         for(var i=1; i<hitboxes.length; i++) {
  121.             if(hitboxes[i][0] < 30 && hitboxes[i][1] > player_pos-5 && hitboxes[i][0]+hitboxes[i][2] > 0) {
  122.                 b_jump = hitboxes[i][1];
  123.                 hbx = true;
  124.             }
  125.         }
  126.  
  127.         if(!hbx) {
  128.             b_jump = HEIGHT-92;
  129.         }
  130.  
  131.         if(vel == 1) {
  132.             player_pos -= 15;
  133.  
  134.             if(player_pos < b_jump-150) {
  135.                 vel = 0;
  136.             }
  137.         } else {
  138.             if(player_pos < b_jump)
  139.                 player_pos += 15;
  140.             else
  141.                 on_air = false;
  142.         }
  143.  
  144.         for(var i=1; i<hitboxes.length; i++) {
  145.             if(hitboxes[i][0]+hitboxes[i][2] < 0) {
  146.                 hitboxes.splice(i-1, i);
  147.                 hitboxes.push([1200, 290, 200]);
  148.             }
  149.         }
  150.  
  151.         t0 = window.performance.now();
  152.     }
  153. }
  154.  
  155. function draw() {
  156.     ctx.fillStyle = "#FFF";
  157.     ctx.fillRect(0, 0, WIDTH, HEIGHT);
  158.  
  159.     switch(player_status) {
  160.         case(0):
  161.         ctx.drawImage(img0,0,player_pos);
  162.             break;
  163.         case(1):
  164.             ctx.drawImage(img1,0,player_pos);
  165.             break;
  166.         case(2):
  167.             ctx.drawImage(img2,0,player_pos);
  168.             break;
  169.         case(3):
  170.             ctx.drawImage(img3,0,player_pos);
  171.             break;
  172.         case(4):
  173.             ctx.drawImage(img4,0,player_pos);
  174.             break;
  175.         case(5):
  176.             ctx.drawImage(img5,0,player_pos);
  177.             break;
  178.         case(6):
  179.             ctx.drawImage(img6,0,player_pos);
  180.             break;
  181.     }
  182.  
  183.     for(var i=1; i<hitboxes.length; i++) {
  184.         ctx.fillStyle = "#F00";
  185.         ctx.fillRect(hitboxes[i][0], hitboxes[i][1]+92, hitboxes[i][2], 20);
  186.     }
  187. }
  188.  
  189. main();
  190. </script>
  191. </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement