1
0
Fork 0

Speed up when score hit a multiply of 5

This commit is contained in:
Ethanell 2021-03-26 23:29:58 +01:00
parent 2f08db7f09
commit 2199a6036c

View file

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