From daf0db65fa2cb81e2de1c21c1d73628d3d0554d3 Mon Sep 17 00:00:00 2001 From: plane000 Date: Sun, 17 Jun 2018 08:25:27 +0100 Subject: [PATCH] Finished snake --- .../Serial_Output/Serial_Output.pde | 4 +- NodeJS/Snake.rar | Bin 0 -> 1892 bytes NodeJS/Snake/game.js | 141 ++++++++++++++---- NodeJS/Snake/index.html | 1 + NodeJS/Snake/package.json | 11 -- 5 files changed, 113 insertions(+), 44 deletions(-) create mode 100644 NodeJS/Snake.rar delete mode 100644 NodeJS/Snake/package.json diff --git a/C++/Arduino/Audio_Visualizer/Serial_Output/Serial_Output.pde b/C++/Arduino/Audio_Visualizer/Serial_Output/Serial_Output.pde index 97c5dbb..08adabf 100644 --- a/C++/Arduino/Audio_Visualizer/Serial_Output/Serial_Output.pde +++ b/C++/Arduino/Audio_Visualizer/Serial_Output/Serial_Output.pde @@ -17,8 +17,8 @@ void setup() { void draw() { float vol = analyzer.analyze(); - float normal = 10+vol*200; - int out = int(map(normal, 0, 60, 0, 11)); + int out = int(map(10+vol*200, 0, 100, 0, 11)); + println(out); port.write(Integer.toString(out)); port.write(" "); } \ No newline at end of file diff --git a/NodeJS/Snake.rar b/NodeJS/Snake.rar new file mode 100644 index 0000000000000000000000000000000000000000..2ab31fc9ac6b95542d143575ca95202c6a958e95 GIT binary patch literal 1892 zcmV-q2b=g(VR9iF2LS-{;ezW60R;yD1_1$pfPer!ekc5MBm`|2b>0l-ESIl zOJ#+YI7uxEQumpmU!w7e9+aLm-zIE8z5yV>!vi?NO?snm|B=U%O2v^p-nx`-IM{@{ zhOHNO6h5MUg)d72TH5viS8r@&QDp$K&S&F5=|D&8%p;@LGm~c1*w(?YcTO!fLXr7c_{(|3%3UZry3xAcUcvH0{{(EZeeR>FK1zGWiD!S3IhT; z20O;U1k?e|C$j@E1~W4=^j1~xz;6dyP%U!VWNuriqRo0Q4g!sLPJ<1wf%H7uk~t-A z2;8zwh4+;t_@vr=5?!U}z80hrB!2?j@zchMMEp<150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C0pN0mV3{B~(K*_%yx*;QA^dAVH1W zEP*JLO68K2H?W%{X|ZaMfd}3o!x~L-w1FGH&CJnqT(3TUjL)ABalhL25i!lU*}#~h zk6-u5e+}xFx+bcMM{-zeThjt{#Q{(dA}sFu3>kJi)T|k0@#)X5Y{w`E&G~jC z0s{*G1pp8L0001h000S7ZeeR>FJp6a3IhTuyJi2A1k?fa?bDVY0s{*G1pp8L0001h e000G3ZeeR>3IhVk8E5~K1k?c?cUDmY1q1-oqHt3H literal 0 HcmV?d00001 diff --git a/NodeJS/Snake/game.js b/NodeJS/Snake/game.js index d4bf390..3f743fb 100644 --- a/NodeJS/Snake/game.js +++ b/NodeJS/Snake/game.js @@ -1,24 +1,35 @@ +/* + Author(s): Ben (plane000)#8618 + Created: 14/06/2018 + Last Updated: 16/06/2018 + Last Updated by: Ben (plane000)#8618 +*/ + let c = document.getElementById("canv"); let ctx = c.getContext("2d"); -let screenHeight = 40; -let screenWidth = 40; +let lost = false; + +let gridSize = 40; let grid = []; //0 = nothing, 1 = snake, 2 = apple -let direction = 0; //0 = up, 1 = right, 2 = down, 3 = left +let direction = 3; //0 = up, 1 = right, 2 = down, 3 = left -let snakeX = 20; -let snakeY = 20; +let snake = [[Math.floor(Math.random() * gridSize), Math.floor(Math.random() * gridSize)]]; +let apple; -window.addEventListener("keydown", onKeyDown, false); - -for (let i = 0; i < screenHeight; i++) { +for (let i = 0; i < gridSize; i++) { grid[i] = []; - for (let j = 0; j < screenWidth; j++) { + for (let j = 0; j < gridSize; j++) { grid[i][j] = 0; } } +window.addEventListener("keydown", onKeyDown, false); +spawnApple(); +render(); +draw(); + function onKeyDown(event) { switch (event.key) { case 'w': @@ -40,31 +51,99 @@ function onKeyDown(event) { } } -function gameLoop() { - for (let i = 0; i < screenHeight; i++) { - for (let j = 0; j < screenWidth; j++) { - grid[i][j] = 0; +async function gameLoop() { + if (!lost) { + await updateSnake(); + await checkColisions(); + render(); + draw(); + } else { + for (let i = 0; i < gridSize; i++) { + for (let j = 0; j < gridSize; j++) { + grid[i][j] = 0; + } } + snake = [[Math.floor(Math.random() * gridSize), Math.floor(Math.random() * gridSize)]]; + spawnApple(); + render(); + draw(); + lost = false; } - - if (direction == 0) { - snakeY--; - } else if (direction == 1) { - snakeX++; - } else if (direction == 2) { - snakeY++; - } else if (direction == 3) { - snakeX--; - } - - grid[snakeX][snakeY] = 1; - - render(); } -function render() { - for (let i = 0; i < screenHeight; i++) { - for (let j = 0; j < screenWidth; j++) { +function updateSnake() { + let newX; + let newY; + if (direction == 0) { + newX = snake[snake.length - 1][0]; + newY = snake[snake.length - 1][1] - 1; + } else if (direction == 1) { + newX = snake[snake.length - 1][0] + 1; + newY = snake[snake.length - 1][1]; + } else if (direction == 2) { + newX = snake[snake.length - 1][0]; + newY = snake[snake.length - 1][1] + 1; + } else if (direction == 3) { + newX = snake[snake.length - 1][0] - 1; + newY = snake[snake.length - 1][1]; + } + + + try { + grid[snake[0][0]][snake[0][1]] = 0; + } catch (e) { + + } + snake.push([newX, newY]); + snake.shift(1); +} + +function checkColisions() { + if (apple[0] == snake[[snake.length - 1][0]][0] && apple[1] == snake[[snake.length - 1][0]][1] ) { + snake.push([apple[0], apple[1]]); + apple = []; + spawnApple(); + } + if (grid[snake[[snake.length - 1][0]][0]][snake[[snake.length - 1][0]][1]] == 1) { + lost = true; + } + if (snake[[snake.length - 1][0]][0] < 1) { + lost = true; + } + if (snake[[snake.length - 1][0]][0] > gridSize - 2) { + lost = true; + } + if (snake[[snake.length - 1][0]][1] < 1) { + lost = true; + } + if (snake[[snake.length - 1][0]][1] > gridSize - 1) { + lost = true; + } +} + +function spawnApple() { + let spawned = false + while (!spawned) { + let x = Math.floor(Math.random() * gridSize); + let y = Math.floor(Math.random() * gridSize); + if (grid[x][y] != 1 && x != 0 && y != 0) { + apple = [x, y]; + spawned = true; + break; + } + } +} + +function render() { + for (let s = 0; s < snake.length; s++) { + grid[snake[s][0]][snake[s][1]] = 1; + } + grid[apple[0]][apple[1]] = 2; +} + +function draw() { + for (let i = 0; i < gridSize; i++) { + for (let j = 0; j < gridSize; j++) { if (grid[i][j] == 0) { ctx.fillStyle="#FFFFF0"; } else if (grid[i][j] == 1) { @@ -72,7 +151,7 @@ function render() { } else { ctx.fillStyle="#00FF00"; } - ctx.fillRect(i * 10, j * 10, 400 / screenHeight, 400 / screenWidth); + ctx.fillRect(i * 10, j * 10, 400 / gridSize, 400 / gridSize); } } } diff --git a/NodeJS/Snake/index.html b/NodeJS/Snake/index.html index 721e027..754d4ad 100644 --- a/NodeJS/Snake/index.html +++ b/NodeJS/Snake/index.html @@ -8,6 +8,7 @@ + return diff --git a/NodeJS/Snake/package.json b/NodeJS/Snake/package.json deleted file mode 100644 index c94a11a..0000000 --- a/NodeJS/Snake/package.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "snake", - "version": "1.0.0", - "description": "", - "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "Ben (plane000)", - "license": "MIT" -}