From b606b09508c837eb75ce8a5a6ea9f3e0acb5c598 Mon Sep 17 00:00:00 2001 From: flifloo Date: Tue, 30 Mar 2021 09:05:38 +0200 Subject: [PATCH] Add life power up --- README.md | 4 +++- sources/js/Game.js | 51 ++++++++++++++++++++++++++++++++++++--------- sources/js/Tile.js | 7 ++++++- sources/js/index.js | 1 + 4 files changed, 51 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 943d115..e15ee7b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/sources/js/Game.js b/sources/js/Game.js index a641870..2941c64 100644 --- a/sources/js/Game.js +++ b/sources/js/Game.js @@ -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]} diff --git a/sources/js/Tile.js b/sources/js/Tile.js index 0a6837b..03e7394 100644 --- a/sources/js/Tile.js +++ b/sources/js/Tile.js @@ -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" }; diff --git a/sources/js/index.js b/sources/js/index.js index 3b19ba7..4e676f3 100644 --- a/sources/js/index.js +++ b/sources/js/index.js @@ -62,6 +62,7 @@ game.onStop = () => { game.onEat = (score) => { eatSound.play(); updateScore(score); + updateLives(game.lives); }; game.onDie = lives => {