diff --git a/src/DataOfSquare.class b/src/DataOfSquare.class new file mode 100644 index 0000000..cfc1813 Binary files /dev/null and b/src/DataOfSquare.class differ diff --git a/src/KeyboardListener.class b/src/KeyboardListener.class new file mode 100644 index 0000000..fcb0add Binary files /dev/null and b/src/KeyboardListener.class differ diff --git a/src/KeyboardListener.java b/src/KeyboardListener.java index 0271729..281d186 100644 --- a/src/KeyboardListener.java +++ b/src/KeyboardListener.java @@ -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; } diff --git a/src/Main.class b/src/Main.class new file mode 100644 index 0000000..fde3ed4 Binary files /dev/null and b/src/Main.class differ diff --git a/src/SquarePanel.class b/src/SquarePanel.class new file mode 100644 index 0000000..1424e06 Binary files /dev/null and b/src/SquarePanel.class differ diff --git a/src/ThreadsController.class b/src/ThreadsController.class new file mode 100644 index 0000000..89f8269 Binary files /dev/null and b/src/ThreadsController.class differ diff --git a/src/ThreadsController.java b/src/ThreadsController.java index 3df2d3d..4488a68 100644 --- a/src/ThreadsController.java +++ b/src/ThreadsController.java @@ -6,7 +6,9 @@ public class ThreadsController extends Thread { ArrayList> Squares= new ArrayList>(); 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 positions = new ArrayList(); @@ -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(){ diff --git a/src/Tuple.class b/src/Tuple.class new file mode 100644 index 0000000..017f5cd Binary files /dev/null and b/src/Tuple.class differ diff --git a/src/Window.class b/src/Window.class new file mode 100644 index 0000000..e922340 Binary files /dev/null and b/src/Window.class differ diff --git a/src/Window.java b/src/Window.java index 518fa9e..4e24323 100644 --- a/src/Window.java +++ b/src/Window.java @@ -1,8 +1,10 @@ -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{ @@ -10,8 +12,12 @@ class Window extends JFrame{ public static ArrayList> 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>(); @@ -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