Snake can accelerate

This commit is contained in:
Pierre HUBERT 2020-03-23 18:04:59 +01:00
parent 4aebcb3f3d
commit 6029210098
2 changed files with 11 additions and 3 deletions

View File

@ -1,4 +1,6 @@
{
"minDelay": 100,
"acceleration": 1,
"dimensions": [40, 40],
"delay": 200,
"walls": [

View File

@ -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 = "#";
}