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

Prevent the snake move opposite

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