diff --git a/levels/2.json b/levels/2.json index 9a5163e..d3d9089 100644 --- a/levels/2.json +++ b/levels/2.json @@ -1,4 +1,6 @@ { + "minDelay": 100, + "acceleration": 1, "dimensions": [40, 40], "delay": 200, "walls": [ diff --git a/script.js b/script.js index 8279979..97ba105 100644 --- a/script.js +++ b/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 = "#"; }