added fire and a stub for level 5

This commit is contained in:
Nathan DECHER
2020-03-26 19:26:47 +01:00
parent f050baafbc
commit 02e72795cc
7 changed files with 88 additions and 9 deletions
+11 -2
View File
@@ -1,4 +1,4 @@
const [EMPTY, FOOD, WALL, HOLE, HOLE_S, SNAKE]=Array(6).keys();
const [EMPTY, FOOD, WALL, FIRE, HOLE, HOLE_S, SNAKE]=Array(7).keys();
class SnekGame {
constructor(settings, canvas, rules) {
@@ -19,6 +19,7 @@ class SnekGame {
case 'f': return FOOD;
case 'w': return WALL;
case 'o': return HOLE;
case 'i': return FIRE;
}
})();
}
@@ -53,6 +54,9 @@ class SnekGame {
// add the holes
if(settings.holes) settings.holes.forEach(([x, y]) => this.world[x][y]=HOLE);
// add the fires
if(settings.fires) settings.fires.forEach(([x, y]) => this.world[x][y]=FIRE);
// add the food
settings.food.forEach(([x, y]) => this.world[x][y]=FOOD);
this.fruits=[...settings.food];
@@ -122,7 +126,7 @@ class SnekGame {
// draw our walls
const wall=assets.get('wall');
const hole=assets.get('hole');
const fire=assets.get('fire');
const putTile=(x, y, tile) => this.ctx.drawImage(
tile,
offsetX+cellSize*x,
@@ -149,6 +153,10 @@ class SnekGame {
putTile(x, y, wall);
break;
case FIRE:
putTile(x, y, fire[Math.floor(Date.now()/1000*60)%60]);
break;
case HOLE:
case HOLE_S: {
putTile(x, y, hole.base);
@@ -253,6 +261,7 @@ class SnekGame {
switch(this.world[head[0]][head[1]]) {
// you hit, you die
case WALL:
case FIRE:
case SNAKE:
case HOLE_S:
return this.die();