Snake Game

Play Snake on any webpage by pressing 'S'

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/491883/1355808/Snake%20Game.js

  1. // ==UserScript==
  2. // @name Snake Game
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Play Snake on any webpage by pressing 'S'
  6. // @author You
  7. // @match https://*/*
  8. // @match http://*/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. let snakeInterval;
  16. let snake;
  17. let food;
  18. let direction;
  19. let score;
  20.  
  21. function initSnake() {
  22. snake = [{x: 10, y: 10}];
  23. food = getRandomPosition();
  24. direction = 'right';
  25. score = 0;
  26. snakeInterval = setInterval(moveSnake, 100);
  27. }
  28.  
  29. function getRandomPosition() {
  30. return {
  31. x: Math.floor(Math.random() * 20),
  32. y: Math.floor(Math.random() * 20)
  33. };
  34. }
  35.  
  36. function moveSnake() {
  37. const head = { ...snake[0] };
  38.  
  39. switch (direction) {
  40. case 'up':
  41. head.y--;
  42. break;
  43. case 'down':
  44. head.y++;
  45. break;
  46. case 'left':
  47. head.x--;
  48. break;
  49. case 'right':
  50. head.x++;
  51. break;
  52. }
  53.  
  54. if (head.x < 0 || head.x >= 20 || head.y < 0 || head.y >= 20 || isSnakeCollided(head)) {
  55. clearInterval(snakeInterval);
  56. alert('Game Over! Score: ' + score);
  57. initSnake();
  58. return;
  59. }
  60.  
  61. snake.unshift(head);
  62.  
  63. if (head.x === food.x && head.y === food.y) {
  64. score++;
  65. food = getRandomPosition();
  66. } else {
  67. snake.pop();
  68. }
  69.  
  70. drawSnake();
  71. }
  72.  
  73. function isSnakeCollided(head) {
  74. return snake.some(segment => segment.x === head.x && segment.y === head.y);
  75. }
  76.  
  77. function drawSnake() {
  78. const canvas = document.getElementById('snakeCanvas');
  79. const ctx = canvas.getContext('2d');
  80.  
  81. ctx.clearRect(0, 0, canvas.width, canvas.height);
  82.  
  83. snake.forEach(segment => {
  84. ctx.fillStyle = 'green';
  85. ctx.fillRect(segment.x * 20, segment.y * 20, 20, 20);
  86. });
  87.  
  88. ctx.fillStyle = 'red';
  89. ctx.fillRect(food.x * 20, food.y * 20, 20, 20);
  90. }
  91.  
  92. function handleKeydown(event) {
  93. switch (event.key) {
  94. case 'ArrowUp':
  95. direction = 'up';
  96. break;
  97. case 'ArrowDown':
  98. direction = 'down';
  99. break;
  100. case 'ArrowLeft':
  101. direction = 'left';
  102. break;
  103. case 'ArrowRight':
  104. direction = 'right';
  105. break;
  106. case 's':
  107. case 'S':
  108. if (!snakeInterval) {
  109. initSnake();
  110. }
  111. break;
  112. }
  113. }
  114.  
  115. function createCanvas() {
  116. const canvas = document.createElement('canvas');
  117. canvas.id = 'snakeCanvas';
  118. canvas.width = 400;
  119. canvas.height = 400;
  120. canvas.style.border = '1px solid black';
  121. canvas.style.position = 'fixed';
  122. canvas.style.top = '50%';
  123. canvas.style.left = '50%';
  124. canvas.style.transform = 'translate(-50%, -50%)';
  125. canvas.style.zIndex = '9999';
  126. document.body.appendChild(canvas);
  127. }
  128.  
  129. createCanvas();
  130. document.addEventListener('keydown', handleKeydown);
  131.  
  132. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址