Skip to content
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
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.snakeDirection!=SnakeDirections.LEFT)
ThreadsController.snakeDirection=SnakeDirections.RIGHT;
break;
case 38: // -> Top
if(ThreadsController.directionSnake!=4)
ThreadsController.directionSnake=3;
if(ThreadsController.snakeDirection!=SnakeDirections.DOWN)
ThreadsController.snakeDirection=SnakeDirections.UP;
break;

case 37: // -> Left
if(ThreadsController.directionSnake!=1)
ThreadsController.directionSnake=2;
if(ThreadsController.snakeDirection!=SnakeDirections.RIGHT)
ThreadsController.snakeDirection=SnakeDirections.LEFT;
break;

case 40: // -> Bottom
if(ThreadsController.directionSnake!=3)
ThreadsController.directionSnake=4;
if(ThreadsController.snakeDirection!=SnakeDirections.UP)
ThreadsController.snakeDirection=SnakeDirections.DOWN;
break;

default: break;
Expand Down
61 changes: 52 additions & 9 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,60 @@
import javax.swing.JFrame;
import javax.swing.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Main {

private static int attempt = 1;
private static Window currentWindow;

public static void main(String[] args) {
startGame();
}

private static void startGame() {
// game restart loop
do {
if (attempt > 1) {
clearBoard();
}
//Creating the window with all its awesome snaky features
currentWindow = new Window();

//Setting up the window settings
currentWindow.setTitle("Snake attempt " + attempt++);
currentWindow.setSize(300,300);
currentWindow.setLocationRelativeTo(null);
currentWindow.setVisible(true);
currentWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Wait until the current game is finished
currentWindow.waitForGameEnd();

} while (restartRequested());
clearBoard();
}

private static void clearBoard() {
currentWindow.dispose();
}

private static boolean restartRequested() {

//Creating the window with all its awesome snaky features
Window f1= new Window();

//Setting up the window settings
f1.setTitle("Snake");
f1.setSize(300,300);
f1.setVisible(true);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FutureTask<Integer> restartQuestion = new FutureTask<Integer>(
()->JOptionPane.showConfirmDialog(
null,
"Play again?",
"You played yourself",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE));

restartQuestion.run();
try {
int response = restartQuestion.get();
return response == JOptionPane.YES_OPTION;
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
return false;
}
}
}
3 changes: 3 additions & 0 deletions src/SnakeDirections.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public enum SnakeDirections {
UP, DOWN, LEFT, RIGHT
}
61 changes: 30 additions & 31 deletions src/ThreadsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,40 @@ public class ThreadsController extends Thread {
Tuple headSnakePos;
int sizeSnake=3;
long speed = 50;
public static int directionSnake ;
public static SnakeDirections snakeDirection;
private boolean gameOver = false;

ArrayList<Tuple> positions = new ArrayList<Tuple>();
Tuple foodPosition;
//Constructor of ControlleurThread

//Constructor of ControlleurThread
ThreadsController(Tuple positionDepart){
//Get all the threads
Squares=Window.Grid;

headSnakePos=new Tuple(positionDepart.x,positionDepart.y);
directionSnake = 1;
snakeDirection = SnakeDirections.RIGHT;

//!!! Pointer !!!!
Tuple headPos = new Tuple(headSnakePos.getX(),headSnakePos.getY());
positions.add(headPos);

foodPosition= new Tuple(Window.height-1,Window.width-1);
spawnFood(foodPosition);

}

//Important part :
public void run() {
while(true){
moveInterne(directionSnake);
while(!gameOver){
moveInterne(snakeDirection);
checkCollision();
moveExterne();
deleteTail();
pauser();
}
}

//delay between each move of the snake
private void pauser(){
try {
Expand All @@ -48,7 +49,7 @@ private void pauser(){
e.printStackTrace();
}
}

//Checking if the snake bites itself or is eating
private void checkCollision() {
Tuple posCritique = positions.get(positions.size()-1);
Expand All @@ -58,56 +59,54 @@ private void checkCollision() {
stopTheGame();
}
}

boolean eatingFood = posCritique.getX()==foodPosition.y && posCritique.getY()==foodPosition.x;
if(eatingFood){
System.out.println("Yummy!");
sizeSnake=sizeSnake+1;
foodPosition = getValAleaNotInSnake();

spawnFood(foodPosition);
spawnFood(foodPosition);
}
}

//Stops The Game
private void stopTheGame(){
System.out.println("COLISION! \n");
while(true){
pauser();
}
gameOver = true;
}

//Put food in a position and displays it
private void spawnFood(Tuple foodPositionIn){
Squares.get(foodPositionIn.x).get(foodPositionIn.y).lightMeUp(1);
}

//return a position not occupied by the snake
private Tuple getValAleaNotInSnake(){
Tuple p ;
int ranX= 0 + (int)(Math.random()*19);
int ranY= 0 + (int)(Math.random()*19);
int ranX= 0 + (int)(Math.random()*19);
int ranY= 0 + (int)(Math.random()*19);
p=new Tuple(ranX,ranY);
for(int i = 0;i<=positions.size()-1;i++){
if(p.getY()==positions.get(i).getX() && p.getX()==positions.get(i).getY()){
ranX= 0 + (int)(Math.random()*19);
ranY= 0 + (int)(Math.random()*19);
ranX= 0 + (int)(Math.random()*19);
ranY= 0 + (int)(Math.random()*19);
p=new Tuple(ranX,ranY);
i=0;
}
}
return p;
}

//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){
switch(dir){
case 4:
private void moveInterne(SnakeDirections direction){
switch(direction){
case DOWN:
headSnakePos.ChangeData(headSnakePos.x,(headSnakePos.y+1)%20);
positions.add(new Tuple(headSnakePos.x,headSnakePos.y));
break;
case 3:
case UP:
if(headSnakePos.y-1<0){
headSnakePos.ChangeData(headSnakePos.x,19);
}
Expand All @@ -116,17 +115,17 @@ 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);
}
else{
headSnakePos.ChangeData(Math.abs(headSnakePos.x-1)%20,headSnakePos.y);
}
}
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
19 changes: 14 additions & 5 deletions src/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ class Window extends JFrame{
public static ArrayList<ArrayList<DataOfSquare>> Grid;
public static int width = 20;
public static int height = 20;
private ThreadsController controllerThread;
public Window(){


// Creates the arraylist that'll contain the threads
Grid = new ArrayList<ArrayList<DataOfSquare>>();
ArrayList<DataOfSquare> data;
Expand All @@ -26,7 +27,7 @@ public Window(){
}
Grid.add(data);
}

// Setting up the layout of the panel
getContentPane().setLayout(new GridLayout(20,20,0,0));

Expand All @@ -36,13 +37,13 @@ public Window(){
getContentPane().add(Grid.get(i).get(j).square);
}
}

// initial position of the snake
Tuple position = new Tuple(10,10);
// passing this value to the controller
ThreadsController c = new ThreadsController(position);
controllerThread = new ThreadsController(position);
//Let's start the game now..
c.start();
controllerThread.start();

// Links the window to the keyboardlistenner.
this.addKeyListener((KeyListener) new KeyboardListener());
Expand All @@ -54,4 +55,12 @@ public Window(){
//c2.start();

}

public void waitForGameEnd() {
try {
controllerThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}