Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
239
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="300" style="border:1px 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.  
  11.  
  12. var x1corner =0.0;
  13. var y1corner=0.0;
  14. var change = 5.0;
  15. console.log(document);
  16. var c = document.getElementById("myCanvas");
  17. console.log(c);
  18. var ctx = c.getContext("2d");
  19. ctx.fillStyle = "#FF0000";
  20. var gravity = 0.8;
  21. var momentum = 0;
  22. var rect1 = {x: 100, y: 200, width: 20, height: 20};
  23. var rect2 = {x: 140, y: 160, width: 20, height: 20};
  24. var rect3 = {x: 170, y: 120, width: 20, height: 20};
  25.  
  26. var xchange = 1.0;
  27. var ychange = 70.0;
  28.  
  29. var obstacles = [rect1,rect2,rect3];
  30. var collision = false;
  31. var keys = [];
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. function drawrect(x1,y1){
  39. ctx.fillRect(x1,y1,20,20);
  40. }
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48. ///this is the function for the keypress event
  49. function ProcessKeydown(e)
  50. {
  51.     //alert(e.keyCode);
  52.    
  53.     if (e.keyCode) keycode=e.keyCode;
  54.     else keycode=e.which;
  55.     ch=String.fromCharCode(keycode);
  56.    /*
  57.     if(keycode==65)         {
  58.        
  59.         x1corner -= xchange;
  60.         momentum = -0.2;
  61.     }
  62.     else if(keycode==68)    {
  63.         x1corner += xchange;
  64.         momentum = 0.2;
  65.     }
  66.     else if(keycode==83)    {
  67.         y1corner += ychange;
  68.     }
  69.     else if(keycode==87)    {
  70.       if (y1corner == 230 || collision == true)
  71.       {
  72.         y1corner -= ychange;
  73.       }
  74.        
  75.     }
  76.    
  77.     */
  78.     ctx.clearRect(0, 0, myCanvas.width, myCanvas.height)
  79.     ctx.fillStyle = "#FF0000";
  80.     drawrect(x1corner,y1corner);
  81.     ctx.fillStyle = "#000000";  
  82.  
  83.     ctx.fillRect(0,250,500,50);
  84.     //update();
  85.     //draw();
  86. }
  87.  
  88. setInterval(function()
  89.             {
  90.  
  91.   if (keys[68]) {
  92.     console.log("right down");
  93.         x1corner += xchange;
  94.         //momentum = 0.2;
  95.     }
  96.   if (keys[65]) {
  97.     console.log("left down");
  98.         x1corner -= xchange;
  99.         //momentum = -0.2;
  100.     }
  101.   if (keys[87]) {
  102.     console.log("up key down " + y1corner);
  103.     if ((y1corner < 230.5 && y1corner > 229.5) || collision == true)
  104.       {
  105.                  var i1 = 0;
  106.                  setInterval(function()
  107.                 {
  108.                     if (i1 < 10){
  109.                         y1corner -= 2;
  110.                         i1 += 1;
  111.                     }
  112.                 },10);
  113.               }
  114.              
  115.        
  116.        
  117.     }
  118.   if (keys[83]) {
  119.     console.log("down key down");
  120.         y1corner += ychange;
  121.        
  122.     }
  123.               for   (index = 0; index < obstacles.length;                   index++){
  124.               rectx = obstacles[index].x;
  125.               //console.log(rectx);
  126.               recty = obstacles[index].y;
  127.               if (x1corner < rectx + 20 &&
  128.               x1corner > rectx - 20 &&
  129.               y1corner > recty - 20 &&
  130.               y1corner < recty) {
  131.               // collision detected!
  132.               collision = true;
  133.               break;
  134.             }
  135.               else{collision = false;}
  136.               }
  137.  
  138.               if (y1corner < 230 && collision == false){
  139.              
  140.                
  141.               y1corner = y1corner + gravity;
  142.               ctx.clearRect(0, 0, myCanvas.width, myCanvas.height)
  143.              
  144.               ctx.fillStyle = "#000000";  
  145.  
  146.               ctx.fillRect(0,250,500,50);
  147.  
  148.               }
  149.               if (x1corner > -1 && x1corner < 480){
  150.               x1corner = x1corner + momentum;
  151.               ctx.clearRect(0, 0, myCanvas.width, 250)
  152.               ctx.fillStyle = "#FF0000";
  153.               drawrect(x1corner,y1corner);
  154.               ctx.fillStyle = "#000000";
  155.                 for (index = 0; index < obstacles.length;                     index++){
  156.                   drawrect(obstacles[index].x,
  157.                        obstacles[index].y);
  158.              
  159.                 }
  160.                
  161.               }
  162.  
  163.             }, 1);
  164.  
  165.  
  166. document.body.addEventListener("keydown", function (e) {
  167.     keys[e.keyCode] = true;
  168. });
  169. document.body.addEventListener("keyup", function (e) {
  170.     keys[e.keyCode] = false;
  171. });
  172. </script>
  173. <!--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
  174. <body onKeyDown="ProcessKeydown(event);">
  175. -->
  176.  
  177. </body>
  178.  
  179.  
  180. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement