Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <canvas id="myCanvas" width="500" height="500" style="border:5px solid #c3c3c3;">
  5. Your browser does not support the HTML5 canvas tag.
  6. </canvas>
  7. <!-- this command below tells the browser to start interpreting the lines below
  8. javascript and not html -->
  9. <script>
  10. var audio = new Audio('http://www.freesfx.co.uk/rx2/mp3s/9/10064_1361272689.mp3');
  11.   var audio2 = new Audio('http://www.freesfx.co.uk/rx2/mp3s/4/5080_1333456140.mp3');
  12.  
  13.     var audio3 = new Audio('http://www.freesfx.co.uk/rx2/mp3s/9/10385_1371750888.mp3');
  14.  
  15. audio3.play();
  16.  
  17. var x1corner =250;
  18. var y1corner=0.0;
  19. var change = 5.0;
  20. console.log(document);
  21. var c = document.getElementById("myCanvas");
  22. console.log(c);
  23. var ctx = c.getContext("2d");
  24. ctx.fillStyle = "#FF0000";
  25. var gravity = 0.8;
  26. var momentum = 0;
  27. var rect1 = {x: 100, y: 200, width: 20, height: 20};
  28. var rect2 = {x: 200, y: 160, width: 20, height: 20};
  29. var rect5 = {x: 200, y: 20, width: 20, height: 20};
  30. var rect3 = {x: 300, y: 120, width: 20, height: 20};
  31. var rect4 = {x: 300, y: 40, width: 20, height: 20};
  32.  
  33. var xchange = 1.0;
  34. var ychange = 70.0;
  35.  
  36. var obstacles = [rect1,rect2,rect3,rect4,rect5];
  37.  
  38.  
  39.  
  40. for (i = 4; i < 32; i++){
  41. var A = Object.create(obstacles[i]);
  42. obstacles.push(A);
  43. obstacles[i+1].x = i*100;
  44. obstacles[i+1].y = Math.random()*220;
  45. }
  46.  
  47.  
  48. var collision = false;
  49. var keys = [];
  50. var win = false;
  51. var scrollx = 10;
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. function drawrect(x1,y1){
  59. ctx.fillRect(x1,y1,20,20);
  60. }
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68. ///this is the function for the keypress event
  69. function ProcessKeydown(e)
  70. {
  71.     //alert(e.keyCode);
  72.    
  73.     if (e.keyCode) keycode=e.keyCode;
  74.     else keycode=e.which;
  75.     ch=String.fromCharCode(keycode);
  76.    /*
  77.     if(keycode==65)         {
  78.        
  79.         x1corner -= xchange;
  80.         momentum = -0.2;
  81.     }
  82.     else if(keycode==68)    {
  83.         x1corner += xchange;
  84.         momentum = 0.2;
  85.     }
  86.     else if(keycode==83)    {
  87.         y1corner += ychange;
  88.     }
  89.     else if(keycode==87)    {
  90.       if (y1corner == 230 || collision == true)
  91.       {
  92.         y1corner -= ychange;
  93.       }
  94.        
  95.     }
  96.    
  97.     */
  98.     ctx.clearRect(0, 0, myCanvas.width, myCanvas.height)
  99.     ctx.fillStyle = "#FF0000";
  100.     drawrect(x1corner,y1corner);
  101.     ctx.fillStyle = "#000000";  
  102.  
  103.     ctx.fillRect(0,250,500,50);
  104.     //update();
  105.     //draw();
  106. }
  107.  
  108. setInterval(function()
  109.             {
  110.  
  111.  
  112.   if (x1corner < 230 + 30 &&
  113.               x1corner > 230 - 30 &&
  114.               y1corner < -10)  {
  115.               //alert("you win");
  116.               win = true;
  117.               audio2.play();
  118.   }
  119.  
  120.   if (keys[68] || keys[39]) {
  121.     console.log("right down");
  122.    
  123.         for (index = 0; index < obstacles.length;                       index++){
  124.               obstacles[index].x -= xchange;
  125.               //console.log(rectx);
  126.               //recty = obstacles[index].y;
  127.         }
  128.         //x1corner += xchange;
  129.         //momentum = 0.2;
  130.     }
  131.   if (keys[65] || keys[37]) {
  132.     console.log("left down");
  133.         //x1corner -= xchange;
  134.     for (index = 0; index < obstacles.length;                       index++){
  135.               obstacles[index].x += xchange;
  136.               //console.log(rectx);
  137.               //recty = obstacles[index].y;
  138.         }
  139.         //momentum = -0.2;
  140.     }
  141.   if (keys[87]|| keys[38]) {
  142.     audio.play();
  143.     console.log("up key down " + y1corner);
  144.     if ((y1corner < 230.5 && y1corner > 229.5) || collision == true)
  145.       {
  146.                  var i1 = 0;
  147.                  setInterval(function()
  148.                 {
  149.                     if (i1 < 5){
  150.                       for   (index = 0; index < obstacles.length;                       index++){
  151.               obstacles[index].y += 1;
  152.               //console.log(rectx);
  153.               //recty = obstacles[index].y;
  154.         }
  155.                         //y1corner -= 2;
  156.                         i1 += 1;
  157.                     }
  158.                 },10);
  159.               }
  160.              
  161.        
  162.        
  163.     }
  164.   if (keys[83] || keys[40]) {
  165.     console.log("down key down");
  166.         y1corner += ychange;
  167.        
  168.     }
  169.               for   (index = 0; index < obstacles.length;                   index++){
  170.               rectx = obstacles[index].x;
  171.               //console.log(rectx);
  172.               recty = obstacles[index].y;
  173.               if (x1corner < rectx + 20 &&
  174.               x1corner > rectx - 20 &&
  175.               y1corner > recty - 20 &&
  176.               y1corner < recty) {
  177.               // collision detected!
  178.               collision = true;
  179.               break;
  180.             }
  181.               else{collision = false;}
  182.               }
  183.  
  184.               if (y1corner < 230 && collision == false){
  185.              
  186.                
  187.               y1corner = y1corner + gravity;
  188.               ctx.clearRect(0, 0, myCanvas.width, myCanvas.height)
  189.              
  190.              
  191.               //ctx.fillStyle = "#000000";  
  192.  
  193.               //ctx.fillRect(0,250,500,50);
  194.  
  195.               }
  196.               if (x1corner > -1 && x1corner < 480){
  197.               x1corner = x1corner + momentum;
  198.               ctx.clearRect(0, 0, myCanvas.width, 250)
  199.               if (win == false){
  200.                       ctx.fillStyle = "#FF0000";
  201.                
  202.               }
  203.                 else{ctx.fillStyle = "rgb(" +  Math.round(Math.random()*255).toString() + "," +
  204. Math.round(Math.random()*255).toString() + "," +
  205. Math.round(Math.random()*255).toString() +
  206. ")" ;        }
  207.               drawrect(x1corner,y1corner);
  208.               ctx.fillStyle = "#000000";
  209.                 for (index = 0; index < obstacles.length;                     index++){
  210.                   drawrect(obstacles[index].x,
  211.                        obstacles[index].y);
  212.              
  213.                 }
  214.                
  215.               }
  216.               ctx.font='small-caps 20px Arial';
  217.              
  218.               if (win == false){
  219.                
  220.               ctx.fillText("What's above here?->",5,35);
  221.                 ctx.fillText("W,A,D to move",5,55);
  222.                 ctx.fillText(x1corner + " " + Math.round(y1corner),10,75);
  223.               }
  224.               else
  225.                {ctx.fillText("You win!",10,35);}
  226.  
  227.             }, 1);
  228.  
  229.  
  230. document.body.addEventListener("keydown", function (e) {
  231.     keys[e.keyCode] = true;
  232. });
  233. document.body.addEventListener("keyup", function (e) {
  234.     keys[e.keyCode] = false;
  235. });
  236. </script>
  237. <!--the script below accesses one of the DOM built in functions which is onKeyPress.  You can look from the watch window and see many other ones
  238. <body onKeyDown="ProcessKeydown(event);">
  239. -->
  240.  
  241. </body>
  242.  
  243.  
  244. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement