Advertisement
Guest User

yes3333

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