Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added enums for direction #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Directions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public enum Directions {
RIGHT, LEFT, UP, DOWN
}
16 changes: 8 additions & 8 deletions src/KeyboardListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions src/ThreadsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Tuple> positions = new ArrayList<Tuple>();
Tuple foodPosition;
Expand All @@ -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());
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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;
Expand Down