From b7f1dbf1485e3cd4b568b0eceea9267e195252ea Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Sun, 29 Mar 2020 17:05:54 +0200 Subject: [PATCH] Make the ice "live" --- script.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/script.js b/script.js index 9f17325..f793aaf 100644 --- a/script.js +++ b/script.js @@ -333,6 +333,8 @@ async function startGame(gameID) { ctx.fillStyle = "white"; ctx.fillRect(x*cell_width, y*cell_height, cell_width, cell_height) + // If the snake is on the cell, we must render it too. + // That's why I put a condition to this break if(map[y][x] == ICE) break; @@ -401,8 +403,15 @@ async function startGame(gameID) { document.body.addEventListener("keydown", (ev) => { if(!canvas.isConnected) return; - if(["ArrowDown", "ArrowLeft", "ArrowRight", "ArrowUp"].includes(ev.key)) + if(["ArrowDown", "ArrowLeft", "ArrowRight", "ArrowUp"].includes(ev.key)) { + + // Check if the snake is on some ice + const headPos = snake[snake.length-1]; + if(map[headPos[0]][headPos[1]] == ICE_SNAKE) + return; + key = ev.key; + } if(ev.key == "p") { audioEl.pause()