Skip to content

Improved snake game #32

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

Open
wants to merge 4 commits 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
4 changes: 2 additions & 2 deletions src/DataOfSquare.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class DataOfSquare {
public DataOfSquare(int col){

//Lets add the color to the arrayList
C.add(Color.darkGray);//0
C.add(Color.BLUE); //1
C.add(Color.BLUE);//0
C.add(Color.RED); //1
C.add(Color.white); //2
color=col;
square = new SquarePanel(C.get(color));
Expand Down
22 changes: 17 additions & 5 deletions src/KeyboardListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,34 @@ 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!=2 && ThreadsController.ableToAcceptKeyPress){
ThreadsController.directionSnake=1;
ThreadsController.ableToAcceptKeyPress = false;
}

break;
case 38: // -> Top
if(ThreadsController.directionSnake!=4)
if(ThreadsController.directionSnake!=4 && ThreadsController.ableToAcceptKeyPress){
ThreadsController.directionSnake=3;
ThreadsController.ableToAcceptKeyPress = false;
}

break;

case 37: // -> Left
if(ThreadsController.directionSnake!=1)
if(ThreadsController.directionSnake!=1 && ThreadsController.ableToAcceptKeyPress){
ThreadsController.directionSnake=2;
ThreadsController.ableToAcceptKeyPress = false;
}

break;

case 40: // -> Bottom
if(ThreadsController.directionSnake!=3)
if(ThreadsController.directionSnake!=3 && ThreadsController.ableToAcceptKeyPress){
ThreadsController.directionSnake=4;
ThreadsController.ableToAcceptKeyPress = false;
}

break;

default: break;
Expand Down
38 changes: 33 additions & 5 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;

public class Main {

static final int WINDOWX = 700;
static final int WINDOWY = 700;
public static JFrame closeFrame = new JFrame("You Died!");
public static void main(String[] args) {



//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.setSize(WINDOWX,WINDOWY);
f1.setVisible(true);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


closeFrame.setSize(400, 400);

JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());

JLabel label = new JLabel("Oh No! You died. Your score was, " + ThreadsController.score);



panel.add(label);


closeFrame.add(panel);
closeFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
closeFrame.pack();

closeFrame.setLocationRelativeTo(null);
closeFrame.setVisible(false);
while(true){
label.setText("Oh No! You died. Your score was, " + ThreadsController.score);
}

}
}
83 changes: 57 additions & 26 deletions src/ThreadsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ public class ThreadsController extends Thread {
int sizeSnake=3;
long speed = 50;
public static int directionSnake ;

ArrayList<Tuple> positions = new ArrayList<Tuple>();
Tuple foodPosition;

public static int score;
public static boolean ableToAcceptKeyPress;
public static boolean gameEnd;
//Constructor of ControlleurThread
ThreadsController(Tuple positionDepart){
score = 0;
//Get all the threads
Squares=Window.Grid;
ableToAcceptKeyPress = true;
gameEnd = false;


headSnakePos=new Tuple(positionDepart.x,positionDepart.y);
directionSnake = 1;
Expand All @@ -24,22 +29,28 @@ public class ThreadsController extends Thread {
Tuple headPos = new Tuple(headSnakePos.getX(),headSnakePos.getY());
positions.add(headPos);

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

}

//Important part :
public void run() {
while(true){
moveInterne(directionSnake);
checkCollision();
moveExterne();
deleteTail();
pauser();
if(!gameEnd){
// System.out.println("Update");
ableToAcceptKeyPress = true;
moveInterne(directionSnake);
checkCollision();
moveExterne();
deleteTail();
pauser();
}


}
}

//delay between each move of the snake
private void pauser(){
try {
Expand All @@ -54,14 +65,20 @@ private void checkCollision() {
Tuple posCritique = positions.get(positions.size()-1);
for(int i = 0;i<=positions.size()-2;i++){
boolean biteItself = posCritique.getX()==positions.get(i).getX() && posCritique.getY()==positions.get(i).getY();
boolean touchWall = headSnakePos.getX() > Main.WINDOWX ||headSnakePos.getY() > Main.WINDOWY || headSnakePos.getX() < 0 || headSnakePos.getY() < 0;
if(biteItself){
stopTheGame();
}
if(touchWall){
stopTheGame();
}

}

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

Expand All @@ -71,10 +88,13 @@ private void checkCollision() {

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

}

//Put food in a position and displays it
Expand Down Expand Up @@ -104,30 +124,36 @@ private Tuple getValAleaNotInSnake(){
private void moveInterne(int dir){
switch(dir){
case 4:
headSnakePos.ChangeData(headSnakePos.x,(headSnakePos.y+1)%20);
headSnakePos.ChangeData(headSnakePos.x,(headSnakePos.y+1));


positions.add(new Tuple(headSnakePos.x,headSnakePos.y));
break;
case 3:
if(headSnakePos.y-1<0){
headSnakePos.ChangeData(headSnakePos.x,19);
}
else{
headSnakePos.ChangeData(headSnakePos.x,Math.abs(headSnakePos.y-1)%20);
}

//if(headSnakePos.y-1<0){
// headSnakePos.ChangeData(headSnakePos.x,19);
// }
//else{
headSnakePos.ChangeData(headSnakePos.x,Math.abs(headSnakePos.y-1)%20);
//}
positions.add(new Tuple(headSnakePos.x,headSnakePos.y));
break;
case 2:
if(headSnakePos.x-1<0){
headSnakePos.ChangeData(19,headSnakePos.y);
}
else{
headSnakePos.ChangeData(Math.abs(headSnakePos.x-1)%20,headSnakePos.y);
}
//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:
headSnakePos.ChangeData(Math.abs(headSnakePos.x+1)%20,headSnakePos.y);

headSnakePos.ChangeData(headSnakePos.x+1%20,headSnakePos.y);


positions.add(new Tuple(headSnakePos.x,headSnakePos.y));
break;
}
Expand All @@ -138,7 +164,12 @@ private void moveExterne(){
for(Tuple t : positions){
int y = t.getX();
int x = t.getY();
Squares.get(x).get(y).lightMeUp(0);
try{
Squares.get(x).get(y).lightMeUp(0);
} catch (IndexOutOfBoundsException e){
stopTheGame();
}


}
}
Expand Down
21 changes: 11 additions & 10 deletions src/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.*;


class Window extends JFrame{
private static final long serialVersionUID = -2542001418764869760L;
public static ArrayList<ArrayList<DataOfSquare>> Grid;
public static int width = 20;
public static int height = 20;

public Window(){


// Creates the arraylist that'll contain the threads
Grid = new ArrayList<ArrayList<DataOfSquare>>();
ArrayList<DataOfSquare> data;

// Creates Threads and its data and adds it to the arrayList
for(int i=0;i<width;i++){
data= new ArrayList<DataOfSquare>();
Expand All @@ -26,19 +25,19 @@ public Window(){
}
Grid.add(data);
}

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

// Start & pauses all threads, then adds every square of each thread to the panel
for(int i=0;i<width;i++){
for(int j=0;j<height;j++){
getContentPane().add(Grid.get(i).get(j).square);
}
}

// initial position of the snake
Tuple position = new Tuple(10,10);
Tuple position = new Tuple(5,5);
// passing this value to the controller
ThreadsController c = new ThreadsController(position);
//Let's start the game now..
Expand All @@ -47,11 +46,13 @@ public Window(){
// Links the window to the keyboardlistenner.
this.addKeyListener((KeyListener) new KeyboardListener());




//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();

}
}