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

Restarts the game #17

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
24 changes: 20 additions & 4 deletions src/ThreadsController.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import java.util.ArrayList;

import javax.swing.JFrame;


//Controls all the game logic .. most important class in this project.
public class ThreadsController extends Thread {
ArrayList<ArrayList<DataOfSquare>> Squares= new ArrayList<ArrayList<DataOfSquare>>();
Tuple headSnakePos;
int sizeSnake=3;
int sizeSnake = 3;
long speed = 50;
public static int directionSnake ;

Expand Down Expand Up @@ -42,12 +44,23 @@ public void run() {

//delay between each move of the snake
private void pauser(){
try {
try {
sleep(speed);
} catch (InterruptedException e) {
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//Setting up restart after game pauses
private void restart() {
// Restarting the game
//Creating the window again
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);
}

//Checking if the snake bites itself or is eating
private void checkCollision() {
Expand All @@ -56,6 +69,7 @@ private void checkCollision() {
boolean biteItself = posCritique.getX()==positions.get(i).getX() && posCritique.getY()==positions.get(i).getY();
if(biteItself){
stopTheGame();

}
}

Expand All @@ -72,9 +86,11 @@ private void checkCollision() {
//Stops The Game
private void stopTheGame(){
System.out.println("COLISION! \n");
restart();
while(true){
pauser();
}
}

}

//Put food in a position and displays it
Expand Down
6 changes: 3 additions & 3 deletions src/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public Window(){

//To do : handle multiplayers .. The above works, test it and see what happens

//Tuple position2 = new Tuple(13,13);
//ControlleurThreads c2 = new ControlleurThreads(position2);
//c2.start();
// Tuple position2 = new Tuple(13,13);
// ControlleurThreads c2 = new ControlleurThreads(position2);
// c2.start();

}
}