Advertisement
FlyFar

Snake Xenzia In HTML with UNDER 100 lines of codes

Oct 22nd, 2021
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 4.00 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <title></title>
  5.   <style>
  6.   html, body {
  7.     height: 100%;
  8.     margin: 0;
  9.   }
  10.  
  11.   body {
  12.     background: black;
  13.     display: flex;
  14.     align-items: center;
  15.     justify-content: center;
  16.   }
  17.   canvas {
  18.     border: 1px solid white;
  19.   }
  20.   </style>
  21. </head>
  22. <body>
  23. <canvas width="400" height="400" id="game"></canvas>
  24. <script>
  25. var canvas = document.getElementById('game');
  26. var context = canvas.getContext('2d');
  27.  
  28. var grid = 16;
  29. var count = 0;
  30.  
  31. var snake = {
  32.   x: 160,
  33.   y: 160,
  34.  
  35.   // snake velocity. moves one grid length every frame in either the x or y direction
  36.   dx: grid,
  37.   dy: 0,
  38.  
  39.   // keep track of all grids the snake body occupies
  40.   cells: [],
  41.  
  42.   // length of the snake. grows when eating an apple
  43.   maxCells: 4
  44. };
  45. var apple = {
  46.   x: 320,
  47.   y: 320
  48. };
  49.  
  50. // get random whole numbers in a specific range
  51. // @see https://stackoverflow.com/a/1527820/2124254
  52. function getRandomInt(min, max) {
  53.   return Math.floor(Math.random() * (max - min)) + min;
  54. }
  55.  
  56. // game loop
  57. function loop() {
  58.   requestAnimationFrame(loop);
  59.  
  60.   // slow game loop to 15 fps instead of 60 (60/15 = 4)
  61.   if (++count < 4) {
  62.    return;
  63.  }
  64.  
  65.  count = 0;
  66.  context.clearRect(0,0,canvas.width,canvas.height);
  67.  
  68.  // move snake by it's velocity
  69.  snake.x += snake.dx;
  70.  snake.y += snake.dy;
  71.  // wrap snake position horizontally on edge of screen
  72.  if (snake.x < 0) {
  73.    snake.x = canvas.width - grid;
  74.  }
  75.  else if (snake.x >= canvas.width) {
  76.     snake.x = 0;
  77.   }
  78.  
  79.   // wrap snake position vertically on edge of screen
  80.   if (snake.y < 0) {
  81.    snake.y = canvas.height - grid;
  82.  }
  83.  else if (snake.y >= canvas.height) {
  84.     snake.y = 0;
  85.   }
  86.  
  87.   // keep track of where snake has been. front of the array is always the head
  88.   snake.cells.unshift({x: snake.x, y: snake.y});
  89.  
  90.   // remove cells as we move away from them
  91.   if (snake.cells.length > snake.maxCells) {
  92.     snake.cells.pop();
  93.   }
  94.  
  95.   // draw apple
  96.   context.fillStyle = 'red';
  97.   context.fillRect(apple.x, apple.y, grid-1, grid-1);
  98.  
  99.   // draw snake one cell at a time
  100.   context.fillStyle = 'green';
  101.   snake.cells.forEach(function(cell, index) {
  102.    
  103.     // drawing 1 px smaller than the grid creates a grid effect in the snake body so you can see how long it is
  104.     context.fillRect(cell.x, cell.y, grid-1, grid-1);  
  105.  
  106.     // snake ate apple
  107.     if (cell.x === apple.x && cell.y === apple.y) {
  108.      snake.maxCells++;
  109.  
  110.       // canvas is 400x400 which is 25x25 grids
  111.       apple.x = getRandomInt(0, 25) * grid;
  112.       apple.y = getRandomInt(0, 25) * grid;
  113.     }
  114.  
  115.     // check collision with all cells after this one (modified bubble sort)
  116.     for (var i = index + 1; i < snake.cells.length; i++) {
  117.      
  118.      // snake occupies same space as a body part. reset game
  119.      if (cell.x === snake.cells[i].x && cell.y === snake.cells[i].y) {
  120.        snake.x = 160;
  121.        snake.y = 160;
  122.        snake.cells = [];
  123.        snake.maxCells = 4;
  124.        snake.dx = grid;
  125.        snake.dy = 0;
  126.  
  127.        apple.x = getRandomInt(0, 25) * grid;
  128.        apple.y = getRandomInt(0, 25) * grid;
  129.      }
  130.    }
  131.  });
  132. }
  133.  
  134. // listen to keyboard events to move the snake
  135. document.addEventListener('keydown', function(e) {
  136.  // prevent snake from backtracking on itself by checking that it's
  137.  // not already moving on the same axis (pressing left while moving
  138.  // left won't do anything, and pressing right while moving left
  139.  // shouldn't let you collide with your own body)
  140.  
  141.  // left arrow key
  142.  if (e.which === 37 && snake.dx === 0) {
  143.    snake.dx = -grid;
  144.    snake.dy = 0;
  145.  }
  146.  // up arrow key
  147.  else if (e.which === 38 && snake.dy === 0) {
  148.    snake.dy = -grid;
  149.    snake.dx = 0;
  150.  }
  151.  // right arrow key
  152.  else if (e.which === 39 && snake.dx === 0) {
  153.    snake.dx = grid;
  154.    snake.dy = 0;
  155.  }
  156.  // down arrow key
  157.  else if (e.which === 40 && snake.dy === 0) {
  158.    snake.dy = grid;
  159.    snake.dx = 0;
  160.  }
  161. });
  162. // start the game
  163. requestAnimationFrame(loop);
  164. </script>
  165. </body>
  166. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement