Changing apple generation
This commit is contained in:
parent
bc23ae7722
commit
af9f4c670e
1 changed files with 5 additions and 16 deletions
|
@ -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
|
||||
|
|
Reference in a new issue