1
0
Fork 0

Add orange power up

This commit is contained in:
Ethanell 2021-03-30 09:18:11 +02:00
parent b606b09508
commit 3244ae7046
4 changed files with 42 additions and 4 deletions

View file

@ -17,6 +17,6 @@
- [x] Increase speed every 5 points - [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 - [x] Pause game when press escape

View file

@ -31,6 +31,7 @@ export class Game {
this.onDie = null; this.onDie = null;
this.onGameOver = null; this.onGameOver = null;
this.baseLifeCooldown = this.lifeCooldown = 30000; this.baseLifeCooldown = this.lifeCooldown = 30000;
this.orangeCooldown = this.baseOrangeCooldown = 60000;
} }
/** /**
@ -129,6 +130,7 @@ export class Game {
try { try {
this.moveSnake(); this.moveSnake();
this.lifeGenerator(); this.lifeGenerator();
this.orangeGenerator();
this.drawGrid(); this.drawGrid();
} catch (err) { } catch (err) {
@ -180,6 +182,9 @@ export class Game {
case tiles.LIFE: case tiles.LIFE:
this.lives++; this.lives++;
break; break;
case tiles.ORANGE:
this.snake.shrink();
break;
default: default:
haveEat = false; haveEat = false;
} }
@ -227,7 +232,7 @@ export class Game {
lifeGenerator() { lifeGenerator() {
if (!this.world.find(v => v === tiles.LIFE)) { if (!this.world.find(v => v === tiles.LIFE)) {
if (this.lifeCooldown <= 0) { if (this.lifeCooldown <= 0) {
if (this.lives < 3 && 0.15 < Math.random()) { if (this.lives < 3 && Math.random() < 0.15) {
let pos = this.randCoords(); let pos = this.randCoords();
this.world[pos[0]][pos[1]].type = tiles.LIFE; 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 * Generate random coordinates of empty tiles

View file

@ -25,6 +25,7 @@ export class Snake {
} }
this.eating = false; 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)) if (this.body.find(([x,y]) => pos[0] === x && pos[1] === y))
throw new GameOver(); 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() { eat() {
this.eating = true; this.eating = true;
} }
/**
* Enable narrowing
*/
shrink() {
this.narrowing = true;
}
} }
export const directions = { export const directions = {

View file

@ -190,6 +190,10 @@ export class Tile {
this.game.ctx.fillStyle = "#e400ff"; this.game.ctx.fillStyle = "#e400ff";
this.game.ctx.fillRect(...canvasPos, ...size); this.game.ctx.fillRect(...canvasPos, ...size);
break; break;
case tiles.ORANGE:
this.game.ctx.fillStyle = "#ff7500";
this.game.ctx.fillRect(...canvasPos, ...size);
break;
} }
this.game.ctx.stroke(); this.game.ctx.stroke();
@ -265,5 +269,6 @@ export const tiles = {
HEAD: "head", HEAD: "head",
BODY: "body", BODY: "body",
TAIL: "tail", TAIL: "tail",
LIFE: "life" LIFE: "life",
ORANGE: "orange"
}; };