Fix som error on tiny map & snake size
This commit is contained in:
parent
58806617e6
commit
120e58e88e
4 changed files with 29 additions and 9 deletions
|
@ -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
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -95,9 +95,14 @@ function loadLevels(levels) {
|
|||
}
|
||||
|
||||
function loadGame(data) {
|
||||
try {
|
||||
game.load(data);
|
||||
game.start();
|
||||
startSound.play();
|
||||
} catch (e) {
|
||||
alert("Error on level loading: "+e);
|
||||
window.location.reload();
|
||||
}
|
||||
}
|
||||
|
||||
function updateScore(s) {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue