Skip to content
Snippets Groups Projects
Commit b8d0f0d5 authored by Ahmed Loui Al Koasmh's avatar Ahmed Loui Al Koasmh
Browse files

Add new game transition and updated Game over autoreplay

parent f2299652
No related branches found
No related tags found
No related merge requests found
import java.awt.GridLayout;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Dimension;
class GameDisplay extends JFrame{
private static final long serialVersionUID = -2542001418764869760L;
public static ArrayList<ArrayList<SquareData>> grid;
public static int width = 20;
public static int height = 20;
public static JLabel scoreLabel;
public static JLabel timerLabel;
public static JLabel speedLabel;
public static JLabel modeLabel;
public GameDisplay(){
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JPanel game = new JPanel();
JPanel stats = new JPanel();
stats.setLayout(new BoxLayout(stats, BoxLayout.Y_AXIS));
// Creates the arraylist that'll contain the threads
grid = new ArrayList<ArrayList<SquareData>>();
ArrayList<SquareData> data;
// Creates Threads and its data and adds it to the arrayList
for(int i=0;i<width;i++){
data= new ArrayList<SquareData>();
for(int j=0;j<height;j++){
SquareData c = new SquareData(2);
data.add(c);
}
grid.add(data);
}
// Setting up the layout of the panel
game.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++){
game.add(grid.get(i).get(j).square);
}
}
scoreLabel = new JLabel("Score: " + 0);
// Dimension size = scoreLabel.getPreferredSize();
// scoreLabel.setBounds(25, 25, size.width, size.height); setbounds code doesn't do anything lol
timerLabel = new JLabel("Timer: " + 30 + " seconds left");
// timerLabel.setBounds(400, 25, size.width, size.height);
speedLabel = new JLabel("Speed: " + 5);
// timerLabel.setBounds(775, 25, size.width, size.height);
modeLabel = new JLabel("Modes: ");
stats.add(scoreLabel);
stats.add(timerLabel);
stats.add(speedLabel);
stats.add(modeLabel);
panel.add(stats);
panel.add(game);
getContentPane().add(panel);
// initial position of the snake
Tuple position = new Tuple(3,10);
// passing this value to the controller
ThreadsController c = new ThreadsController(position);
//Let's start the game now..
c.start();
// 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);
// ThreadsController c2 = new ThreadsController(position2);
// c2.start();
}
}
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import java.awt.Dimension;
class GameDisplay extends JPanel{
private static final long serialVersionUID = -2542001418764869760L;
public static ArrayList<ArrayList<SquareData>> grid;
public static int width = 20;
public static int height = 20;
public static JLabel scoreLabel;
public static JLabel timerLabel;
public static JLabel speedLabel;
public static JLabel modeLabel;
public static boolean gameOver = false;
public static boolean backClicked = false;
public GameDisplay(){
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
JPanel game = new JPanel();
JPanel stats = new JPanel();
stats.setLayout(new BoxLayout(stats, BoxLayout.Y_AXIS));
// Creates the arraylist that'll contain the threads
grid = new ArrayList<ArrayList<SquareData>>();
ArrayList<SquareData> data;
// Creates Threads and its data and adds it to the arrayList
for(int i=0;i<width;i++){
data= new ArrayList<SquareData>();
for(int j=0;j<height;j++){
SquareData c = new SquareData(2);
data.add(c);
}
grid.add(data);
}
// Setting up the layout of the panel
game.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++){
game.add(grid.get(i).get(j).square);
}
}
scoreLabel = new JLabel("Score: " + 0);
// Dimension size = scoreLabel.getPreferredSize();
// scoreLabel.setBounds(25, 25, size.width, size.height); setbounds code doesn't do anything lol
timerLabel = new JLabel("Timer: " + 30 + " seconds left");
// timerLabel.setBounds(400, 25, size.width, size.height);
speedLabel = new JLabel("Speed: " + 5);
// timerLabel.setBounds(775, 25, size.width, size.height);
modeLabel = new JLabel("Modes: ");
stats.add(scoreLabel);
stats.add(timerLabel);
stats.add(speedLabel);
stats.add(modeLabel);
this.add(stats);
this.add(game);
JButton newButton = new JButton("End Game");
newButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
backClicked = true;
}
});
JToolBar toolBar = new JToolBar();
toolBar.add(newButton);
this.add(toolBar);
toolBar.addSeparator(new Dimension(150, 0));
// initial position of the snake
Tuple position = new Tuple(3,10);
// passing this value to the controller
ThreadsController c = new ThreadsController(position);
//Let's start the game now..
c.start();
// 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);
// ThreadsController c2 = new ThreadsController(position2);
// c2.start();
}
}
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import java.awt.BorderLayout;
......@@ -14,6 +15,8 @@ import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import javax.swing.ImageIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
......@@ -48,21 +51,71 @@ public class MainMenuDisplay extends JFrame {
btnPlayNewGame.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
GameDisplay f1 = new GameDisplay();
f1.setSize(600,600);
int x = (int) ((dimension.getWidth() - f1.getWidth()) / 2);
int y = (int) ((dimension.getHeight() - f1.getHeight()) / 2);
f1.setLocation(x, y);
f1.setTitle("Snake Revamped");
f1.setVisible(true);
f1.requestFocusInWindow();
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Define a blank white JPanel
JPanel gamePane = new JPanel();
setContentPane(gamePane);
gamePane.setBackground(Color.WHITE);
// Add a new game to the content pane
GameDisplay newGame = new GameDisplay();
setLayout(new BorderLayout());
getContentPane().add(newGame);
// Request focus so that keyboard inputs work
newGame.requestFocusInWindow();
invalidate();
validate();
}
// Start a new thread to recognize when end game screen is needed
Thread endGameView = new Thread(){
public void run(){
while(!GameDisplay.gameOver) {
System.out.checkError(); // Do not delete this line
}
getContentPane().removeAll();
EndGameDisplay EndGame = new EndGameDisplay();
setContentPane(EndGame);
GameDisplay.gameOver = false;
GameDisplay.backClicked = false;
Thread transition = new Thread(){
public void run(){
while(!EndGameDisplay.goMenu && !EndGameDisplay.playAgain) {
System.out.checkError(); // Do not delete this line
}
getContentPane().removeAll();
if (EndGameDisplay.goMenu) {
EndGameDisplay.goMenu = false;
setContentPane(contentPane);
contentPane.setLayout(null);
}
else if (EndGameDisplay.playAgain) {
EndGameDisplay.playAgain = false;
setContentPane(contentPane);
MouseEvent click;
Point point;
long time;
point = new Point(arg0.getX(),arg0.getY());
SwingUtilities.convertPointToScreen(point, btnPlayNewGame);
time = System.currentTimeMillis();
click = new MouseEvent(btnPlayNewGame, MouseEvent.MOUSE_CLICKED, time, 0, arg0.getX(),arg0.getY(), point.x, point.y, 1, false, MouseEvent.BUTTON1);
btnPlayNewGame.dispatchEvent(click);
}
}
};
transition.start();
}
};
endGameView.start();
}
});
// btnPlayNewGame.setFont(new Font("Tahoma", Font.PLAIN, 27));
......@@ -145,7 +198,7 @@ public class MainMenuDisplay extends JFrame {
Thread settingWait = new Thread() {
public void run() {
while(!panel.settingsSaved) {
System.out.println("Wait for Settings");
System.out.checkError(); // Do not delete
}
panel.settingsSaved = false;
pack();
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment