Add random food & walls
This commit is contained in:
parent
380abb24b0
commit
24815897e5
@ -4,6 +4,7 @@
|
|||||||
"walls": [
|
"walls": [
|
||||||
[5,5], [5,6], [5,7], [5,8], [40, 35], [38, 35], [23, 35]
|
[5,5], [5,6], [5,7], [5,8], [40, 35], [38, 35], [23, 35]
|
||||||
],
|
],
|
||||||
|
"randWallFreq": 5000,
|
||||||
"food": [
|
"food": [
|
||||||
[10,10],
|
[10,10],
|
||||||
[11,10],
|
[11,10],
|
||||||
@ -13,6 +14,7 @@
|
|||||||
[20, 23],
|
[20, 23],
|
||||||
[20, 21]
|
[20, 21]
|
||||||
],
|
],
|
||||||
|
"randFoodFreq": 2000,
|
||||||
"snake": [
|
"snake": [
|
||||||
[4,2], [4,1], [3,1], [2,1]
|
[4,2], [4,1], [3,1], [2,1]
|
||||||
],
|
],
|
||||||
|
36
script.js
36
script.js
@ -41,6 +41,16 @@ function drawLine(ctx, x1, y1, x2, y2) {
|
|||||||
ctx.stroke();
|
ctx.stroke();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a random number
|
||||||
|
*
|
||||||
|
* @param {Number} min Minimum value
|
||||||
|
* @param {Number} max Maximum value
|
||||||
|
*/
|
||||||
|
function randInt(min, max) {
|
||||||
|
return Math.floor((Math.random()*max) + min)
|
||||||
|
}
|
||||||
|
|
||||||
// Get elements
|
// Get elements
|
||||||
const startScreen = byId("startScreen")
|
const startScreen = byId("startScreen")
|
||||||
const gameScreen = byId("gameScreen")
|
const gameScreen = byId("gameScreen")
|
||||||
@ -267,6 +277,32 @@ async function startGame(gameID) {
|
|||||||
if(["ArrowDown", "ArrowLeft", "ArrowRight", "ArrowUp"].includes(ev.key))
|
if(["ArrowDown", "ArrowLeft", "ArrowRight", "ArrowUp"].includes(ev.key))
|
||||||
key = ev.key;
|
key = ev.key;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Automatically generate new map element if required
|
||||||
|
if(level.randWallFreq) {
|
||||||
|
const int = setInterval(() => {
|
||||||
|
if(!canvas.isConnected)
|
||||||
|
clearInterval(int);
|
||||||
|
else {
|
||||||
|
const pos = [randInt(0, map.length), randInt(0, map[0].length)]
|
||||||
|
if(map[pos[0]][pos[1]] == EMPTY)
|
||||||
|
map[pos[0]][pos[1]] = WALL;
|
||||||
|
}
|
||||||
|
}, level.randWallFreq);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Automatically generate new map element if required
|
||||||
|
if(level.randFoodFreq) {
|
||||||
|
const int = setInterval(() => {
|
||||||
|
if(!canvas.isConnected)
|
||||||
|
clearInterval(int);
|
||||||
|
else {
|
||||||
|
const pos = [randInt(0, map.length), randInt(0, map[0].length)]
|
||||||
|
if(map[pos[0]][pos[1]] == EMPTY)
|
||||||
|
map[pos[0]][pos[1]] = FOOD;
|
||||||
|
}
|
||||||
|
}, level.randFoodFreq);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user