edaross

Snake game with ants via ChatGPT 2023 13 01

Jan 13th, 2023
505
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>🐜 Snake Game</title>
  5. <style>
  6. #game-canvas {
  7. border: 1px solid black;
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <canvas id="game-canvas" width="400" height="400"></canvas>
  13. <script>
  14. // Initialize the game
  15. const canvas = document.getElementById("game-canvas");
  16. const ctx = canvas.getContext("2d");
  17. let snake = [{x: 150, y: 150}];
  18. let food = {x: 300, y: 300};
  19. let dx = 0;
  20. let dy = 0;
  21. let score = 0;
  22. const FOOD_RADIUS = 20;
  23. const SNAKE_SIZE = 20;
  24. let newSegment = null;
  25. let newFood = true;
  26.  
  27. // Draw the game on the canvas
  28. function draw() {
  29. ctx.clearRect(0, 0, canvas.width, canvas.height);
  30. for (let i = 0; i < snake.length; i++) {
  31. ctx.fillStyle = "black";
  32. ctx.font = "bold 20px Arial";
  33. ctx.fillText("🐜",snake[i].x,snake[i].y);
  34. }
  35. if (newFood) {
  36. ctx.fillStyle = "red";
  37. ctx.font = "bold 20px Arial";
  38. ctx.fillText("🧂",food.x,food.y);
  39. }
  40. ctx.fillStyle = "black";
  41. ctx.font = "20px Arial";
  42. ctx.fillText(`Score: ${score}`, 10, 25);
  43. if (newSegment) {
  44. ctx.fillText("🐜", newSegment.x, newSegment.y);
  45. }
  46. }
  47.  
  48. // Move the snake
  49. function move() {
  50. const head = {x: snake[0].x + dx, y: snake[0].y + dy};
  51. snake.unshift(head);
  52. const didEatFood = Math.abs(snake[0].x - food.x) <= FOOD_RADIUS && Math.abs(snake[0].y - food.y) <= FOOD_RADIUS;
  53. if (didEatFood) {
  54. score++;
  55. newFood = false; // food is removed
  56. // Add new 🐜
  57. const edge = Math.floor(Math.random() * 4);
  58. let newX, newY;
  59. if (edge === 0) {
  60. newX = -SNAKE_SIZE;
  61. newY = Math.floor(Math.random() * canvas.height);
  62. } else if (edge === 1) {
  63. newX = canvas.width + SNAKE_SIZE;
  64. newY = Math.floor(Math.random() * canvas.height);
  65. } else if (edge === 2) {
  66. newX = Math.floor(Math.random() * canvas.width);
  67. newY = -SNAKE_SIZE;
  68. } else {
  69. newX = Math.floor(Math.random() * canvas.width);
  70. newY = canvas.height + SNAKE_SIZE;
  71. }
  72. newSegment = {x: newX, y: newY, targetX: snake[snake.length - 1].x, targetY: snake[snake.length - 1].y};
  73. } else {
  74. snake.pop();
  75. }
  76. }
  77.  
  78. // Move the new 🐜 segment towards the end of the snake
  79. function moveNewSegment() {
  80. if (newSegment) {
  81. const speed = 150; // increase the speed of the new 🐜
  82. const distX = newSegment.targetX - newSegment.x;
  83. const distY = newSegment.targetY - newSegment.y;
  84. if (Math.abs(distX) > speed || Math.abs(distY) > speed) {
  85. newSegment.x += distX / 10;
  86. newSegment.y += distY / 10;
  87. } else {
  88. snake.push(newSegment);
  89. newSegment = null;
  90. newFood = true; //only add new food when new segment has joined the snake
  91. //Generate new food in a different position
  92. do {
  93. food = {x: Math.floor(Math.random() * canvas.width), y: Math.floor(Math.random() * canvas.height)};
  94. } while (snake.some(segment => segment.x === food.x && segment.y === food.y));
  95. }
  96. }
  97. }
  98.  
  99. // Handle arrow key presses to move the snake
  100. document.addEventListener("keydown", event => {
  101. if (event.keyCode === 37) {
  102. dx = -5;
  103. dy = 0;
  104. } else if (event.keyCode === 38) {
  105. dx = 0;
  106. dy = -5;
  107. } else if (event.keyCode === 39) {
  108. dx = 5;
  109. dy = 0;
  110. } else if (event.keyCode === 40) {
  111. dx = 0;
  112. dy = 5;
  113. }
  114. });
  115.  
  116. // Main game loop
  117. setInterval(() => {
  118. move();
  119. moveNewSegment();
  120. draw();
  121. }, 100);
  122. </script>
  123. </body>
  124. </html>
  125.  
  126.  
  127.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment