1
0
Fork 0

Add life and orange power up toggle config

This commit is contained in:
Ethanell 2021-03-30 09:23:18 +02:00
parent 3244ae7046
commit 896347025c

View file

@ -22,6 +22,9 @@ export class Game {
this.direction = directions.RIGHT; this.direction = directions.RIGHT;
this.snakeSpeed = this.baseSnakeSpeed = 500; this.snakeSpeed = this.baseSnakeSpeed = 500;
this.lives = 3; this.lives = 3;
this.walls = true;
this.life = true;
this.orange = true;
this.world = []; this.world = [];
this.score = 0; this.score = 0;
@ -129,7 +132,9 @@ export class Game {
main() { main() {
try { try {
this.moveSnake(); this.moveSnake();
if (this.life)
this.lifeGenerator(); this.lifeGenerator();
if (this.orange)
this.orangeGenerator(); this.orangeGenerator();
this.drawGrid(); this.drawGrid();
@ -260,7 +265,6 @@ export class Game {
} }
} }
/** /**
* Generate random coordinates of empty tiles * Generate random coordinates of empty tiles
* @returns {[int, int]} * @returns {[int, int]}
@ -285,8 +289,11 @@ export class Game {
* @param {int} snakeSpeed * @param {int} snakeSpeed
* @param {int} appleSpeed * @param {int} appleSpeed
* @param {int} lives * @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) 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; this.size = size;
else else
@ -312,8 +319,20 @@ export class Game {
else else
throw new InvalidGameOption("lives"); throw new InvalidGameOption("lives");
if (walls && typeof walls === "boolean") if (typeof walls === "boolean")
this.walls = walls 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");
} }
} }