<!DOCTYPE html>
<html>
  <head>
    <title>贪吃蛇游戏</title>
    <style>
      #game-board {
        width: 600px;
        height: 600px;
        border: 1px solid black;
      }
<pre><code>  .snake {
    width: 20px;
    height: 20px;
    background-color: green;
    position: absolute;
  }

  .food {
    width: 20px;
    height: 20px;
    background-color: red;
    position: absolute;
  }
&lt;/style&gt;
</code></pre>
  </head>
  <body>
    <h1>贪吃蛇游戏</h1>
    <p>使用方向键控制蛇的移动,吃到食物可以增加长度</p>
    <div id="game-board"></div>
    <script>
      const board = document.getElementById("game-board");
      const blockWidth = 20;
      const blockHeight = 20;
      const boardWidth = board.offsetWidth;
      const boardHeight = board.offsetHeight;
      const snake = [{ x: 0, y: 0 }];
      let food = null;
      let direction = "right";
      let intervalId = null;
<pre><code>  function createBlock(x, y, className) {
    const block = document.createElement(&quot;div&quot;);
    block.className = className;
    block.style.left = `${x}px`;
    block.style.top = `${y}px`;
    board.appendChild(block);
    return block;
  }

  function createSnake() {
    snake.forEach((block) =&gt; {
      createBlock(block.x, block.y, &quot;snake&quot;);
    });
  }

  function createFood() {
    const x = Math.floor(Math.random() * boardWidth);
    const y = Math.floor(Math.random() * boardHeight);
    food = createBlock(x, y, &quot;food&quot;);
  }

  function updateSnake() {
    for (let i = snake.length - 1; i &gt; 0; i--) {
      snake[i].x = snake[i - 1].x;
      snake[i].y = snake[i - 1].y;
    }
    switch (direction) {
      case &quot;up&quot;:
        snake[0].y -= blockHeight;
        break;
      case &quot;down&quot;:
        snake[0].y += blockHeight;
        break;
      case &quot;left&quot;:
        snake[0].x -= blockWidth;
        break;
      case &quot;right&quot;:
        snake[0].x += blockWidth;
        break;
    }
  }

  function checkCollision() {
    if (
      snake[0].x &lt; 0 ||
      snake[0].x &gt;= boardWidth ||
      snake[0].y &lt; 0 ||
      snake[0].y &gt;= boardHeight
    ) {
      clearInterval(intervalId);
      alert(&quot;游戏结束&quot;);
    }
    if (snake.slice(1).some((block) =&gt; block.x === snake[0].x &amp;&amp; block.y === snake[0].y)) {
      clearInterval(intervalId);
      alert(&quot;游戏结束&quot;);
    }
    if (snake[0].x === food.offsetLeft &amp;&amp; snake[0].y === food.offsetTop) {
      food.remove();
      createFood();
      snake.push({ x: snake[snake.length - 1].x, y: snake[snake.length - 1].y });
    }
  }

  function render() {
    board.innerHTML = &quot;&quot;;
    createSnake();
    if (!food) {
      createFood();
    }
  }

  function gameLoop() {
    updateSnake();
    checkCollision();
    render();
  }

  document.addEventListener(&quot;keydown&quot;, (event) =&gt; {
    switch (event.keyCode) {
      case 38:
        if (direction !== &quot;down&quot;) {
          direction = &quot;up&quot;;
        }
        break;
      case 40:
        if (direction !== &quot;up&quot;) {
          direction = &quot;down&quot;;
        }
        break;
      case 37:
        if (direction !== &quot;right&quot;) {
          direction = &quot;left&quot;;
        }
        break;
      case 39:
        if (direction !== &quot;left&quot;) {
          direction = &quot;right&quot;;
        }
        break;
    }
  });

  intervalId = setInterval(gameLoop, 200);

&lt;/script&gt;
</code></pre>
  </body>
</html>

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

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