[英]HTML5 Snake Game - add multiple food
I have followedthis guide to make my snake game.我已经按照本指南制作了我的蛇游戏。
But now I want to add more then one food into the map.但现在我想在地图中添加更多的食物。
Well, I tried putting rows with makeFoodItem();
好吧,我尝试使用makeFoodItem();
放置行makeFoodItem();
instead of just the default 1 row.而不仅仅是默认的 1 行。 So like:所以像:
makeFoodItem();
makeFoodItem();
makeFoodItem();
makeFoodItem();
makeFoodItem();
Which also made 5 foods.这也做了5种食物。 But the score wasn't added when the snake got them, and they was just removed, and didn't added a new one when it was taken.但是snake拿到的时候分数没有加,只是拿掉了,拿的时候也没加新的。
So I went into looking at the Function makeFoodItem()
, which looks like this:所以我开始研究函数makeFoodItem()
,它看起来像这样:
function makeFoodItem(){
suggestedPoint = [Math.floor(Math.random()*(canvas.width/gridSize))*gridSize, Math.floor(Math.random()*(canvas.height/gridSize))*gridSize];
if (snakeBody.some(hasPoint)) {
makeFoodItem();
} else {
ctx.fillStyle = "rgb(10,100,0)";
ctx.fillRect(suggestedPoint[0], suggestedPoint[1], gridSize, gridSize);
};
}
but I could not really figure out what to do there.但我真的不知道在那里做什么。
function spawnMultipleFood () {
for (let i = 0; i < spawnMoreFoodX.length; i++) {
let spawnMultipleX = spawnMoreFoodX[i]
for (let j = 0; j < spawnMoreFoodY.length; j++) {
let spawnMultipleY = spawnMoreFoodY[i]
innerBoard.fillStyle = "purple";
innerBoard.fillRect(spawnMultipleX * grid, spawnMultipleY * grid, gridSize, gridSize)
}
}
} }
/ my global vars for spawnMoreFoodX and spawnMoreFoodY are an array that I loop through and set that to another variable, then plug that var in to fillRect() / /我的 spawnMoreFoodX 和 spawnMoreFoodY 的全局变量是一个数组,我循环遍历并将其设置为另一个变量,然后将该变量插入到 fillRect() /
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.