if语句在我的JS蛇游戏中不起作用

时间:2019-01-20 05:45:00

标签: 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,
	y : Math.floor(Math.random()*(cvsH/unit - 1)+1)*unit
}

//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 and food
	ctx.clearRect(0, 0, cvsW, cvsH);
	ctx.fillStyle = 'limegreen';
	ctx.fillRect(snake[0].x-unit/2, snake[0].y-unit/2, unit, unit);
	ctx.fillStyle = 'red';
	ctx.fillRect(food.x, food.y, unit, unit);

	//move snake in chosen direction
	changeDirection();

	//check to see if snakes eaten
	if(snake[0].x === food.x && snake[0].y === food.y) {
		alert('yo');
	}

	//check to see if snake has hit a wall
	collision(snake[0].x, snake[0].y);
}


let runGame = setInterval(draw, 70);

function changeDirection() {
	if(direction == 'left') snake[0].x -= unit;
	else if(direction == 'right') snake[0].x += unit;
	else if(direction == 'up') snake[0].y -= unit;
	else if(direction == 'down') snake[0].y += unit;
}


function collision(x, y) {
	if(x < 0 || x > cvsW || y < 0 || y > cvsH) {
		clearInterval(runGame);
	}
}
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Snake</title>
	<style>
		body {
			background-color: #333;
		}

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

1 个答案:

答案 0 :(得分:0)

问题似乎在这里

df.groupby('COL1').COL2.nunique().to_frame().query('COL2 > 1')

      COL2
COL1      
B        2

在这种情况下,方向为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'; }) ,因此不会为undefined分配任何值