1
0
Fork 0

Changing apple generation

This commit is contained in:
Ethanell 2021-03-25 10:13:26 +01:00
parent bc23ae7722
commit af9f4c670e

View file

@ -16,11 +16,9 @@ export class Game {
this.size = [15, 15]; this.size = [15, 15];
this.direction = directions.RIGHT; this.direction = directions.RIGHT;
this.snakeSpeed = 500; this.snakeSpeed = 500;
this.appleSpeed = 5000;
this.lives = 3; this.lives = 3;
this.world = []; this.world = [];
this.apple = false;
this.score = 0; this.score = 0;
this.onStart = null; this.onStart = null;
this.onStop = null; this.onStop = null;
@ -90,9 +88,9 @@ export class Game {
this.updateSnake(); this.updateSnake();
this.drawGrid(); this.drawGrid();
this.initCanvas(); this.initCanvas();
this.appleGenerator();
this.mainInterval = setInterval(() => this.main(), this.snakeSpeed); this.mainInterval = setInterval(() => this.main(), this.snakeSpeed);
this.appleInterval = setInterval(() => this.appleGenerator(), this.appleSpeed);
} }
/** /**
@ -100,7 +98,6 @@ export class Game {
*/ */
stop() { stop() {
clearInterval(this.mainInterval); clearInterval(this.mainInterval);
clearInterval(this.appleInterval);
// Clear the grid // Clear the grid
this.ctx.globalCompositeOperation = "destination-out"; this.ctx.globalCompositeOperation = "destination-out";
@ -156,7 +153,7 @@ export class Game {
const t = this.world[x][y]; const t = this.world[x][y];
if (t.type === tiles.APPLE) { if (t.type === tiles.APPLE) {
this.snake.eat(); this.snake.eat();
this.apple = false; this.appleGenerator();
this.score++; this.score++;
if (this.onEat && typeof this.onEat === "function") if (this.onEat && typeof this.onEat === "function")
this.onEat(this.score); this.onEat(this.score);
@ -184,12 +181,9 @@ export class Game {
* Main loop fir apple generation * Main loop fir apple generation
*/ */
appleGenerator() { appleGenerator() {
if (!this.apple) { let pos = this.randCoords();
let pos = this.randCoords();
this.world[pos[0]][pos[1]].type = tiles.APPLE; this.world[pos[0]][pos[1]].type = tiles.APPLE;
this.apple = true;
}
} }
/** /**
@ -217,7 +211,7 @@ export class Game {
* @param {int} appleSpeed * @param {int} appleSpeed
* @param {int} lives * @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) 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
@ -233,11 +227,6 @@ export class Game {
else else
throw new InvalidGameOption("snakeSpeed"); 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) if (lives && typeof lives === "number" && lives > 0 && lives % 1 === 0)
this.lives = lives; this.lives = lives;
else else