From 6029210098d4a8f10b4b002ec8aa536087f9fb16 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Mon, 23 Mar 2020 18:04:59 +0100 Subject: [PATCH] Snake can accelerate --- levels/2.json | 2 ++ script.js | 12 +++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) 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 = "#"; }