From af9f4c670ee5defdba7370fa004a1b660b9b3f43 Mon Sep 17 00:00:00 2001 From: flifloo Date: Thu, 25 Mar 2021 10:13:26 +0100 Subject: [PATCH] Changing apple generation --- sources/js/Game.js | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/sources/js/Game.js b/sources/js/Game.js index 6498550..a6dc5c8 100644 --- a/sources/js/Game.js +++ b/sources/js/Game.js @@ -16,11 +16,9 @@ export class Game { this.size = [15, 15]; this.direction = directions.RIGHT; this.snakeSpeed = 500; - this.appleSpeed = 5000; this.lives = 3; this.world = []; - this.apple = false; this.score = 0; this.onStart = null; this.onStop = null; @@ -90,9 +88,9 @@ export class Game { this.updateSnake(); this.drawGrid(); this.initCanvas(); + this.appleGenerator(); this.mainInterval = setInterval(() => this.main(), this.snakeSpeed); - this.appleInterval = setInterval(() => this.appleGenerator(), this.appleSpeed); } /** @@ -100,7 +98,6 @@ export class Game { */ stop() { clearInterval(this.mainInterval); - clearInterval(this.appleInterval); // Clear the grid this.ctx.globalCompositeOperation = "destination-out"; @@ -156,7 +153,7 @@ export class Game { const t = this.world[x][y]; if (t.type === tiles.APPLE) { this.snake.eat(); - this.apple = false; + this.appleGenerator(); this.score++; if (this.onEat && typeof this.onEat === "function") this.onEat(this.score); @@ -184,12 +181,9 @@ export class Game { * Main loop fir apple generation */ appleGenerator() { - if (!this.apple) { - let pos = this.randCoords(); + let pos = this.randCoords(); - this.world[pos[0]][pos[1]].type = tiles.APPLE; - this.apple = true; - } + this.world[pos[0]][pos[1]].type = tiles.APPLE; } /** @@ -217,7 +211,7 @@ export class Game { * @param {int} appleSpeed * @param {int} lives */ - load({size = [15, 15], direction = directions.RIGHT, snakeSpeed = 500, appleSpeed = 5000, lives = 3} = {}) { + load({size = [15, 15], direction = directions.RIGHT, snakeSpeed = 500, lives = 3} = {}) { if (size && Array.isArray(size) && size.length === 2 && size.filter(s => typeof s === "number" && s > 0 && s % 1 === 0).length === size.length) this.size = size; else @@ -233,11 +227,6 @@ export class Game { else throw new InvalidGameOption("snakeSpeed"); - if (appleSpeed && typeof appleSpeed === "number" && appleSpeed > 0 && appleSpeed % 1 === 0) - this.appleSpeed = appleSpeed; - else - throw new InvalidGameOption("appleSpeed"); - if (lives && typeof lives === "number" && lives > 0 && lives % 1 === 0) this.lives = lives; else