【问题标题】:trying to increase snae size by for units in my JS snake game试图通过我的 JS 蛇游戏中的单位来增加蛇的大小
【发布时间】:2019-01-14 21:07:03
【问题描述】:

大家好,我正在用 JavaScript 制作蛇游戏,到目前为止,我可以移动蛇并在画布上生成随机食物单元。我还可以吃食物并将蛇的长度加一。但是我想把它增加四。我使用snake.unshift(newHead); 添加了一个新头像,所以我的想法是我只是重复了 4 次。这只会使蛇变大。有人可以解释为什么我会得到这种结果并给我一个解决方案的提示吗?谢谢:)

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}];

//delcare global variable to hold users direction
let direction;

//create food object
let food = {
	x : Math.floor(Math.random()*((cvsW/unit)-1)+1)*unit,
	y : Math.floor(Math.random()*((cvsH/unit)-1)+1)*unit
}

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

function changeDirection(e) {
	//set direction
	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';
}

function draw() {
	//refresh canvas
	ctx.clearRect(0, 0, cvsW, cvsH);
	//draw snake
	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;

	//posistion food on board
	ctx.fillStyle = 'red';
	ctx.fillRect(food.x, food.y, unit, unit);

	//send the snake in chosen direction
	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}

	if(headX == food.x && headY == food.y) {
		//create new food position
	 	food = {
			x : Math.floor(Math.random()*((cvsW/unit)-1)+1)*unit,
			y : Math.floor(Math.random()*((cvsH/unit)-1)+1)*unit
		}
	}
	else {
		//remove tail
		snake.pop();
	}

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

//run game engine
setInterval(draw, 70);
<!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>

【问题讨论】:

  • 无论蛇是否吃东西,每次迭代都会添加四个新头。

标签: javascript


【解决方案1】:

无论蛇是否吃东西,每次迭代都会添加四个新的头。

使用您在下面的确切代码,我刚刚将进食部分与它自己的功能分开(当然这可以改进很多 - 但这会给您一个想法)。蛇是在吃东西的时候长大的,而不是一直在长大。

而且...只是提醒一下,您还需要确保食物没有放在蛇身上。这是一个很酷的项目,祝你好运。

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}];

//delcare global variable to hold users direction
let direction;

//create food object
let food = {
	x : Math.floor(Math.random()*((cvsW/unit)-1)+1)*unit,
	y : Math.floor(Math.random()*((cvsH/unit)-1)+1)*unit
}

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

function changeDirection(e) {
	//set direction
	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';
}

function draw() {
	//refresh canvas
	ctx.clearRect(0, 0, cvsW, cvsH);
	//draw snake
	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;

	//posistion food on board
	ctx.fillStyle = 'red';
	ctx.fillRect(food.x, food.y, unit, unit);

	//send the snake in chosen direction
	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}

	if(headX == food.x && headY == food.y) {
		//create new food position
	 	food = {
			x : Math.floor(Math.random()*((cvsW/unit)-1)+1)*unit,
			y : Math.floor(Math.random()*((cvsH/unit)-1)+1)*unit
		}

        eat(newHead);
	}
	else {
		//remove tail
		snake.pop();
	}

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

function eat(newHead) {
    //add 3 nore heads
	snake.unshift(newHead);
	snake.unshift(newHead);
	snake.unshift(newHead);
}

//run game engine
setInterval(draw, 70);
<!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>

【讨论】:

  • 谢谢伙计。你是说吃功能可以改进很多,还是我的代码一般?
  • 没问题,好吧..关于功能,这里有一个提示;而不是多次重复同一行代码,您可以创建某种为您执行此操作的逻辑。代码总是可以改进的,一种改进通常是将逻辑拆分为适当的部分。如果整个逻辑都在您的游戏循环中,那么随着它的增长可能难以维护。
猜你喜欢
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多