Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
3
3XA3 Lab 3 Group 7
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mike Li
3XA3 Lab 3 Group 7
Commits
b8d0f0d5
Commit
b8d0f0d5
authored
3 years ago
by
Ahmed Loui Al Koasmh
Browse files
Options
Downloads
Patches
Plain Diff
Add new game transition and updated Game over autoreplay
parent
f2299652
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/GameDisplay.java
+106
-89
106 additions, 89 deletions
src/GameDisplay.java
src/MainMenuDisplay.java
+67
-14
67 additions, 14 deletions
src/MainMenuDisplay.java
src/ThreadsController.java
+562
-544
562 additions, 544 deletions
src/ThreadsController.java
with
735 additions
and
647 deletions
src/GameDisplay.java
+
106
−
89
View file @
b8d0f0d5
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();
}
}
This diff is collapsed.
Click to expand it.
src/MainMenuDisplay.java
+
67
−
14
View file @
b8d0f0d5
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.
Click to expand it.
src/ThreadsController.java
+
562
−
544
View file @
b8d0f0d5
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment