1
0
Fork 0

Add life power up

This commit is contained in:
Ethanell 2021-03-30 09:05:38 +02:00
parent cad95c81c6
commit b606b09508
4 changed files with 51 additions and 12 deletions

View file

@ -9,7 +9,7 @@
- [x] Life - [x] Life
- [ ] Heart item to regain a life - [x] Heart item to regain a life
- [x] No wall option - [x] No wall option
@ -18,3 +18,5 @@
- [x] Increase speed every 5 points - [x] Increase speed every 5 points
- [ ] Power up to decrease the snake size - [ ] Power up to decrease the snake size
- [x] Pause game when press escape

View file

@ -30,6 +30,7 @@ export class Game {
this.onEat = null; this.onEat = null;
this.onDie = null; this.onDie = null;
this.onGameOver = null; this.onGameOver = null;
this.baseLifeCooldown = this.lifeCooldown = 30000;
} }
/** /**
@ -127,7 +128,9 @@ export class Game {
main() { main() {
try { try {
this.moveSnake(); this.moveSnake();
this.lifeGenerator();
this.drawGrid(); this.drawGrid();
} catch (err) { } catch (err) {
if (err instanceof GameOver) { if (err instanceof GameOver) {
this.lives--; 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])]; this.snake.body[i] = [mod_floor(x, this.size[0]), mod_floor(y, this.size[1])];
else { else {
const t = this.world[x][y]; const t = this.world[x][y];
if (t.type === tiles.APPLE) { let haveEat = true;
this.snake.eat(); switch (t.type) {
this.appleGenerator(); case tiles.APPLE:
this.score++; this.snake.eat();
if (!(this.score % 5) && this.snakeSpeed > 50) { this.appleGenerator();
this.snakeSpeed -= 10; this.score++;
} if (!(this.score % 5) && this.snakeSpeed > 50) {
if (this.onEat && typeof this.onEat === "function") this.snakeSpeed -= 10;
this.onEat(this.score); }
break;
case tiles.LIFE:
this.lives++;
break;
default:
haveEat = false;
} }
if (haveEat && this.onEat && typeof this.onEat === "function")
this.onEat(this.score);
if (!i) if (!i)
t.type = tiles.HEAD; t.type = tiles.HEAD;
else if (i === this.snake.body.length-1) 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() { appleGenerator() {
let pos = this.randCoords(); let pos = this.randCoords();
@ -208,6 +221,24 @@ export class Game {
this.world[pos[0]][pos[1]].type = tiles.APPLE; 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 * Generate random coordinates of empty tiles
* @returns {[int, int]} * @returns {[int, int]}

View file

@ -186,6 +186,10 @@ export class Tile {
this.game.ctx.setTransform(1, 0, 0, 1, 0, 0); this.game.ctx.setTransform(1, 0, 0, 1, 0, 0);
} }
break; break;
case tiles.LIFE:
this.game.ctx.fillStyle = "#e400ff";
this.game.ctx.fillRect(...canvasPos, ...size);
break;
} }
this.game.ctx.stroke(); this.game.ctx.stroke();
@ -260,5 +264,6 @@ export const tiles = {
APPLE: "apple", APPLE: "apple",
HEAD: "head", HEAD: "head",
BODY: "body", BODY: "body",
TAIL: "tail" TAIL: "tail",
LIFE: "life"
}; };

View file

@ -62,6 +62,7 @@ game.onStop = () => {
game.onEat = (score) => { game.onEat = (score) => {
eatSound.play(); eatSound.play();
updateScore(score); updateScore(score);
updateLives(game.lives);
}; };
game.onDie = lives => { game.onDie = lives => {