From dba65139379e811a491a32b2f79374e0e7b20f7f Mon Sep 17 00:00:00 2001 From: NNelson2 Date: Sun, 30 Apr 2023 16:56:55 -0500 Subject: [PATCH] added enums for direction --- src/Directions.java | 3 +++ src/KeyboardListener.java | 16 ++++++++-------- src/ThreadsController.java | 14 +++++++------- 3 files changed, 18 insertions(+), 15 deletions(-) create mode 100644 src/Directions.java diff --git a/src/Directions.java b/src/Directions.java new file mode 100644 index 0000000..ab1a266 --- /dev/null +++ b/src/Directions.java @@ -0,0 +1,3 @@ +public enum Directions { + RIGHT, LEFT, UP, DOWN +} diff --git a/src/KeyboardListener.java b/src/KeyboardListener.java index 0271729..c41ac83 100644 --- a/src/KeyboardListener.java +++ b/src/KeyboardListener.java @@ -7,22 +7,22 @@ public void keyPressed(KeyEvent e){ switch(e.getKeyCode()){ case 39: // -> Right //if it's not the opposite direction - if(ThreadsController.directionSnake!=2) - ThreadsController.directionSnake=1; + if(ThreadsController.directionSnake!=Directions.LEFT) + ThreadsController.directionSnake= Directions.RIGHT; break; case 38: // -> Top - if(ThreadsController.directionSnake!=4) - ThreadsController.directionSnake=3; + if(ThreadsController.directionSnake!= Directions.UP) + ThreadsController.directionSnake= Directions.DOWN; break; case 37: // -> Left - if(ThreadsController.directionSnake!=1) - ThreadsController.directionSnake=2; + if(ThreadsController.directionSnake!= Directions.RIGHT) + ThreadsController.directionSnake= Directions.LEFT; break; case 40: // -> Bottom - if(ThreadsController.directionSnake!=3) - ThreadsController.directionSnake=4; + if(ThreadsController.directionSnake!= Directions.DOWN) + ThreadsController.directionSnake= Directions.UP; break; default: break; diff --git a/src/ThreadsController.java b/src/ThreadsController.java index 3df2d3d..df06099 100644 --- a/src/ThreadsController.java +++ b/src/ThreadsController.java @@ -7,7 +7,7 @@ public class ThreadsController extends Thread { Tuple headSnakePos; int sizeSnake=3; long speed = 50; - public static int directionSnake ; + public static Directions directionSnake; ArrayList positions = new ArrayList(); Tuple foodPosition; @@ -18,7 +18,7 @@ public class ThreadsController extends Thread { Squares=Window.Grid; headSnakePos=new Tuple(positionDepart.x,positionDepart.y); - directionSnake = 1; + directionSnake = Directions.RIGHT; //!!! Pointer !!!! Tuple headPos = new Tuple(headSnakePos.getX(),headSnakePos.getY()); @@ -101,13 +101,13 @@ private Tuple getValAleaNotInSnake(){ //Moves the head of the snake and refreshes the positions in the arraylist //1:right 2:left 3:top 4:bottom 0:nothing - private void moveInterne(int dir){ + private void moveInterne(Directions dir){ switch(dir){ - case 4: + case UP: headSnakePos.ChangeData(headSnakePos.x,(headSnakePos.y+1)%20); positions.add(new Tuple(headSnakePos.x,headSnakePos.y)); break; - case 3: + case DOWN: if(headSnakePos.y-1<0){ headSnakePos.ChangeData(headSnakePos.x,19); } @@ -116,7 +116,7 @@ private void moveInterne(int dir){ } positions.add(new Tuple(headSnakePos.x,headSnakePos.y)); break; - case 2: + case LEFT: if(headSnakePos.x-1<0){ headSnakePos.ChangeData(19,headSnakePos.y); } @@ -126,7 +126,7 @@ private void moveInterne(int dir){ positions.add(new Tuple(headSnakePos.x,headSnakePos.y)); break; - case 1: + case RIGHT: headSnakePos.ChangeData(Math.abs(headSnakePos.x+1)%20,headSnakePos.y); positions.add(new Tuple(headSnakePos.x,headSnakePos.y)); break;