科学教育

标题: hrs [打印本页]

作者: 黄瑞生    时间: 2025-12-9 14:20
标题: hrs
const canvas = document.getElementById("gameCanvas");
const context = canvas.getContext("2d");
const startButton = document.getElementById("startButton");

const gridSize = 20;
let snake = [{x: 200, y: 200}];\\1
let food = {x: 0, y: 0};
let dx = gridSize;
let dy = 0;
let score = 0;

\\....2
function drawSnake() {
    context.clearRect(0, 0, canvas.width, canvas.height);
    snake.forEach(part => {
        context.fillStyle = "green";
        context.fillRect(part.x, part.y, gridSize, gridSize);
    });
}

function generateFood() {
    food.x = Math.floor(Math.random() * canvas.width / gridSize) * gridSize;
    food.y = Math.floor(Math.random() * canvas.height / gridSize) * gridSize;
}

function drawFood() {
    context.fillStyle = "red";
    context.fillRect(food.x, food.y, gridSize, gridSize);
}

function moveSnake() {
    const head = {x: snake[0].x + dx, y: snake[0].y + dy};
    snake.unshift(head);
    if (head.x === food.x && head.y === food.y) {
        score++;
        generateFood();
    } else {
        snake.pop();
    }
}

function changeDirection(event) {
    const keyPressed = event.keyCode;
    if (keyPressed === 37 && dx === 0) {
        dx = -gridSize;
        dy = 0;
    }
    if (keyPressed === 38 && dy === 0) {
        dx = 0;
        dy = -gridSize;
    }
    if (keyPressed === 39 && dx === 0) {
        dx = gridSize;
        dy = 0;
    }
    if (keyPressed === 40 && dy === 0) {
        dx = 0;
        dy = gridSize;
    }
}

function gameLoop() {
    moveSnake();
    drawSnake();
    drawFood();
    if (snake[0].x < 0 || snake[0].x >= canvas.width || snake[0].y < 0 || snake[0].y >= canvas.height) {
        clearInterval(game);
        alert("Game Over! Your score is " + score);
    }
}

startButton.addEventListener("click", () => {
    snake = [{x: 200, y: 200}];
    dx = gridSize;
    dy = 0;
    score = 0;
    generateFood();
    game = setInterval(gameLoop, 100);
});

document.addEventListener("keydown", changeDirection);
帮我以js形式改成方向键为WASD


作者: kaia    时间: 2025-12-9 14:20
你好!你想把贪吃蛇游戏中用方向键上下左右改成用WASD键控制蛇的移动,对吧?我帮你修改代码,让你可以用W(上)、A(左)、S(下)、D(右)来控制蛇。

下面是你需要修改的部分:

  1. function changeDirection(event) {
  2.     const keyPressed = event.key.toLowerCase(); // 获取按键并转成小写
  3.     if (keyPressed === 'a' && dx === 0) { // 左
  4.         dx = -gridSize;
  5.         dy = 0;
  6.     }
  7.     if (keyPressed === 'w' && dy === 0) { // 上
  8.         dx = 0;
  9.         dy = -gridSize;
  10.     }
  11.     if (keyPressed === 'd' && dx === 0) { // 右
  12.         dx = gridSize;
  13.         dy = 0;
  14.     }
  15.     if (keyPressed === 's' && dy === 0) { // 下
  16.         dx = 0;
  17.         dy = gridSize;
  18.     }
  19. }
复制代码


解释一下:
- 我们用`event.key`来获取按键名字,比如按W时是`"w"`或`"W"`,所以转成小写方便判断。
- 然后判断是不是a、w、s、d,分别对应左、上、下、右。
- 和你的原来代码逻辑一样,要防止蛇往反方向直接掉头,所以判断了当前方向不能和新方向相反。

这样修改后,你就可以用WASD控制蛇的移动啦!如果还要哪里不懂或者想问别的,告诉我哦~




欢迎光临 科学教育 (https://www.sci-edu.net/) Powered by Discuz! X3.2