Snake can accelerate
This commit is contained in:
parent
4aebcb3f3d
commit
6029210098
@ -1,4 +1,6 @@
|
||||
{
|
||||
"minDelay": 100,
|
||||
"acceleration": 1,
|
||||
"dimensions": [40, 40],
|
||||
"delay": 200,
|
||||
"walls": [
|
||||
|
12
script.js
12
script.js
@ -134,12 +134,13 @@ async function startGame(gameID) {
|
||||
* I placed this function here to inherit
|
||||
* the map, snake, canvas & ctx variables...
|
||||
*/
|
||||
let interval = setInterval(() => step(), level.delay);
|
||||
let currDelay = level.delay;
|
||||
setTimeout(() => step(), currDelay);
|
||||
function step() {
|
||||
|
||||
// Check if a game was destroyed
|
||||
if(!canvas.isConnected)
|
||||
clearInterval(interval)
|
||||
return;
|
||||
|
||||
// Move the snake if required
|
||||
if(key) {
|
||||
@ -261,13 +262,18 @@ async function startGame(gameID) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if(level.hasOwnProperty("acceleration"))
|
||||
currDelay -= level.acceleration;
|
||||
if(level.hasOwnProperty("minDelay") && currDelay <= level.minDelay)
|
||||
currDelay = level.minDelay
|
||||
setTimeout(() => step(), currDelay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call this function once the user loose the game
|
||||
*/
|
||||
function gameOver() {
|
||||
clearInterval(interval);
|
||||
alert("Game over !!!");
|
||||
location.href = "#";
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user