Advertisement
plattina

Snake Web

Jul 22nd, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.80 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5.     <title>Snake Game</title>
  6. </head>
  7.  
  8. <body>
  9.     <canvas id="stage" width="600" height="600"></canvas>
  10.     <script type="text/javascript">
  11.         window.onload = function () {
  12.             var stage = document.getElementById('stage');
  13.             var context = stage.getContext("2d");
  14.             document.addEventListener("keydown", keyPush);
  15.             setInterval(game, 80);
  16.  
  17.             const vel = 1;
  18.  
  19.             var speedX = speedY = 0;
  20.             var startPointX = 10;
  21.             var startPointY = 15;
  22.             var lenghtPiece = 30;
  23.             var tileCount = 20;
  24.             var appleX = appleY = 15;
  25.  
  26.             var trail = [];
  27.             tail = 5;
  28.  
  29.             function game() {
  30.                 move();
  31.  
  32.                 teleportPosition();
  33.  
  34.                 drawBacktround();
  35.  
  36.                 drawApple();
  37.  
  38.                 drawSnake();
  39.  
  40.                 eat();
  41.             }
  42.  
  43.             function drawBacktround() {
  44.                 context.fillStyle = "black";
  45.                 context.fillRect(0, 0, stage.width, stage.height);
  46.             }
  47.  
  48.             function drawApple() {
  49.                 context.fillStyle = "red";
  50.                 context.fillRect(appleX * lenghtPiece, appleY * lenghtPiece, lenghtPiece, lenghtPiece);
  51.             }
  52.  
  53.             function drawSnake() {
  54.                 context.fillStyle = "gray";
  55.                 for (var i = 0; i < trail.length; i++) {
  56.                    context.fillRect(trail[i].x * lenghtPiece, trail[i].y * lenghtPiece, lenghtPiece - 1, lenghtPiece - 1);
  57.                    if (trail[i].x == startPointX && trail[i].y == startPointY) {
  58.                        gameOver();
  59.                    }
  60.                }
  61.  
  62.                trail.push({ x: startPointX, y: startPointY });
  63.                while (trail.length > tail) {
  64.                     trail.shift();
  65.                 }
  66.             }
  67.  
  68.             function eat() {
  69.                 if (appleX == startPointX && appleY == startPointY) {
  70.                    tail++;
  71.                     generateApple();
  72.                 }
  73.             }
  74.  
  75.             function generateApple() {
  76.                 appleX = Math.floor(Math.random() * tileCount);
  77.                 appleY = Math.floor(Math.random() * tileCount);
  78.             }
  79.  
  80.             function move() {
  81.                 startPointX += speedX;
  82.                 startPointY += speedY;
  83.             }
  84.  
  85.             function teleportPosition() {
  86.                 if (startPointX < 0) {
  87.                    startPointX = tileCount - 1;
  88.                }
  89.                if (startPointX > tileCount - 1) {
  90.                     startPointX = 0;
  91.                 }
  92.                 if (startPointY < 0) {
  93.                    startPointY = tileCount - 1;
  94.                }
  95.                if (startPointY > tileCount - 1) {
  96.                     startPointY = 0;
  97.                 }
  98.             }
  99.  
  100.             function gameOver() {
  101.                 speedX = speedY = 0;
  102.                 tail = 5;
  103.             }
  104.  
  105.             function keyPush(event) {
  106.                 switch (event.keyCode) {
  107.                     case 37: // Left
  108.                         speedX = -vel;
  109.                         speedY = 0;
  110.                         break;
  111.                     case 38: // up
  112.                         speedX = 0;
  113.                         speedY = -vel;
  114.                         break;
  115.                     case 39: // right
  116.                         speedX = vel;
  117.                         speedY = 0;
  118.                         break;
  119.                     case 40: // down
  120.                         speedX = 0;
  121.                         speedY = vel;
  122.                         break;
  123.                     default:
  124.  
  125.                         break;
  126.                 }
  127.             }
  128.         }
  129.  
  130.     </script>
  131. </body>
  132.  
  133. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement