Advertisement
Guest User

Untitled

a guest
Sep 13th, 2015
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.94 KB | None | 0 0
  1. <!DOCTYPE HTML>
  2.  
  3. <html>
  4.     <head>
  5.         <title>Pastime</title>
  6.     </head>
  7.     <body>
  8.         <canvas id="canvas">This text is displaying if your browser is unable to display HTML5 canvas.</canvas>
  9.     </body>
  10.     <script>
  11.         var Box = function(x, y, direction)
  12.         {
  13.             this.x = x;
  14.             this.y = y;
  15.             this.direction = direction;
  16.         }
  17.        
  18.         var canvas = document.getElementById("canvas");
  19.         var ctx = canvas.getContext("2d");
  20.         var snake = [];
  21.         var head = new Box(100, 100, 1);
  22.         ctx.fillStyle = "red";
  23.        
  24.         snake.push(head);
  25.         //addBox();
  26.         //addBox();
  27.         //addBox();
  28.        
  29.        
  30.         Box.prototype.draw = function()
  31.         {
  32.             ctx.fillRect(this.x, this.y, 20, 20);
  33.         }
  34.        
  35.         function drawSnake()
  36.         {
  37.             for (var i = 0; i < snake.length; i++)
  38.             {
  39.                 switch (snake[i].direction)
  40.                 {
  41.                 case 0:
  42.                     snake[i].x -= 20;
  43.                     break;
  44.                 case 1:
  45.                     snake[i].y -= 20;
  46.                     break;
  47.                 case 2:
  48.                     snake[i].x += 20;
  49.                     break;
  50.                 case 3:
  51.                     snake[i].y += 20;
  52.                     break;
  53.                 }
  54.             snake[i].draw();
  55.             }
  56.         }
  57.        
  58.         function addBox()
  59.         {
  60.             var lastBox = snake[snake.length - 1];
  61.             var direction = lastBox.direction;
  62.             var x, y;
  63.             switch (direction)
  64.             {
  65.             case 0:
  66.                 x = lastBox.x + 20;
  67.                 y = lastBox.y;
  68.                 break;
  69.             case 1:
  70.                 x = lastBox.x;
  71.                 y = lastBox.y + 20;
  72.                 break;
  73.             case 2:
  74.                 x = lastBox.x - 20;
  75.                 y = lastBox.y;
  76.                 break;
  77.             case 3:
  78.                 x = lastBox.x;
  79.                 y = lastBox.y - 20;
  80.                 break;
  81.             }
  82.             snake.push(new Box(x, y, direction));
  83.         }
  84.        
  85.         document.addEventListener("keydown", function (event)
  86.         {
  87.             switch (event.keyCode)
  88.             {
  89.             case 37:
  90.                 head.direction = 0;
  91.                 break;
  92.             case 38:
  93.                 head.direction = 1;
  94.                 break;
  95.             case 39:
  96.                 head.direction = 2;
  97.                 break;
  98.             case 40:
  99.                 head.direction = 3:
  100.                 break;
  101.             }
  102.         });
  103.        
  104.         function gameLoop()
  105.         {
  106.             setTimeout("gameLoop()", 100);
  107.             drawSnake();
  108.         }
  109.        
  110.        
  111.         head.draw();
  112.         gameLoop();
  113.     </script>
  114. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement