Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
Snake with the edition of Menu's
This game includes a menu that add the following features:
-Allows the modification of initial snake size
-Allows the modification of snake speed
-Allows the edition of walls
-Each of the food blocks created are a random color
-Allows the ability to replay the game if the player loses
  • Loading branch information
sdhjkjk committed Nov 24, 2015
commit a238617e4a15eb2402d7376cdf1f71512e4290a8
Binary file removed BIN -14.8 KB SnakeGame.jar
Binary file not shown.
10 changes: 9 additions & 1 deletion 10 src/DataOfSquare.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package snakegame;

import java.util.ArrayList;
import java.awt.Color;

Expand All @@ -13,7 +15,13 @@ public DataOfSquare(int col){
//Lets add the color to the arrayList
C.add(Color.darkGray);//0
C.add(Color.BLUE); //1
C.add(Color.white); //2
C.add(Color.white); //2
C.add(Color.green);
C.add(Color.pink);
C.add(Color.red);
C.add(Color.orange);
C.add(Color.magenta);
C.add(Color.BLUE);
color=col;
square = new SquarePanel(C.get(color));
}
Expand Down
2 changes: 2 additions & 0 deletions 2 src/KeyboardListener.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package snakegame;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

Expand Down
25 changes: 16 additions & 9 deletions 25 src/Main.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import javax.swing.JFrame;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package snakegame;

/**
*
* @author Tom
*/
public class Main {

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.setVisible(true);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

mainMenu Complete = new mainMenu();
Complete.setSize(325,265);
Complete.setVisible(true);


}
}
}
2 changes: 2 additions & 0 deletions 2 src/SquarePanel.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package snakegame;

import java.awt.Color;
import javax.swing.JPanel;

Expand Down
99 changes: 70 additions & 29 deletions 99 src/ThreadsController.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import java.util.ArrayList;
package snakegame;

import java.util.ArrayList;
import javax.swing.JOptionPane;

//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;
long speed = 50;


int sizeSnakeFromMenu=settingsMenu.snakeSizeI; //These are all the new obtained value from the settings menu
int speedFromMenu = settingsMenu.snakeSpeedI;
int growthFromSetting = settingsMenu.snakeGrowthI;
public static int directionSnake ;
public static boolean closeGameWindow = false;

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


//Constructor of ControlleurThread
ThreadsController(Tuple positionDepart){
Expand All @@ -24,8 +31,10 @@ 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= new Tuple(settingsMenu.blockHeightI-3,settingsMenu.blockWidthI-3);

spawnFood(foodPosition);


}

Expand All @@ -43,12 +52,22 @@ public void run() {
//delay between each move of the snake
private void pauser(){
try {
sleep(speed);
sleep(speedFromMenu);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

private int RandoColor(){
Double randoColor = Math.random() * 10;
Integer randoColor2 = randoColor.intValue();
if (randoColor2 >= 3 && randoColor2 <=8){
return randoColor2;
}
else {
return 5;
}

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

if (settingsMenu.wallsOn){ //checks if the wall box is checked
boolean wallCollision = posCritique.getX()==0 || posCritique.getY()==0 // checks if the snake is 1. on the left side 2. on the bottom
|| posCritique.getX()==settingsMenu.blockHeightI-1 || posCritique.getY()== settingsMenu.blockWidthI-1;//3. on the right 4. on the top
if (wallCollision){ //if any of these conditions are met, stop the game
stopTheGame();
}
}

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

spawnFood(foodPosition);
spawnFood(foodPosition);

}
}

//Stops The Game
mainMenu Again;
//Stops The Game and gives user another chance
private void stopTheGame(){
System.out.println("COLISION! \n");
while(true){
pauser();
}
}

int tryAgain = JOptionPane.showConfirmDialog(null, "You died! Would you like to play again?");
//this shows a dialog box and gives the user the option to press "Yes" "No" or "Cancel"

if (tryAgain == 0){
//If the user presses yes
Again = new mainMenu();
Again.setSize(325, 265);
Again.setVisible(true);
//Make a new mainMenu appear
this.closeGameWindow = true;
}

while(true){
pauser();
}
}

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

Squares.get(foodPositionIn.x).get(foodPositionIn.y).lightMeUp(RandoColor());

}

//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= 1 + (int)(Math.random()*17);
int ranY= 1 + (int)(Math.random()*17);
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= 1 + (int)(Math.random()*17);
ranY= 1 + (int)(Math.random()*17);
p=new Tuple(ranX,ranY);
i=0;
}
Expand All @@ -104,30 +145,30 @@ 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)%settingsMenu.blockHeightI);
positions.add(new Tuple(headSnakePos.x,headSnakePos.y));
break;
case 3:
if(headSnakePos.y-1<0){
headSnakePos.ChangeData(headSnakePos.x,19);
headSnakePos.ChangeData(headSnakePos.x,settingsMenu.blockHeightI-1);
}
else{
headSnakePos.ChangeData(headSnakePos.x,Math.abs(headSnakePos.y-1)%20);
headSnakePos.ChangeData(headSnakePos.x,Math.abs(headSnakePos.y-1)%settingsMenu.blockHeightI);
}
positions.add(new Tuple(headSnakePos.x,headSnakePos.y));
break;
case 2:
if(headSnakePos.x-1<0){
headSnakePos.ChangeData(19,headSnakePos.y);
headSnakePos.ChangeData(settingsMenu.blockWidthI-1,headSnakePos.y);
}
else{
headSnakePos.ChangeData(Math.abs(headSnakePos.x-1)%20,headSnakePos.y);
headSnakePos.ChangeData(Math.abs(headSnakePos.x-1)%settingsMenu.blockWidthI,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(Math.abs(headSnakePos.x+1)%settingsMenu.blockWidthI,headSnakePos.y);
positions.add(new Tuple(headSnakePos.x,headSnakePos.y));
break;
}
Expand All @@ -146,7 +187,7 @@ private void moveExterne(){
//Refreshes the tail of the snake, by removing the superfluous data in positions arraylist
//and refreshing the display of the things that is removed
private void deleteTail(){
int cmpt = sizeSnake;
int cmpt = sizeSnakeFromMenu;
for(int i = positions.size()-1;i>=0;i--){
if(cmpt==0){
Tuple t = positions.get(i);
Expand All @@ -156,7 +197,7 @@ private void deleteTail(){
cmpt--;
}
}
cmpt = sizeSnake;
cmpt = sizeSnakeFromMenu;
for(int i = positions.size()-1;i>=0;i--){
if(cmpt==0){
positions.remove(i);
Expand Down
2 changes: 2 additions & 0 deletions 2 src/Tuple.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package snakegame;

public class Tuple {
public int x;
public int y;
Expand Down
22 changes: 15 additions & 7 deletions 22 src/Window.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package snakegame;

import java.awt.GridLayout;
import java.awt.event.KeyListener;
import java.util.ArrayList;
Expand All @@ -8,8 +10,8 @@
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;
int width=settingsMenu.blockWidthI;
int height = settingsMenu.blockHeightI;
public Window(){


Expand All @@ -21,14 +23,20 @@ public Window(){
for(int i=0;i<width;i++){
data= new ArrayList<DataOfSquare>();
for(int j=0;j<height;j++){
DataOfSquare c = new DataOfSquare(2);
data.add(c);
if (settingsMenu.wallsOn && (i == 0 || i == width -1 || j ==0 || j == height -1)){ //this line selected the location of the walls
DataOfSquare c = new DataOfSquare(0); //this changed the color of those areas to black
data.add(c);// set input those colors into "data", an array containing the color of all squares.
}
else {
DataOfSquare c = new DataOfSquare(2);
data.add(c);
}
}
Grid.add(data);
Grid.add(data);
}

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

// Start & pauses all threads, then adds every square of each thread to the panel
for(int i=0;i<width;i++){
Expand All @@ -43,7 +51,7 @@ public Window(){
ThreadsController c = new ThreadsController(position);
//Let's start the game now..
c.start();

// Links the window to the keyboardlistenner.
this.addKeyListener((KeyListener) new KeyboardListener());

Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.