Add orange power up
This commit is contained in:
parent
b606b09508
commit
3244ae7046
4 changed files with 42 additions and 4 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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();
|
||||
|
||||
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 = {
|
||||
|
|
|
@ -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"
|
||||
};
|
||||
|
|
Reference in a new issue