From 3244ae7046e38e5f7c7a856febd0223533d04e1f Mon Sep 17 00:00:00 2001 From: flifloo Date: Tue, 30 Mar 2021 09:18:11 +0200 Subject: [PATCH] Add orange power up --- README.md | 2 +- sources/js/Game.js | 24 +++++++++++++++++++++++- sources/js/Snake.js | 13 ++++++++++++- sources/js/Tile.js | 7 ++++++- 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e15ee7b..24722c6 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,6 @@ - [x] Increase speed every 5 points -- [ ] Power up to decrease the snake size +- [x] 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 2941c64..22938a4 100644 --- a/sources/js/Game.js +++ b/sources/js/Game.js @@ -31,6 +31,7 @@ export class Game { this.onDie = null; this.onGameOver = null; this.baseLifeCooldown = this.lifeCooldown = 30000; + this.orangeCooldown = this.baseOrangeCooldown = 60000; } /** @@ -129,6 +130,7 @@ export class Game { try { this.moveSnake(); this.lifeGenerator(); + this.orangeGenerator(); this.drawGrid(); } catch (err) { @@ -180,6 +182,9 @@ export class Game { case tiles.LIFE: this.lives++; break; + case tiles.ORANGE: + this.snake.shrink(); + break; default: haveEat = false; } @@ -227,7 +232,7 @@ export class Game { lifeGenerator() { if (!this.world.find(v => v === tiles.LIFE)) { if (this.lifeCooldown <= 0) { - if (this.lives < 3 && 0.15 < Math.random()) { + if (this.lives < 3 && Math.random() < 0.15) { let pos = this.randCoords(); this.world[pos[0]][pos[1]].type = tiles.LIFE; @@ -238,6 +243,23 @@ export class Game { } } + /** + * Handel the orange generation + */ + orangeGenerator() { + if (!this.world.find(v => v === tiles.ORANGE)) { + if (this.orangeCooldown <= 0) { + if (Math.random() < 0.05) { + let pos = this.randCoords(); + + this.world[pos[0]][pos[1]].type = tiles.ORANGE; + this.orangeCooldown = this.baseOrangeCooldown; + } + } else + this.orangeCooldown -= this.snakeSpeed; + } + } + /** * Generate random coordinates of empty tiles diff --git a/sources/js/Snake.js b/sources/js/Snake.js index 698c024..3dd7689 100644 --- a/sources/js/Snake.js +++ b/sources/js/Snake.js @@ -25,6 +25,7 @@ export class Snake { } this.eating = false; + this.narrowing = false; } /** @@ -42,7 +43,10 @@ export class Snake { if (this.body.find(([x,y]) => pos[0] === x && pos[1] === y)) throw new GameOver(); - this.body.unshift(pos); + if (!this.narrowing) + this.body.unshift(pos); + else + this.narrowing = false; } /** @@ -51,6 +55,13 @@ export class Snake { eat() { this.eating = true; } + + /** + * Enable narrowing + */ + shrink() { + this.narrowing = true; + } } export const directions = { diff --git a/sources/js/Tile.js b/sources/js/Tile.js index 03e7394..b3da897 100644 --- a/sources/js/Tile.js +++ b/sources/js/Tile.js @@ -190,6 +190,10 @@ export class Tile { this.game.ctx.fillStyle = "#e400ff"; this.game.ctx.fillRect(...canvasPos, ...size); break; + case tiles.ORANGE: + this.game.ctx.fillStyle = "#ff7500"; + this.game.ctx.fillRect(...canvasPos, ...size); + break; } this.game.ctx.stroke(); @@ -265,5 +269,6 @@ export const tiles = { HEAD: "head", BODY: "body", TAIL: "tail", - LIFE: "life" + LIFE: "life", + ORANGE: "orange" };