<!DOCTYPE html>
<html>
  <head>
    <style>
      #gameArea {
        position: relative;
        width: 100vw;
        height: 100vh;
        background-color: #000;
      }
<pre><code>  #snake {
    position: absolute;
    top: 0;
    left: 0;
    width: 10px;
    height: 10px;
    background-color: #fff;
  }

  #food {
    position: absolute;
    top: 0;
    left: 0;
    width: 10px;
    height: 10px;
    background-color: #f00;
  }
&lt;/style&gt;
</code></pre>
  </head>
  <body>
    <div id="gameArea"></div>
<pre><code>&lt;script&gt;
  var gameArea = document.getElementById(&quot;gameArea&quot;);
  var snake = document.getElementById(&quot;snake&quot;);
  var food = document.getElementById(&quot;food&quot;);

  var direction = &quot;right&quot;;
  var speed = 1;
  var score = 0;
  var maxLength = 0;

  var gameLoop;

  function startGame() {
    direction = &quot;right&quot;;
    speed = 1;
    score = 0;
    maxLength = 0;

    snake.style.top = &quot;0px&quot;;
    snake.style.left = &quot;0px&quot;;

    createFood();

    gameLoop = setInterval(moveSnake, 1000 / speed);

    document.addEventListener(&quot;keydown&quot;, handleKeyPress);
    gameArea.addEventListener(&quot;touchstart&quot;, handleTouchStart, false);
    gameArea.addEventListener(&quot;touchmove&quot;, handleTouchMove, false);
  }

  function endGame() {
    clearInterval(gameLoop);

    document.removeEventListener(&quot;keydown&quot;, handleKeyPress);
    gameArea.removeEventListener(&quot;touchstart&quot;, handleTouchStart, false);
    gameArea.removeEventListener(&quot;touchmove&quot;, handleTouchMove, false);

    if (score &gt; maxLength) {
      maxLength = score;
    }

    alert(&quot;Game Over! Your score: &quot; + score + &quot; Max length: &quot; + maxLength);
  }

  function moveSnake() {
    var currentTop = parseInt(snake.style.top);
    var currentLeft = parseInt(snake.style.left);

    switch (direction) {
      case &quot;up&quot;:
        currentTop -= 10;
        break;
      case &quot;down&quot;:
        currentTop += 10;
        break;
      case &quot;left&quot;:
        currentLeft -= 10;
        break;
      case &quot;right&quot;:
        currentLeft += 10;
        break;
    }

    if (
      currentLeft &lt; 0 ||
      currentTop &lt; 0 ||
      currentLeft &gt;= gameArea.offsetWidth ||
      currentTop &gt;= gameArea.offsetHeight
    ) {
      endGame();
      return;
    }

    snake.style.top = currentTop + &quot;px&quot;;
    snake.style.left = currentLeft + &quot;px&quot;;

    if (checkCollision(food)) {
      score++;
      createFood();
      speed += 0.5;
      clearInterval(gameLoop);
      gameLoop = setInterval(moveSnake, 1000 / speed);
    }
  }

  function handleKeyPress(event) {
    switch (event.keyCode) {
      case 32: // Space key for acceleration
        clearInterval(gameLoop);
        gameLoop = setInterval(moveSnake, 1000 / (speed * 2));
        break;
      case 37: // Left arrow key
        if (direction !== &quot;right&quot;) {
          direction = &quot;left&quot;;
        }
        break;
      case 38: // Up arrow key
        if (direction !== &quot;down&quot;) {
          direction = &quot;up&quot;;
        }
        break;
      case 39: // Right arrow key
        if (direction !== &quot;left&quot;) {
          direction = &quot;right&quot;;
        }
        break;
      case 40: // Down arrow key
        if (direction !== &quot;up&quot;) {
          direction = &quot;down&quot;;
        }
        break;
    }
  }

  var xDown = null;
  var yDown = null;

  function handleTouchStart(event) {
    xDown = event.touches[0].clientX;
    yDown = event.touches[0].clientY;
  }

  function handleTouchMove(event) {
    if (!xDown || !yDown) {
      return;
    }

    var xUp = event.touches[0].clientX;
    var yUp = event.touches[0].clientY;

    var xDiff = xDown - xUp;
    var yDiff = yDown - yUp;

    if (Math.abs(xDiff) &gt; Math.abs(yDiff)) {
      if (xDiff &gt; 0 &amp;&amp; direction !== &quot;right&quot;) {
        direction = &quot;left&quot;;
      } else if (xDiff &lt; 0 &amp;&amp; direction !== &quot;left&quot;) {
        direction = &quot;right&quot;;
      }
    } else {
      if (yDiff &gt; 0 &amp;&amp; direction !== &quot;down&quot;) {
        direction = &quot;up&quot;;
      } else if (yDiff &lt; 0 &amp;&amp; direction !== &quot;up&quot;) {
        direction = &quot;down&quot;;
      }
    }

    xDown = null;
    yDown = null;
  }

  function checkCollision(element) {
    var snakeRect = snake.getBoundingClientRect();
    var elementRect = element.getBoundingClientRect();

    return !(
      snakeRect.right &lt; elementRect.left ||
      snakeRect.left &gt; elementRect.right ||
      snakeRect.bottom &lt; elementRect.top ||
      snakeRect.top &gt; elementRect.bottom
    );
  }

  function createFood() {
    var maxX = gameArea.offsetWidth / 10;
    var maxY = gameArea.offsetHeight / 10;

    var randomX = Math.floor(Math.random() * maxX) * 10;
    var randomY = Math.floor(Math.random() * maxY) * 10;

    food.style.top = randomY + &quot;px&quot;;
    food.style.left = randomX + &quot;px&quot;;
  }

  startGame();
&lt;/script&gt;
</code></pre>
  </body>
</html>
以下是一个简单的贪吃蛇游戏的HTML代码它可以通过方向键操作并且支持空格键加速。游戏范围为手机全屏并且拥有颜色可以通过吃游戏范围内的食物来增加长度。如果蛇头碰到游戏范围界限或者自身游戏将结束。游戏结束后可以记录最大长度并且有重新开始按键。此外代码还支持手机屏幕滑动操作上下左右。

原文地址: https://www.cveoy.top/t/topic/i7Er 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录