Start to add ice
This commit is contained in:
parent
8ba5361469
commit
b84dff7f79
@ -16,6 +16,22 @@
|
|||||||
[20, 23],
|
[20, 23],
|
||||||
[20, 21]
|
[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,
|
"randFoodFreq": 2000,
|
||||||
"snake": [
|
"snake": [
|
||||||
[4,2], [4,1], [3,1], [2,1]
|
[4,2], [4,1], [3,1], [2,1]
|
||||||
|
20
script.js
20
script.js
@ -13,6 +13,8 @@ const EMPTY = i++;
|
|||||||
const SNAKE = i++;
|
const SNAKE = i++;
|
||||||
const WALL = i++;
|
const WALL = i++;
|
||||||
const FOOD = i++;
|
const FOOD = i++;
|
||||||
|
const ICE = i++;
|
||||||
|
const ICE_SNAKE = i++; // If the snake is on the ice
|
||||||
delete i;
|
delete i;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -189,6 +191,11 @@ async function startGame(gameID) {
|
|||||||
map[f[0]-1][f[1]-1] = FOOD
|
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) => {
|
level.snake.forEach((s) => {
|
||||||
map[s[0]-1][s[1]-1] = SNAKE
|
map[s[0]-1][s[1]-1] = SNAKE
|
||||||
snake.push([s[0]-1, s[1]-1]);
|
snake.push([s[0]-1, s[1]-1]);
|
||||||
@ -252,6 +259,7 @@ async function startGame(gameID) {
|
|||||||
score++;
|
score++;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case ICE_SNAKE:
|
||||||
case SNAKE:
|
case SNAKE:
|
||||||
case WALL:
|
case WALL:
|
||||||
gameOver();
|
gameOver();
|
||||||
@ -261,12 +269,12 @@ async function startGame(gameID) {
|
|||||||
|
|
||||||
// Push new snake position
|
// Push new snake position
|
||||||
snake.push(newHead);
|
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
|
// Remove the end of the snake if he has not eaten anything
|
||||||
if(!increaseSize) {
|
if(!increaseSize) {
|
||||||
const oldPos = snake.shift()
|
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
|
// Check if the user has won
|
||||||
@ -319,6 +327,14 @@ async function startGame(gameID) {
|
|||||||
case FOOD:
|
case FOOD:
|
||||||
ctx.drawImage(imgApple, x*cell_width, y*cell_height, cell_width, cell_height);
|
ctx.drawImage(imgApple, x*cell_width, y*cell_height, cell_width, cell_height);
|
||||||
break;
|
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:
|
case SNAKE:
|
||||||
ctx.drawImage(imgSnakeBody, x*cell_width, y*cell_height, cell_width, cell_height);
|
ctx.drawImage(imgSnakeBody, x*cell_width, y*cell_height, cell_width, cell_height);
|
||||||
|
Loading…
Reference in New Issue
Block a user