From 896347025c231d9bc662bb08b5d5b0af41e5b8c5 Mon Sep 17 00:00:00 2001 From: flifloo Date: Tue, 30 Mar 2021 09:23:18 +0200 Subject: [PATCH] Add life and orange power up toggle config --- sources/js/Game.js | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/sources/js/Game.js b/sources/js/Game.js index 22938a4..e728d9a 100644 --- a/sources/js/Game.js +++ b/sources/js/Game.js @@ -22,6 +22,9 @@ export class Game { this.direction = directions.RIGHT; this.snakeSpeed = this.baseSnakeSpeed = 500; this.lives = 3; + this.walls = true; + this.life = true; + this.orange = true; this.world = []; this.score = 0; @@ -129,8 +132,10 @@ export class Game { main() { try { this.moveSnake(); - this.lifeGenerator(); - this.orangeGenerator(); + if (this.life) + this.lifeGenerator(); + if (this.orange) + this.orangeGenerator(); this.drawGrid(); } catch (err) { @@ -260,7 +265,6 @@ export class Game { } } - /** * Generate random coordinates of empty tiles * @returns {[int, int]} @@ -285,8 +289,11 @@ export class Game { * @param {int} snakeSpeed * @param {int} appleSpeed * @param {int} lives + * @param {boolean} walls + * @param {boolean} life + * @param {boolean} orange */ - load({size = [15, 15], snakeSize = 4, direction = directions.RIGHT, snakeSpeed = 500, lives = 3, walls = true} = {}) { + load({size = [15, 15], snakeSize = 4, direction = directions.RIGHT, snakeSpeed = 500, lives = 3, walls = true, life = true, orange = true} = {}) { 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 @@ -312,8 +319,20 @@ export class Game { else throw new InvalidGameOption("lives"); - if (walls && typeof walls === "boolean") - this.walls = walls + if (typeof walls === "boolean") + this.walls = walls; + else + throw new InvalidGameOption("walls"); + + if (typeof life === "boolean") + this.life = life; + else + throw new InvalidGameOption("life"); + + if (typeof orange === "boolean") + this.orange = orange; + else + throw new InvalidGameOption("orange"); } }