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
Binary file added src/DataOfSquare.class
Binary file not shown.
Binary file added src/KeyboardListener.class
Binary file not shown.
4 changes: 4 additions & 0 deletions src/KeyboardListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public void keyPressed(KeyEvent e){
if(ThreadsController.directionSnake!=3)
ThreadsController.directionSnake=4;
break;

case KeyEvent.VK_SPACE: // -> space
Window.controller.toggleSpeed();
break;

default: break;
}
Expand Down
Binary file added src/Main.class
Binary file not shown.
Binary file added src/SquarePanel.class
Binary file not shown.
Binary file added src/ThreadsController.class
Binary file not shown.
11 changes: 10 additions & 1 deletion src/ThreadsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ public class ThreadsController extends Thread {
ArrayList<ArrayList<DataOfSquare>> Squares= new ArrayList<ArrayList<DataOfSquare>>();
Tuple headSnakePos;
int sizeSnake=3;
long speed = 50;
int speedLevel = 1; // 1:slow, 2:mid, 3:fast
long[] speedTable = {150, 80, 30};
long speed = 150;
public static int directionSnake ;

ArrayList<Tuple> positions = new ArrayList<Tuple>();
Expand Down Expand Up @@ -39,6 +41,13 @@ public void run() {
pauser();
}
}

public void toggleSpeed() {
speedLevel = speedLevel % 3 + 1;
speed = speedTable[speedLevel - 1];
Window.currentSpeedLevel = speedLevel;
Window.instance.repaint();
}

//delay between each move of the snake
private void pauser(){
Expand Down
Binary file added src/Tuple.class
Binary file not shown.
Binary file added src/Window.class
Binary file not shown.
43 changes: 36 additions & 7 deletions src/Window.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import java.awt.GridLayout;
import java.awt.*;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


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 static int currentSpeedLevel = 1;
public static ThreadsController controller;
public static Window instance;
private JLabel speedLabel;
public Window(){

instance = this;

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

this.setLayout(new BorderLayout());

speedLabel = new JLabel("Speed: " + currentSpeedLevel);
speedLabel.setFont(new Font("Arial", Font.BOLD, 18));
speedLabel.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 10, 5, 0));
this.add(speedLabel, BorderLayout.NORTH);

JPanel gamePanel = new JPanel();
gamePanel.setLayout(new GridLayout(20, 20, 0, 0));

// Setting up the layout of the panel
getContentPane().setLayout(new GridLayout(20,20,0,0));
// // 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);
gamePanel.add(Grid.get(i).get(j).square);
}
}

this.add(gamePanel, BorderLayout.CENTER);

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

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

}

public void updateSpeedLabel() {
speedLabel.setText("Speed: " + currentSpeedLevel);
}
// @Override
// public void paint(Graphics g) {
// super.paint(g);
// g.setColor(Color.BLACK);
// g.setFont(new Font("Arial", Font.BOLD, 18));
// g.drawString("Speed: " + currentSpeedLevel, 10, 25);
// }
}