Start to add ice

This commit is contained in:
Pierre HUBERT 2020-03-29 16:59:56 +02:00
parent 8ba5361469
commit b84dff7f79
2 changed files with 34 additions and 2 deletions

View File

@ -16,6 +16,22 @@
[20, 23],
[20, 21]
],
"ice": [
[15,15],
[15,16],
[15,17],
[15,18],
[16,15],
[16,16],
[16,17],
[16,18],
[17,15],
[17,16],
[17,17],
[17,18]
],
"randFoodFreq": 2000,
"snake": [
[4,2], [4,1], [3,1], [2,1]

View File

@ -13,6 +13,8 @@ const EMPTY = i++;
const SNAKE = i++;
const WALL = i++;
const FOOD = i++;
const ICE = i++;
const ICE_SNAKE = i++; // If the snake is on the ice
delete i;
/**
@ -189,6 +191,11 @@ async function startGame(gameID) {
map[f[0]-1][f[1]-1] = FOOD
})
if(level.hasOwnProperty("ice"))
level.ice.forEach((f) => {
map[f[0]-1][f[1]-1] = ICE
})
level.snake.forEach((s) => {
map[s[0]-1][s[1]-1] = SNAKE
snake.push([s[0]-1, s[1]-1]);
@ -252,6 +259,7 @@ async function startGame(gameID) {
score++;
break;
case ICE_SNAKE:
case SNAKE:
case WALL:
gameOver();
@ -261,12 +269,12 @@ async function startGame(gameID) {
// Push new snake position
snake.push(newHead);
map[newHead[0]][newHead[1]] = SNAKE
map[newHead[0]][newHead[1]] = map[newHead[0]][newHead[1]] == ICE ? ICE_SNAKE : SNAKE
// Remove the end of the snake if he has not eaten anything
if(!increaseSize) {
const oldPos = snake.shift()
map[oldPos[0]][oldPos[1]] = EMPTY
map[oldPos[0]][oldPos[1]] = map[oldPos[0]][oldPos[1]] == ICE_SNAKE ? ICE : EMPTY
}
// Check if the user has won
@ -319,6 +327,14 @@ async function startGame(gameID) {
case FOOD:
ctx.drawImage(imgApple, x*cell_width, y*cell_height, cell_width, cell_height);
break;
case ICE:
case ICE_SNAKE:
ctx.fillStyle = "white";
ctx.fillRect(x*cell_width, y*cell_height, cell_width, cell_height)
if(map[y][x] == ICE)
break;
case SNAKE:
ctx.drawImage(imgSnakeBody, x*cell_width, y*cell_height, cell_width, cell_height);