Snake Game!

плохое настроение? сыграйте в меня!

目前为 2024-12-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Snake Game!
  3. // @version 3.6
  4. // @description плохое настроение? сыграйте в меня!
  5. // @autor Minish
  6. // @namespace drawaria.snake.game
  7. // @match drawaria.online
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // Create the game canvas
  15. const canvas = document.createElement('canvas');
  16. canvas.width = 800; // Increased width
  17. canvas.height = 600; // Increased height
  18. document.body.appendChild(canvas);
  19. const ctx = canvas.getContext('2d');
  20.  
  21. // Load images
  22. const appleImage = new Image();
  23. appleImage.src = 'https://w7.pngwing.com/pngs/381/179/png-transparent-apple-green-apple-green-apple-food-granny-smith-asian-pear-thumbnail.png'; // Transparent background apple
  24.  
  25. const goldenAppleImage = new Image();
  26. goldenAppleImage.src = 'https://w7.pngwing.com/pngs/534/248/png-transparent-minecraft-computer-icons-golden-apple-golden-orange-fruit-apple-thumbnail.png'; // Transparent background golden apple
  27.  
  28. const backgroundImage = new Image();
  29. backgroundImage.src = 'https://avatars.mds.yandex.net/i?id=d7ddea51bc660760175ae06837721008ec22c13e3c0bd198-10786048-images-thumbs&n=13'; // Указанная текстура фона
  30.  
  31. // Game variables
  32. const box = 40; // Increased size of apples
  33. const initialPosition = { x: 9 * box, y: 9 * box }; // Initial spawn position
  34. let snake = [initialPosition]; // Start with initial snake position
  35. let direction = null;
  36. let apple = spawnApple();
  37. let goldenApple = null; // Variable for golden apple
  38. let score = 0;
  39. let skins = ['green', 'blue', 'red', 'yellow', 'purple'];
  40. let currentSkin = 0; // Index for the current skin
  41. let isPaused = false; // Game pause state
  42. let appleCount = 0; // Count of regular apples collected
  43.  
  44. // Load sound
  45. const soundEffect = new Audio('https://www.myinstants.com/en/instant/snake-game-food-65186/?utm_source=copy&utm_medium=share'); // URL to sound effect
  46.  
  47. // Create skin selection section
  48. const shopContainer = document.createElement('div');
  49. shopContainer.style.margin = '20px';
  50. shopContainer.style.textAlign = 'center';
  51. shopContainer.style.backgroundColor = 'lightgray';
  52. shopContainer.style.padding = '10px';
  53. shopContainer.style.borderRadius = '5px';
  54.  
  55. const title = document.createElement('h2');
  56. title.innerHTML = 'Shop - Choose Your Skin';
  57. shopContainer.appendChild(title);
  58.  
  59. skins.forEach((color, index) => {
  60. const button = document.createElement('button');
  61. button.innerHTML = `Buy ${color} Skin`;
  62. button.style.margin = '5px';
  63. button.onclick = () => {
  64. currentSkin = index; // Change skin index
  65. };
  66. shopContainer.appendChild(button);
  67. });
  68.  
  69. document.body.appendChild(shopContainer);
  70.  
  71. // Control the snake with arrow keys
  72. document.addEventListener('keydown', (event) => {
  73. if (event.key === 'ArrowUp' && direction !== 'DOWN') {
  74. direction = 'UP';
  75. soundEffect.play(); // Play sound on key press
  76. }
  77. else if (event.key === 'ArrowDown' && direction !== 'UP') {
  78. direction = 'DOWN';
  79. soundEffect.play(); // Play sound on key press
  80. }
  81. else if (event.key === 'ArrowLeft' && direction !== 'RIGHT') {
  82. direction = 'LEFT';
  83. soundEffect.play(); // Play sound on key press
  84. }
  85. else if (event.key === 'ArrowRight' && direction !== 'LEFT') {
  86. direction = 'RIGHT';
  87. soundEffect.play(); // Play sound on key press
  88. }
  89. else if (event.key === ' ') {
  90. isPaused = !isPaused; // Toggle pause on space
  91. soundEffect.play(); // Play sound on key press
  92. }
  93. });
  94.  
  95. // Game loop
  96. function game() {
  97. if (!isPaused) {
  98. ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear the canvas for redrawing
  99. // Draw the background texture
  100. ctx.drawImage(backgroundImage, 0, 0, canvas.width, canvas.height);
  101.  
  102. // Draw the snake
  103. for (let i = 0; i < snake.length; i++) {
  104. ctx.fillStyle = (i === 0) ? skins[currentSkin] : 'lightgreen'; // Change head color based on current skin
  105. ctx.fillRect(snake[i].x, snake[i].y, box, box);
  106. ctx.strokeStyle = 'darkgreen';
  107. ctx.strokeRect(snake[i].x, snake[i].y, box, box);
  108. }
  109.  
  110. // Draw the apple
  111. ctx.drawImage(appleImage, apple.x, apple.y, box, box);
  112.  
  113. // Draw the golden apple if it exists
  114. if (goldenApple) {
  115. ctx.drawImage(goldenAppleImage, goldenApple.x, goldenApple.y, box, box);
  116. }
  117.  
  118. // Move the snake
  119. let snakeX = snake[0].x;
  120. let snakeY = snake[0].y;
  121.  
  122. if (direction === 'LEFT') snakeX -= box;
  123. if (direction === 'UP') snakeY -= box;
  124. if (direction === 'RIGHT') snakeX += box;
  125. if (direction === 'DOWN') snakeY += box;
  126.  
  127. // Check if the snake eats the apple
  128. if (snakeX === apple.x && snakeY === apple.y) {
  129. score++;
  130. appleCount++; // Increment apple count
  131. apple = spawnApple(); // Spawn a new apple
  132. // Check if the apple count reached 100
  133. if (appleCount === 100) {
  134. goldenApple = spawnGoldenApple(); // Spawn golden apple
  135. }
  136. } else {
  137. snake.pop();
  138. }
  139.  
  140. // Check if the snake eats the golden apple
  141. if (goldenApple && snakeX === goldenApple.x && snakeY === goldenApple.y) {
  142. score += 500; // Add 500 points for golden apple
  143. goldenApple = null; // Remove golden apple after eaten
  144. appleCount = 0; // Reset apple count after golden apple
  145. }
  146.  
  147. // Add the new head
  148. const newHead = { x: snakeX, y: snakeY };
  149.  
  150. // Check for collisions with walls or itself
  151. if (collision(newHead, snake) || snakeX < 0 || snakeX >= canvas.width || snakeY < 0 || snakeY >= canvas.height) {
  152. alert('Game Over! Your score: ' + score + '. Click OK to respawn.');
  153. teleportToSpawn(); // Teleport to spawn
  154. } else {
  155. snake.unshift(newHead);
  156. }
  157.  
  158. ctx.fillStyle = 'black';
  159. ctx.font = '20px Arial';
  160. ctx.fillText('Score: ' + score, box, box);
  161. ctx.fillText('Apples Collected: ' + appleCount, box, box + 20); // Display collected apples
  162. } else {
  163. ctx.fillStyle = 'black';
  164. ctx.font = '30px Arial';
  165. ctx.fillText('Game Paused', canvas.width / 2 - 70, canvas.height / 2);
  166. }
  167. }
  168.  
  169. // Check for collision with the snake itself
  170. function collision(head, array) {
  171. for (let i = 0; i < array.length; i++) {
  172. if (head.x === array[i].x && head.y === array[i].y) {
  173. return true;
  174. }
  175. }
  176. return false;
  177. }
  178.  
  179. // Teleport to spawn position
  180. function teleportToSpawn() {
  181. snake = [initialPosition]; // Reset snake to spawn position
  182. direction = null; // Reset direction
  183. apple = spawnApple(); // Spawn a new apple
  184. goldenApple = null; // Remove golden apple if it exists
  185. appleCount = 0; // Reset apple count
  186. }
  187.  
  188. // Function to spawn apple in a valid location
  189. function spawnApple() {
  190. let newApple;
  191. let isValidPosition = false;
  192.  
  193. while (!isValidPosition) {
  194. newApple = {
  195. x: Math.floor(Math.random() * (canvas.width / box)) * box,
  196. y: Math.floor(Math.random() * (canvas.height / box)) * box
  197. };
  198.  
  199. // Check if the apple spawns on the snake
  200. isValidPosition = !collision(newApple, snake);
  201. }
  202.  
  203. return newApple;
  204. }
  205.  
  206. // Function to spawn golden apple in a valid location
  207. function spawnGoldenApple() {
  208. let newGoldenApple;
  209. let isValidPosition = false;
  210.  
  211. while (!isValidPosition) {
  212. newGoldenApple = {
  213. x: Math.floor(Math.random() * (canvas.width / box)) * box,
  214. y: Math.floor(Math.random() * (canvas.height / box)) * box
  215. };
  216.  
  217. // Check if the golden apple spawns on the snake or regular apple
  218. isValidPosition = !collision(newGoldenApple, snake) && !(newGoldenApple.x === apple.x && newGoldenApple.y === apple.y);
  219. }
  220.  
  221. return newGoldenApple;
  222. }
  223.  
  224. // Start the game loop
  225. setInterval(game, 100);
  226. })();