javascript技巧

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript技巧 > js手写贪吃蛇游戏

js手写贪吃蛇游戏实例源码

作者:下一站丶

这篇文章主要给大家介绍了关于js手写贪吃蛇游戏的相关资料,贪吃蛇游戏是一款经典的游戏,在很多平台上都有出现,文中给出了详细源代码,需要的朋友可以参考下

前端手写贪吃蛇游戏

贪吃蛇游戏 场景 使用了js 和 html /css 就可以完成 一个贪吃蛇小游戏

技术分析

主要用到的几个技术点:

首先需要一个背景板 需要吧蛇和食物 在指定内容里 不能超出接下来就是蛇的移动 使用定时间一秒移动一格食物就是根据计算随机生成当蛇和食物的位置重叠了就可以认为蛇吃到了食物当蛇的x,y 大于元素的宽度/高度,就认为碰到了墙壁直接ov

以上就是关键技术点 直接源码

源码

HTML

<div id="game-board"></div> 

JS

<script>
  const gameBoard = document.getElementById("game-board");
  let snake = [{ x: 0, y: 0 }];
  let food = { x: 0, y: 0 };
  let direction = "right";
  function createSnake() {
    for (let segment of snake) {
      const snakeSegment = document.createElement("div");
      snakeSegment.className = "snake";
      snakeSegment.style.left = segment.x + "px";
      snakeSegment.style.top = segment.y + "px";
      gameBoard.appendChild(snakeSegment);
    }
  }
  function createFood() {
    const maxX = gameBoard.clientWidth - 20;
    const maxY = gameBoard.clientHeight - 20;
    food.x = Math.floor(Math.random() * maxX);
    food.y = Math.floor(Math.random() * maxY);
    const foodElement = document.createElement("div");
    foodElement.className = "food";
    foodElement.style.left = food.x + "px";
    foodElement.style.top = food.y + "px";
    gameBoard.appendChild(foodElement);
  }
  function updateGame() {
    const snakeHead = { x: snake[0].x, y: snake[0].y };
    // Move the snake
    if (direction === "right") snakeHead.x += 20;
    else if (direction === "left") snakeHead.x -= 20;
    else if (direction === "up") snakeHead.y -= 20;
    else if (direction === "down") snakeHead.y += 20;
    // Check for collision with food
    if (snakeHead.x === food.x && snakeHead.y === food.y) {
      snake.push({ x: snakeHead.x, y: snakeHead.y });
      const foodElement = document.querySelector(".food");
      foodElement.remove();
      createFood();
    }
    // Remove the tail segment
    snake.pop();
    // Check for collision with walls or itself
    if (
      snakeHead.x < 0 ||
      snakeHead.x >= gameBoard.clientWidth ||
      snakeHead.y < 0 ||
      snakeHead.y >= gameBoard.clientHeight
    ) {
      clearInterval(gameInterval);
      alert("Game Over!");
      return;
    }
    for (let segment of snake) {
      if (segment.x === snakeHead.x && segment.y === snakeHead.y) {
        clearInterval(gameInterval);
        alert("Game Over!");
        return;
      }
    }
    // Update the snake on the game board
    snake.unshift({ x: snakeHead.x, y: snakeHead.y });
    const snakeSegments = document.querySelectorAll(".snake");
    for (let i = 0; i < snakeSegments.length; i++) {
      snakeSegments[i].style.left = snake[i].x + "px";
      snakeSegments[i].style.top = snake[i].y + "px";
    }
  }
  createSnake();
  createFood();
  const gameInterval = setInterval(updateGame, 150);
  document.addEventListener("keydown", (event) => {
    if (event.key === "ArrowRight" && direction !== "left") direction = "right";
    else if (event.key === "ArrowLeft" && direction !== "right") direction = "left";
    else if (event.key === "ArrowUp" && direction !== "down") direction = "up";
    else if (event.key === "ArrowDown" && direction !== "up") direction = "down";
  });
</script>

CSS

<style>
  #game-board {
    width: 300px;
    height: 300px;
    border: 1px solid black;
    position: relative;
  }
  .snake {
    width: 20px;
    height: 20px;
    background-color: green;
    position: absolute;
  }
  .food {
    width: 20px;
    height: 20px;
    background-color: red;
    position: absolute;
  }
</style>

以上就实现了简单的贪吃蛇功能

总结

到此这篇关于js手写贪吃蛇游戏的文章就介绍到这了,更多相关js手写贪吃蛇游戏内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文