Guest User

Snake ChatGPT

a guest
Sep 20th, 2023
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Snake Game</title>
  5. <style>
  6. svg {
  7. background-color: #222;
  8. }
  9.  
  10. .snake {
  11. fill: #39FF14;
  12. stroke: #000;
  13. stroke-width: 2;
  14. }
  15.  
  16. .food {
  17. fill: #FF6347;
  18. stroke: #000;
  19. stroke-width: 2;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <svg id="game" width="400" height="400"></svg>
  25.  
  26. <script>
  27. const svg = document.getElementById('game');
  28. const cellSize = 20;
  29. const gridSize = 20;
  30. const snakeSpeed = 150;
  31.  
  32. const snake = [{ x: 5, y: 5 }];
  33. let direction = 'right';
  34. let food = getRandomPosition();
  35.  
  36. function getRandomPosition() {
  37. const x = Math.floor(Math.random() * gridSize);
  38. const y = Math.floor(Math.random() * gridSize);
  39. return { x, y };
  40. }
  41.  
  42. function draw() {
  43. // Clear the SVG
  44. svg.innerHTML = '';
  45.  
  46. // Draw food
  47. const foodElement = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
  48. foodElement.setAttribute('class', 'food');
  49. foodElement.setAttribute('width', cellSize);
  50. foodElement.setAttribute('height', cellSize);
  51. foodElement.setAttribute('x', food.x * cellSize);
  52. foodElement.setAttribute('y', food.y * cellSize);
  53. svg.appendChild(foodElement);
  54.  
  55. // Draw snake
  56. for (const segment of snake) {
  57. const snakeSegment = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
  58. snakeSegment.setAttribute('class', 'snake');
  59. snakeSegment.setAttribute('width', cellSize);
  60. snakeSegment.setAttribute('height', cellSize);
  61. snakeSegment.setAttribute('x', segment.x * cellSize);
  62. snakeSegment.setAttribute('y', segment.y * cellSize);
  63. svg.appendChild(snakeSegment);
  64. }
  65. }
  66.  
  67. function move() {
  68. // Move the snake
  69. const headX = snake[0].x;
  70. const headY = snake[0].y;
  71.  
  72. switch (direction) {
  73. case 'up':
  74. snake.unshift({ x: headX, y: headY - 1 });
  75. break;
  76. case 'down':
  77. snake.unshift({ x: headX, y: headY + 1 });
  78. break;
  79. case 'left':
  80. snake.unshift({ x: headX - 1, y: headY });
  81. break;
  82. case 'right':
  83. snake.unshift({ x: headX + 1, y: headY });
  84. break;
  85. }
  86.  
  87. // Check for collision with food
  88. if (snake[0].x === food.x && snake[0].y === food.y) {
  89. food = getRandomPosition();
  90. } else {
  91. // Remove the tail
  92. snake.pop();
  93. }
  94. }
  95.  
  96. function gameOver() {
  97. clearInterval(gameInterval);
  98. alert('Game Over! Your score: ' + (snake.length - 1));
  99. document.location.reload();
  100. }
  101.  
  102. function checkCollision() {
  103. // Check for collision with walls
  104. const headX = snake[0].x;
  105. const headY = snake[0].y;
  106. if (headX < 0 || headY < 0 || headX >= gridSize || headY >= gridSize) {
  107. gameOver();
  108. return;
  109. }
  110.  
  111. // Check for collision with itself
  112. for (let i = 1; i < snake.length; i++) {
  113. if (snake[i].x === headX && snake[i].y === headY) {
  114. gameOver();
  115. return;
  116. }
  117. }
  118. }
  119.  
  120. function gameLoop() {
  121. move();
  122. checkCollision();
  123. draw();
  124. }
  125.  
  126. document.addEventListener('keydown', (event) => {
  127. switch (event.key) {
  128. case 'ArrowUp':
  129. if (direction !== 'down') {
  130. direction = 'up';
  131. }
  132. break;
  133. case 'ArrowDown':
  134. if (direction !== 'up') {
  135. direction = 'down';
  136. }
  137. break;
  138. case 'ArrowLeft':
  139. if (direction !== 'right') {
  140. direction = 'left';
  141. }
  142. break;
  143. case 'ArrowRight':
  144. if (direction !== 'left') {
  145. direction = 'right';
  146. }
  147. break;
  148. }
  149. });
  150.  
  151. draw();
  152. const gameInterval = setInterval(gameLoop, snakeSpeed);
  153. </script>
  154. </body>
  155. </html>
  156.  
Advertisement
Add Comment
Please, Sign In to add comment