测试蛇是否自身碰撞的功能不起作用

时间:2019-01-20 23:48:52

标签: javascript

大家好,我正在用JS做蛇游戏。现在,我正在研究该功能,如果蛇头撞到蛇身上的某个地方,它将停止游戏。因为蛇是一个阵列,所以我遍历每个蛇单元并将其与头部单元进行比较。现在的问题是,游戏甚至在开始之前就已停止。由于蛇头位于要通过第一个循环循环的数组中,因此将蛇头位置与其自身进行比较,并且由于两者都位于同一位置,因此游戏会停止。有什么想法吗?

//declare global variables
const canvas = document.querySelector('#canvas');

//set canvas context
const ctx = canvas.getContext('2d');

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

//create snake unit
const unit = 16;

//create snake and set starting position
let snake = [{
	x : cvsW/2,
	y : cvsH/2
}]

//create food object and set its position somewhere on board
let food = {
	//Math.floor(Math.random()*cvsW + 1)---number from 1 to 784
	//Math.floor(Math.random()*cvsW/unit + 1)---number from 1 to 79
	//Math.floor(Math.random()*cvsW/unit + 1)*unit---number from 1 to 784(but it's a multiple of unit)
	//Math.floor(Math.random()*(cvsW/unit - 1)+1)*unit---same as above but -1 keeps food inside canvas
	x : Math.floor(Math.random()*(cvsW/unit - 1)+1)*unit-unit/2,
	y : Math.floor(Math.random()*(cvsH/unit - 1)+1)*unit-unit/2
}

//create a variable to store the direction of the snake
let direction;

//add event to read users input then change direction
document.addEventListener('keydown', (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';
})

function draw() {
	//clear canvas and redraw snake 
	ctx.clearRect(0, 0, cvsW, cvsH);
	for(let i = 0; i < snake.length; i++) {
		ctx.fillStyle = 'limegreen';
		ctx.fillRect(snake[i].x-unit/2, snake[i].y-unit/2, unit, unit);
	}
	//draw food
	ctx.fillStyle = 'red';
	ctx.fillRect(food.x-unit/2, food.y-unit/2, unit, unit);

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

	//move snake in chosen direction
	if(direction == 'left') headX -= unit;
	else if(direction == 'right') headX += unit;
	else if(direction == 'up') headY -= unit;
	else if(direction == 'down') headY += unit;

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

	//check to see if snakes eaten food
	if(headX === food.x && headY === food.y) {
		food = {
			x : Math.floor(Math.random()*(cvsW/unit - 1)+1)*unit-unit/2,
			y : Math.floor(Math.random()*(cvsH/unit - 1)+1)*unit-unit/2
		}
		//create 4 new units
		for(let i = 4; i > 0; i--) {
			//add those units -without this code snake will not grow 
			snake.unshift(newHead);
		}
	} else {
		//remove tail -without this code snake will keep growing
		snake.pop();
	}
	//add new head position -without this code snake will not move
	snake.unshift(newHead);

	//check to see if snake has hit a wall or itself
	if(headX < 0 || headX > cvsW || headY < 0 || headY > cvsH || collision(headX, headY)) {
		clearInterval(runGame);
	}
}

let runGame = setInterval(draw, 70);

function collision(x, y) {
	for(let i = 0; i < snake.length; i++) {
		if(x == snake[i].x && y == snake[i].y) return true;
	}
	return false;
}
<!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>

1 个答案:

答案 0 :(得分:0)

添加碰撞脚本以在检查与自身的碰撞时忽略蛇的前两个链接。

function selfcollision(x, y) {
    for(let i = 2; i < snake.length; i++) {
        if(x == snake[i].x && y == snake[i].y) return true;
    }
    return false;
}