diff --git a/sources/js/Game.js b/sources/js/Game.js index f13bc1b..3155e6b 100644 --- a/sources/js/Game.js +++ b/sources/js/Game.js @@ -18,6 +18,7 @@ export class Game { throw new InvalidGameOption("canvas"); this.size = [15, 15]; + this.snakeSize = 4; this.direction = directions.RIGHT; this.snakeSpeed = this.baseSnakeSpeed = 500; this.lives = 3; @@ -87,7 +88,7 @@ export class Game { 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)), size: this.snakeSize, startDirection: this.startDirection}); this.initWorld(); this.updateSnake(); @@ -156,8 +157,7 @@ export class Game { */ updateSnake() { - for (const i in this.snake.body) { - const [x, y] = this.snake.body[i]; + this.snake.body.forEach(([x,y], i) => { if (!(x in this.world) || !(y in this.world[x])) if (this.walls) throw new GameOver(); @@ -177,7 +177,7 @@ export class Game { } t.type = tiles.SNAKE; } - } + }); } /** @@ -229,12 +229,17 @@ export class Game { * @param {int} appleSpeed * @param {int} lives */ - load({size = [15, 15], direction = directions.RIGHT, snakeSpeed = 500, lives = 3, walls = true} = {}) { + load({size = [15, 15], snakeSize = 4, direction = directions.RIGHT, snakeSpeed = 500, lives = 3, walls = 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 throw new InvalidGameOption("size"); + if (snakeSize && typeof snakeSize === "number" && snakeSize > 0 && snakeSize % 1 === 0) + this.snakeSize = snakeSize; + else + throw new InvalidGameOption("snakeSize"); + if (direction && Object.values(directions).find(([x,y]) => direction[0] === x && direction[1] === y)) this.direction = this.startDirection = this.lastDirection = direction; else diff --git a/sources/js/Snake.js b/sources/js/Snake.js index 40192d3..698c024 100644 --- a/sources/js/Snake.js +++ b/sources/js/Snake.js @@ -11,7 +11,7 @@ export class Snake { else throw new InvalidSnakeOption("startPos"); - if (size && typeof size === "number" && size > 0 && size % 1 === 0) + if (size && typeof size === "number" && size > 1 && size % 1 === 0) this.size = size; else throw new InvalidSnakeOption("size"); diff --git a/sources/js/index.js b/sources/js/index.js index 230d2f9..3e156d9 100644 --- a/sources/js/index.js +++ b/sources/js/index.js @@ -95,9 +95,14 @@ function loadLevels(levels) { } function loadGame(data) { - game.load(data); - game.start(); - startSound.play(); + try { + game.load(data); + game.start(); + startSound.play(); + } catch (e) { + alert("Error on level loading: "+e); + window.location.reload(); + } } function updateScore(s) { diff --git a/sources/levels.json b/sources/levels.json index bff0f7a..14e1ea0 100644 --- a/sources/levels.json +++ b/sources/levels.json @@ -17,5 +17,15 @@ "No walls": { "snakeSpeed": 100, "walls": false + }, + "Tiny": { + "snakeSpeed": 500, + "size": [3,3], + "snakeSize": 2 + }, + "Big": { + "snakeSpeed": 100, + "size": [50,50], + "snakeSize": 10 } }