Add life power up
This commit is contained in:
parent
cad95c81c6
commit
b606b09508
4 changed files with 51 additions and 12 deletions
|
@ -9,7 +9,7 @@
|
|||
|
||||
- [x] Life
|
||||
|
||||
- [ ] Heart item to regain a life
|
||||
- [x] Heart item to regain a life
|
||||
|
||||
- [x] No wall option
|
||||
|
||||
|
@ -18,3 +18,5 @@
|
|||
- [x] Increase speed every 5 points
|
||||
|
||||
- [ ] Power up to decrease the snake size
|
||||
|
||||
- [x] Pause game when press escape
|
||||
|
|
|
@ -30,6 +30,7 @@ export class Game {
|
|||
this.onEat = null;
|
||||
this.onDie = null;
|
||||
this.onGameOver = null;
|
||||
this.baseLifeCooldown = this.lifeCooldown = 30000;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -127,7 +128,9 @@ export class Game {
|
|||
main() {
|
||||
try {
|
||||
this.moveSnake();
|
||||
this.lifeGenerator();
|
||||
this.drawGrid();
|
||||
|
||||
} catch (err) {
|
||||
if (err instanceof GameOver) {
|
||||
this.lives--;
|
||||
|
@ -164,16 +167,26 @@ export class Game {
|
|||
this.snake.body[i] = [mod_floor(x, this.size[0]), mod_floor(y, this.size[1])];
|
||||
else {
|
||||
const t = this.world[x][y];
|
||||
if (t.type === tiles.APPLE) {
|
||||
this.snake.eat();
|
||||
this.appleGenerator();
|
||||
this.score++;
|
||||
if (!(this.score % 5) && this.snakeSpeed > 50) {
|
||||
this.snakeSpeed -= 10;
|
||||
}
|
||||
if (this.onEat && typeof this.onEat === "function")
|
||||
this.onEat(this.score);
|
||||
let haveEat = true;
|
||||
switch (t.type) {
|
||||
case tiles.APPLE:
|
||||
this.snake.eat();
|
||||
this.appleGenerator();
|
||||
this.score++;
|
||||
if (!(this.score % 5) && this.snakeSpeed > 50) {
|
||||
this.snakeSpeed -= 10;
|
||||
}
|
||||
break;
|
||||
case tiles.LIFE:
|
||||
this.lives++;
|
||||
break;
|
||||
default:
|
||||
haveEat = false;
|
||||
}
|
||||
|
||||
if (haveEat && this.onEat && typeof this.onEat === "function")
|
||||
this.onEat(this.score);
|
||||
|
||||
if (!i)
|
||||
t.type = tiles.HEAD;
|
||||
else if (i === this.snake.body.length-1)
|
||||
|
@ -200,7 +213,7 @@ export class Game {
|
|||
}
|
||||
|
||||
/**
|
||||
* Main loop fir apple generation
|
||||
* Generate an apple on the world
|
||||
*/
|
||||
appleGenerator() {
|
||||
let pos = this.randCoords();
|
||||
|
@ -208,6 +221,24 @@ export class Game {
|
|||
this.world[pos[0]][pos[1]].type = tiles.APPLE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handel the live generation
|
||||
*/
|
||||
lifeGenerator() {
|
||||
if (!this.world.find(v => v === tiles.LIFE)) {
|
||||
if (this.lifeCooldown <= 0) {
|
||||
if (this.lives < 3 && 0.15 < Math.random()) {
|
||||
let pos = this.randCoords();
|
||||
|
||||
this.world[pos[0]][pos[1]].type = tiles.LIFE;
|
||||
this.lifeCooldown = this.baseLifeCooldown;
|
||||
}
|
||||
} else
|
||||
this.lifeCooldown -= this.snakeSpeed;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generate random coordinates of empty tiles
|
||||
* @returns {[int, int]}
|
||||
|
|
|
@ -186,6 +186,10 @@ export class Tile {
|
|||
this.game.ctx.setTransform(1, 0, 0, 1, 0, 0);
|
||||
}
|
||||
break;
|
||||
case tiles.LIFE:
|
||||
this.game.ctx.fillStyle = "#e400ff";
|
||||
this.game.ctx.fillRect(...canvasPos, ...size);
|
||||
break;
|
||||
}
|
||||
|
||||
this.game.ctx.stroke();
|
||||
|
@ -260,5 +264,6 @@ export const tiles = {
|
|||
APPLE: "apple",
|
||||
HEAD: "head",
|
||||
BODY: "body",
|
||||
TAIL: "tail"
|
||||
TAIL: "tail",
|
||||
LIFE: "life"
|
||||
};
|
||||
|
|
|
@ -62,6 +62,7 @@ game.onStop = () => {
|
|||
game.onEat = (score) => {
|
||||
eatSound.play();
|
||||
updateScore(score);
|
||||
updateLives(game.lives);
|
||||
};
|
||||
|
||||
game.onDie = lives => {
|
||||
|
|
Reference in a new issue