Daniel4773

Snake game embedded code:

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