Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  7. <title>Snake</title>
  8. <style>
  9. html,
  10. body {
  11. height: 100%;
  12. margin: 0;
  13. }
  14.  
  15. body {
  16. background: black;
  17. display: flex;
  18. align-items: center;
  19. justify-content: center;
  20. }
  21.  
  22. canvas {
  23. border: 1px solid white;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <canvas width="400" height="400" id="game"></canvas>
  29.  
  30. <script>
  31. var canvas = document.getElementById("game");
  32. var context = canvas.getContext("2d");
  33.  
  34. var grid = 16;
  35. var count = 0;
  36.  
  37. var snake = {
  38. x: 160,
  39. y: 160,
  40.  
  41. // snake velocity. Moves one grid length every frame in either x or y direction
  42. dx: grid,
  43. dy: 0,
  44.  
  45. //keep track of all grids the snake body occupies
  46. cells: [],
  47.  
  48. // length of the snake. Which will change after eating an apple.
  49. maxCells: 4
  50. };
  51.  
  52. var apple = {
  53. x: 320,
  54. y: 320
  55. };
  56.  
  57. //get random whole numbers in a specific range
  58. function getRandomInt(min, max) {
  59. return Math.floor(Math.random() * (max - min)) + min;
  60. }
  61.  
  62. function loop() {
  63. requestAnimationFrame(loop);
  64.  
  65. if (++count < 4) {
  66. return;
  67. }
  68.  
  69. count = 0;
  70. context.clearRect(0, 0, canvas.width, canvas.height);
  71.  
  72. // move snake by it's velocity
  73. snake.x += snake.dx;
  74. snake.y += snake.dy;
  75.  
  76. //wrap snake position horizontally on edge of screen
  77. if (snake.x < 0) {
  78. snake.x = canvas.width - grid;
  79. } else if (snake.x >= canvas.width) {
  80. snake.x = 0;
  81. }
  82.  
  83. //wrap snake position vertically on edge of screen
  84. if (snake.y < 0) {
  85. snake.y = canvas.height - grid;
  86. } else if (snake.y >= canvas.height) {
  87. snake.y = 0;
  88. }
  89.  
  90. //keep track of where snake has been. The front of the array is always the head of the snake
  91. snake.cells.unshift({ x: snake.x, y: snake.y });
  92.  
  93. //remove cells as we move away from them
  94. if (snake.cells.length > snake.maxCells) {
  95. snake.cells.pop();
  96. }
  97.  
  98. //draw apple
  99. context.fillStyle = "red";
  100. context.fillRect(apple.x, apple.y, grid - 1, grid - 1);
  101.  
  102. //draw snake
  103. context.fillStyle = "green";
  104. snake.cells.forEach(function(cell, index) {
  105. //drawing 1 px smaller than grid creates a grid effect in the snake body
  106. context.fillRect(cell.x, cell.y, grid - 1, grid - 1);
  107.  
  108. //if snake ate an apple
  109. if (cell.x === apple.x && cell.y === apple.y) {
  110. snake.maxCells++;
  111.  
  112. apple.x = getRandomInt(0, 25) * grid;
  113. apple.y = getRandomInt(0, 25) * grid;
  114. }
  115.  
  116. for (var i = index + 1; i < snake.cells.length; i++) {
  117. if (cell.x === snake.cells[i].x && cell.y === snake.cells[i].y) {
  118. snake.x = 160;
  119. snake.y = 160;
  120. snake.cells = [];
  121. snake.maxCells = 4;
  122. snake.dx = grid;
  123. snake.dy = 0;
  124.  
  125. apple.x = getRandomInt(0, 25) * grid;
  126. apple.y = getRandomInt(0, 25) * grid;
  127. }
  128. }
  129. });
  130. }
  131. //listens to keyboard events to move snake
  132. document.addEventListener("keydown", function(e) {
  133. //left arrow key
  134. if (e.which === 37 && snake.dx === 0) {
  135. snake.dx = -grid;
  136. snake.dy = 0;
  137. }
  138.  
  139. //up arrow
  140. else if (e.which === 38 && snake.dy === 0) {
  141. snake.dy = -grid;
  142. snake.dx = 0;
  143. }
  144.  
  145. //right arrow
  146. else if (e.which === 39 && snake.dx === 0) {
  147. snake.dx = grid;
  148. snake.dy = 0;
  149. }
  150.  
  151. //down arrow
  152. else if (e.which === 40 && snake.dy === 0) {
  153. snake.dy = grid;
  154. snake.dx = 0;
  155. }
  156. });
  157.  
  158. //start game
  159. requestAnimationFrame(loop);
  160. </script>
  161. </body>
  162. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement