From b84dff7f79b433d0453d96cd6e6064be57c22b6c Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Sun, 29 Mar 2020 16:59:56 +0200 Subject: [PATCH] Start to add ice --- levels/2.json | 16 ++++++++++++++++ script.js | 20 ++++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/levels/2.json b/levels/2.json index d3d9089..c8b485d 100644 --- a/levels/2.json +++ b/levels/2.json @@ -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] diff --git a/script.js b/script.js index 3514d52..9f17325 100644 --- a/script.js +++ b/script.js @@ -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);