diff --git a/sources/js/Game.js b/sources/js/Game.js index a6dc5c8..5edae10 100644 --- a/sources/js/Game.js +++ b/sources/js/Game.js @@ -15,7 +15,7 @@ export class Game { this.size = [15, 15]; this.direction = directions.RIGHT; - this.snakeSpeed = 500; + this.snakeSpeed = this.baseSnakeSpeed = 500; this.lives = 3; this.world = []; @@ -82,6 +82,7 @@ export class Game { this.onStart(); this.direction = this.lastDirection = this.startDirection; + this.snakeSpeed = this.baseSnakeSpeed; this.snake = new Snake({startPos: this.size.map(s => Math.floor(s/2))}); this.initWorld(); @@ -90,14 +91,14 @@ export class Game { this.initCanvas(); this.appleGenerator(); - this.mainInterval = setInterval(() => this.main(), this.snakeSpeed); + setTimeout(() => this.main(), this.snakeSpeed); } /** * Stop the party */ stop() { - clearInterval(this.mainInterval); + this.mainBreak = true; // Clear the grid this.ctx.globalCompositeOperation = "destination-out"; @@ -140,6 +141,10 @@ export class Game { alert("An error occurred !"); } } + if (!this.mainBreak) + setTimeout(() => this.main(), this.snakeSpeed); + else + this.mainBreak = false; } /** @@ -155,6 +160,9 @@ export class Game { this.snake.eat(); this.appleGenerator(); this.score++; + if (!(this.score%5) && this.snakeSpeed > 50) { + this.snakeSpeed -= 10; + } if (this.onEat && typeof this.onEat === "function") this.onEat(this.score); } @@ -223,7 +231,7 @@ export class Game { throw new InvalidGameOption("direction"); if (snakeSpeed && typeof snakeSpeed === "number" && snakeSpeed > 0 && snakeSpeed % 1 === 0) - this.snakeSpeed = snakeSpeed; + this.snakeSpeed = this.baseSnakeSpeed = snakeSpeed; else throw new InvalidGameOption("snakeSpeed");