Skip to content
Snippets Groups Projects
Commit 7bc0d8ae authored by ys678's avatar ys678
Browse files

Prevent the snake move opposite

parent 23860e8b
No related branches found
No related tags found
1 merge request!16Potential restart
...@@ -144,22 +144,28 @@ class WebSocketModel { ...@@ -144,22 +144,28 @@ class WebSocketModel {
for (var j = 0; j < gameBoard[0].length; j++) { for (var j = 0; j < gameBoard[0].length; j++) {
if (gameBoard[i][j].type === 1 && gameBoard[i][j].pid === pid) { if (gameBoard[i][j].type === 1 && gameBoard[i][j].pid === pid) {
console.log("Updating direction to: " + movement); console.log("Updating direction to: " + movement);
let currentDirection = gameBoard[i][j].direction;
let newDirection = currentDirection;
switch (movement) { switch (movement) {
case "up": case "up":
gameBoard[i][j].direction = 0; if (currentDirection !== 2) newDirection = 0; // Cannot move up if going down
break; break;
case "right": case "right":
gameBoard[i][j].direction = 1; if (currentDirection !== 3) newDirection = 1; // Cannot move right if going left
cell.direction = 1;
break; break;
case "down": case "down":
gameBoard[i][j].direction = 2; if (currentDirection !== 0) newDirection = 2; // Cannot move down if going up
break; break;
case "left": case "left":
gameBoard[i][j].direction = 3; if (currentDirection !== 1) newDirection = 3; // Cannot move left if going right
break; break;
} }
console.log("New direction set to: " + gameBoard[i][j].direction); if (newDirection !== currentDirection) {
console.log("Updating direction to: " + movement);
gameBoard[i][j].direction = newDirection;
}
return; return;
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment