gpt4 book ai didi

javascript - pop 函数不是从蛇数组中取出尾部吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:56:58 26 4
gpt4 key购买 nike

我正在用 JS 制作贪吃蛇游戏。现在我可以将蛇绘制到 Canvas 上并接受用户的移动方向。鉴于方向,我能够 unshift() 一个新的蛇头,但出于某种原因我不能使用 pop 方法移除尾部。这只会导致我的蛇变得越来越大。知道这是为什么吗?

const canvas = document.querySelector('#canvas');
const ctx = canvas.getContext('2d');

//set canvas dimension equal to css dimension
canvas.width = 768;
canvas.height = 512;

//now put those dimensions into variables
const cvsW = canvas.width;
const cvsH = canvas.height;

//create snake unit
const unit = 16;

//create snake array
let snake = [{x: cvsW/2, y: cvsH/2}];

//read user's direction
document.addEventListener('keydown', changeDirection);

let direction;

function changeDirection(e) {
if (e.keyCode == 37 && direction != 'right') direction = 'left';
else if (e.keyCode == 38 && direction != 'down') direction = 'up';
else if (e.keyCode == 39 && direction != 'left') direction = 'right';
else if (e.keyCode == 40 && direction != 'up') direction = 'down';

console.log(direction);
}

function draw() {
for(let i = 0; i < snake.length; i++) {
ctx.fillStyle = 'limegreen';
ctx.fillRect(snake[i].x, snake[i].y, unit, unit);
}

//grab head position
let headX = snake[0].x;
let headY = snake[0].y;

snake.pop();

if(direction == 'left') headX -= unit;
else if(direction == 'up') headY -= unit;
else if(direction == 'right') headX += unit;
else if(direction == 'down') headY += unit;

//create new head
let newHead = {x: headX, y: headY}

//add head to snake
snake.unshift(newHead);
}

setInterval(draw, 100);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Snake Game</title>
<style>
body {
background-color: #333;
}

canvas {
background-color: #4d4d4d;
margin: auto;
display: block;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
width: 750px;
height: 500px;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script src="script.js"></script>
</body>
</html>

最佳答案

您需要在每次迭代时清除当前 Canvas ,否则先前绘制到 Canvas 上的像素将保留。添加

  ctx.clearRect(0, 0, canvas.width, canvas.height);

就在开始遍历蛇数组以绘制每个方 block 之前:

const canvas = document.querySelector('#canvas');
const ctx = canvas.getContext('2d');

//set canvas dimension equal to css dimension
canvas.width = 768;
canvas.height = 512;

//now put those dimensions into variables
const cvsW = canvas.width;
const cvsH = canvas.height;

//create snake unit
const unit = 16;

//create snake array
let snake = [{
x: cvsW / 2,
y: cvsH / 2
}];

//read user's direction
document.addEventListener('keydown', changeDirection);

let direction;

function changeDirection(e) {
if (e.keyCode == 37 && direction != 'right') direction = 'left';
else if (e.keyCode == 38 && direction != 'down') direction = 'up';
else if (e.keyCode == 39 && direction != 'left') direction = 'right';
else if (e.keyCode == 40 && direction != 'up') direction = 'down';

console.log(direction);
}

function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'limegreen';
for (let i = 0; i < snake.length; i++) {
ctx.fillRect(snake[i].x, snake[i].y, unit, unit);
}

//grab head position
let headX = snake[0].x;
let headY = snake[0].y;

snake.pop();

if (direction == 'left') headX -= unit;
else if (direction == 'up') headY -= unit;
else if (direction == 'right') headX += unit;
else if (direction == 'down') headY += unit;

//create new head
let newHead = {
x: headX,
y: headY
}

//add head to snake
snake.unshift(newHead);
}

setInterval(draw, 100);
body {
background-color: #333;
}

canvas {
background-color: #4d4d4d;
margin: auto;
display: block;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
width: 750px;
height: 500px;
}
<canvas id="canvas"></canvas>

关于javascript - pop 函数不是从蛇数组中取出尾部吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54177472/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com