Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
J
JTankTrouble
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
9831111
JTankTrouble
Commits
83fc6ec7
Commit
83fc6ec7
authored
Aug 03, 2020
by
nargessalehi98
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add constructor.
parent
7ef46f02
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
78 additions
and
62 deletions
+78
-62
GameFrame.java
src/GameFrame.java
+78
-62
No files found.
src/GameFrame.java
View file @
83fc6ec7
/*** In The Name of Allah ***/
/*** In The Name of Allah ***/
import
javax.imageio.ImageIO
;
import
javax.sound.midi.Soundbank
;
import
javax.sound.sampled.*
;
import
javax.swing.*
;
import
java.applet.AudioClip
;
import
java.awt.*
;
import
java.awt.*
;
import
java.awt.event.MouseAdapter
;
import
java.awt.event.MouseEvent
;
import
java.awt.event.MouseListener
;
import
java.awt.geom.AffineTransform
;
import
java.awt.geom.AffineTransform
;
import
java.awt.image.BufferStrategy
;
import
java.awt.image.BufferStrategy
;
import
java.awt.image.BufferedImage
;
import
java.awt.image.BufferedImage
;
import
java.io.*
;
import
java.io.File
;
import
java.lang.reflect.Array
;
import
java.io.FileReader
;
import
java.util.*
;
import
java.io.IOException
;
import
java.util.List
;
import
java.util.ArrayList
;
import
java.util.Scanner
;
import
javax.imageio.ImageIO
;
import
javax.swing.*
;
/**
/**
* The window on which the rendering is performed.
* The window on which the rendering is performed.
...
@@ -34,6 +29,7 @@ public class GameFrame extends JFrame {
...
@@ -34,6 +29,7 @@ public class GameFrame extends JFrame {
public
static
final
int
GAME_WIDTH
=
16
*
720
/
9
;
// wide aspect ratio
public
static
final
int
GAME_WIDTH
=
16
*
720
/
9
;
// wide aspect ratio
//uncomment all /*...*/ in the class for using Tank icon instead of a simple circle
//uncomment all /*...*/ in the class for using Tank icon instead of a simple circle
private
BufferedImage
image
;
private
BufferedImage
image1
;
private
BufferedImage
image1
;
private
BufferedImage
image2
;
private
BufferedImage
image2
;
private
BufferedImage
image3
;
private
BufferedImage
image3
;
...
@@ -57,42 +53,51 @@ public class GameFrame extends JFrame {
...
@@ -57,42 +53,51 @@ public class GameFrame extends JFrame {
int
lastX
=
120
;
int
lastX
=
120
;
int
lastY
=
120
;
int
lastY
=
120
;
boolean
firstPrize
=
true
;
boolean
firstPrize
=
true
;
boolean
getPrize
=
false
;
boolean
getPrize
=
false
;
public
BufferedImage
lastPrize
;
public
BufferedImage
lastPrize
;
private
Graphics2D
g2d
;
private
BufferStrategy
bufferStrategy
;
private
BufferStrategy
bufferStrategy
;
private
ArrayList
<
Wall
>
walls
;
private
Graphics2D
g2d
;
private
ArrayList
<
Prize
>
prizes
;
private
ArrayList
<
Prize
>
prizes
;
private
ArrayList
<
Wall
>
walls
;
private
ArrayList
<
Tank
>
tanks
;
private
ArrayList
<
Tank
>
tanks
;
private
boolean
allWallsAdded
=
false
;
public
GameFrame
(
String
title
)
throws
AWTException
,
IOException
{
public
GameFrame
(
String
title
)
{
super
(
title
);
super
(
title
);
setBackground
(
Color
.
white
);
setResizable
(
false
);
setResizable
(
false
);
setBackground
(
Color
.
white
);
setSize
(
GAME_WIDTH
,
GAME_HEIGHT
);
setSize
(
GAME_WIDTH
,
GAME_HEIGHT
);
setLayout
(
new
BorderLayout
());
setLayout
(
new
BorderLayout
());
try
{
image
=
ImageIO
.
read
(
new
File
(
"tank_dark.png"
));
}
catch
(
IOException
e
)
{
System
.
out
.
println
(
e
);
}
walls
=
new
ArrayList
<>();
walls
=
new
ArrayList
<>();
tanks
=
new
ArrayList
<>();
prizeLoc
=
new
ArrayList
<>();
prizes
=
new
ArrayList
<>();
tanks
.
add
(
new
Tank
(
"tank_dark.png"
,
30
,
70
,
0
));
Controller
.
walls
=
walls
;
Controller
.
walls
=
walls
;
Controller
.
prizes
=
prizes
;
Controller
.
getPrize
=
getPrize
;
Controller
.
tanks
=
tanks
;
Controller
.
g2d
=
g2d
;
Controller
.
g2d
=
g2d
;
Controller
.
taken
=
taken
;
Controller
.
taken
=
taken
;
prizeLoc
=
new
ArrayList
<>();
prizes
=
new
ArrayList
<>();
Controller
.
prizes
=
prizes
;
Controller
.
getPrize
=
getPrize
;
tanks
=
new
ArrayList
<>();
Controller
.
tanks
=
tanks
;
try
{
try
{
image1
=
ImageIO
.
read
(
new
File
(
"
T
ank_dark.png"
));
image1
=
ImageIO
.
read
(
new
File
(
"
t
ank_dark.png"
));
image2
=
ImageIO
.
read
(
new
File
(
"
T
ank_blue.png"
));
image2
=
ImageIO
.
read
(
new
File
(
"
t
ank_blue.png"
));
image3
=
ImageIO
.
read
(
new
File
(
"
T
ank_red.png"
));
image3
=
ImageIO
.
read
(
new
File
(
"
t
ank_red.png"
));
image4
=
ImageIO
.
read
(
new
File
(
"
T
ank_green.png"
));
image4
=
ImageIO
.
read
(
new
File
(
"
t
ank_green.png"
));
image5
=
ImageIO
.
read
(
new
File
(
"
T
ank_sand.png"
));
image5
=
ImageIO
.
read
(
new
File
(
"
t
ank_sand.png"
));
background
=
ImageIO
.
read
(
new
File
(
"background.png"
));
//
background = ImageIO.read(new File("background.png"));
bullet2
=
ImageIO
.
read
(
new
File
(
"2B.png"
));
bullet2
=
ImageIO
.
read
(
new
File
(
"2B.png"
));
bullet3
=
ImageIO
.
read
(
new
File
(
"3B.png"
));
bullet3
=
ImageIO
.
read
(
new
File
(
"3B.png"
));
life
=
ImageIO
.
read
(
new
File
(
"life.png"
));
life
=
ImageIO
.
read
(
new
File
(
"life.png"
));
...
@@ -102,6 +107,8 @@ public class GameFrame extends JFrame {
...
@@ -102,6 +107,8 @@ public class GameFrame extends JFrame {
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
System
.
out
.
println
(
e
);
System
.
out
.
println
(
e
);
}
}
}
}
/**
/**
...
@@ -119,7 +126,7 @@ public class GameFrame extends JFrame {
...
@@ -119,7 +126,7 @@ public class GameFrame extends JFrame {
/**
/**
* Game rendering with triple-buffering using BufferStrategy.
* Game rendering with triple-buffering using BufferStrategy.
*/
*/
public
void
render
(
GameState
state
)
throws
IOException
{
public
void
render
(
GameState
state
)
{
// Render single frame
// Render single frame
do
{
do
{
// The following loop ensures that the contents of the drawing buffer
// The following loop ensures that the contents of the drawing buffer
...
@@ -148,9 +155,8 @@ public class GameFrame extends JFrame {
...
@@ -148,9 +155,8 @@ public class GameFrame extends JFrame {
}
while
(
bufferStrategy
.
contentsLost
());
}
while
(
bufferStrategy
.
contentsLost
());
}
}
public
BufferedImage
rotate
(
BufferedImage
image
,
Double
degrees
)
{
public
BufferedImage
rotate
(
BufferedImage
image
,
Double
degrees
)
{
// Calculate the new size of the image based on the angle of rota
t
ion
// Calculate the new size of the image based on the angle of rotaion
double
radians
=
Math
.
toRadians
(
degrees
);
double
radians
=
Math
.
toRadians
(
degrees
);
double
sin
=
Math
.
abs
(
Math
.
sin
(
radians
));
double
sin
=
Math
.
abs
(
Math
.
sin
(
radians
));
double
cos
=
Math
.
abs
(
Math
.
cos
(
radians
));
double
cos
=
Math
.
abs
(
Math
.
cos
(
radians
));
...
@@ -168,18 +174,17 @@ public class GameFrame extends JFrame {
...
@@ -168,18 +174,17 @@ public class GameFrame extends JFrame {
at
.
setToRotation
(
radians
,
x
+
(
image
.
getWidth
()
/
2
),
y
+
(
image
.
getHeight
()
/
2
));
at
.
setToRotation
(
radians
,
x
+
(
image
.
getWidth
()
/
2
),
y
+
(
image
.
getHeight
()
/
2
));
at
.
translate
(
x
,
y
);
at
.
translate
(
x
,
y
);
g2d
.
setTransform
(
at
);
g2d
.
setTransform
(
at
);
g2d
.
drawImage
(
background
,
0
,
0
,
null
);
// Paint the originl image
// Paint the original image
g2d
.
drawImage
(
image
,
0
,
0
,
null
);
g2d
.
drawImage
(
image
,
0
,
0
,
null
);
g2d
.
dispose
();
g2d
.
dispose
();
this
.
g2d
=
g2d
;
return
rotate
;
return
rotate
;
}
}
/**
/**
* Rendering all game elements based on the game state.
* Rendering all game elements based on the game state.
*/
*/
private
void
doRendering
(
Graphics2D
g2d
,
GameState
state
)
throws
IOException
{
private
void
doRendering
(
Graphics2D
g2d
,
GameState
state
)
{
renderCount
++;
renderCount
++;
// Draw background
// Draw background
g2d
.
setColor
(
Color
.
white
);
g2d
.
setColor
(
Color
.
white
);
...
@@ -193,12 +198,19 @@ public class GameFrame extends JFrame {
...
@@ -193,12 +198,19 @@ public class GameFrame extends JFrame {
g2d
.
setColor
(
Color
.
black
);
g2d
.
setColor
(
Color
.
black
);
//g2d.drawImage(rotate(image1, state1.rotateAmount), state1.locX, state1.locY, null);
//g2d.drawImage(rotate(image1, state1.rotateAmount), state1.locX, state1.locY, null);
setTanks
(
3
,
g2d
,
state
);
setTanks
(
3
,
g2d
,
state
);
setEnemy
(
g2d
,
state
);
setEnemy
(
g2d
,
state
);
setName
(
g2d
,
"narges"
,
"sara"
,
"bardia"
);
setName
(
g2d
,
"narges"
,
"sara"
,
"bardia"
);
setMap
(
g2d
,
new
File
(
"map3.txt"
));
setMap
(
g2d
,
new
File
(
"map3.txt"
));
drawMap
(
g2d
);
drawMap
(
g2d
);
setPrize
(
g2d
);
setPrize
(
g2d
);
state
.
fire
(
g2d
);
if
(
tanks
.
size
()
>
0
)
{
tanks
.
get
(
0
).
x
=
state
.
locX
;
tanks
.
get
(
0
).
y
=
state
.
locY
;
//g2d.drawImage(rotate(tanks.get(0).icon, state.rotateAmountTank), tanks.get(0).x, tanks.get(0).y, null);
}
}
}
public
void
setMap
(
Graphics2D
g2D
,
File
map
)
{
public
void
setMap
(
Graphics2D
g2D
,
File
map
)
{
...
@@ -225,7 +237,7 @@ public class GameFrame extends JFrame {
...
@@ -225,7 +237,7 @@ public class GameFrame extends JFrame {
g2d
.
drawImage
(
image2
,
150
,
750
,
null
);
g2d
.
drawImage
(
image2
,
150
,
750
,
null
);
player1
=
"narges"
;
player1
=
"narges"
;
g2d
.
drawString
(
player1
,
160
,
745
);
g2d
.
drawString
(
player1
,
160
,
745
);
g2d
.
drawString
(
String
.
valueOf
(
tanks
.
get
(
0
).
getHealth
()),
160
,
850
);
g2d
.
drawString
(
String
.
valueOf
(
tanks
.
get
(
0
).
getHealth
()),
160
,
850
);
}
}
// if (player2 != null) {
// if (player2 != null) {
// g2d.drawImage(image3, 450, 750, null);
// g2d.drawImage(image3, 450, 750, null);
...
@@ -240,20 +252,21 @@ public class GameFrame extends JFrame {
...
@@ -240,20 +252,21 @@ public class GameFrame extends JFrame {
// g2d.drawImage(image5, 1050, 750, null);
// g2d.drawImage(image5, 1050, 750, null);
}
}
public
void
setEnemy
(
Graphics2D
g2d
,
GameState
state
)
{
public
void
setEnemy
(
Graphics2D
g2d
,
GameState
state
)
{
Tank
tank
=
new
Tank
(
"enemy.png"
);
Tank
tank
=
new
Tank
(
"enemy.png"
,
state
.
pcX
,
state
.
pcY
,
0
);
taken
=
tank
.
getIcon
();
taken
=
tank
.
getIcon
();
tanks
.
add
(
tank
);
tanks
.
add
(
tank
);
g2d
.
drawImage
(
rotate
(
tank
.
getIcon
(),
state
.
rotateAmountPC
),
state
.
pcX
,
state
.
pcY
,
null
);
g2d
.
drawImage
(
rotate
(
tank
.
getIcon
(),
state
.
rotateAmountPC
),
state
.
pcX
,
state
.
pcY
,
null
);
// System.out.println(pcState.pcX+" "+pcState.pcY);
// System.out.println(pcState.pcX+" "+pcState.pcY);
}
}
public
void
setTanks
(
int
numOfPlayer
,
Graphics2D
g2d
,
GameState
state
)
{
public
void
setTanks
(
int
numOfPlayer
,
Graphics2D
g2d
,
GameState
state
)
{
//g2d.drawImage(background, 0, 0, null);
//g2d.drawImage(background, 0, 0, null);
if
(
numOfPlayer
>
0
)
{
if
(
numOfPlayer
>
0
)
{
Tank
tank
=
new
Tank
(
"tank_blue_RS.png"
);
Tank
tank
=
new
Tank
(
"tank_blue_RS.png"
,
state
.
locX
,
state
.
locY
,
0
);
taken
=
tank
.
getIcon
();
taken
=
tank
.
getIcon
();
tanks
.
add
(
tank
);
tanks
.
add
(
tank
);
g2d
.
drawImage
(
rotate
(
tank
.
getIcon
(),
state
.
rotateAmount
),
state
.
locX
,
state
.
locY
,
null
);
g2d
.
drawImage
(
rotate
(
tank
.
getIcon
(),
state
.
rotateAmount
Tank
),
state
.
locX
,
state
.
locY
,
null
);
// numOfPlayer--;
// numOfPlayer--;
//
//
// if (numOfPlayer > 0) {
// if (numOfPlayer > 0) {
...
@@ -325,7 +338,6 @@ public class GameFrame extends JFrame {
...
@@ -325,7 +338,6 @@ public class GameFrame extends JFrame {
}
}
}
}
}
}
allWallsAdded
=
true
;
currentX
=
30
;
currentX
=
30
;
if
(
lineCounter
%
2
==
0
)
currentY
+=
5
;
if
(
lineCounter
%
2
==
0
)
currentY
+=
5
;
else
currentY
+=
50
;
else
currentY
+=
50
;
...
@@ -339,8 +351,6 @@ public class GameFrame extends JFrame {
...
@@ -339,8 +351,6 @@ public class GameFrame extends JFrame {
}
}
}
}
/**
/**
* Draw prize randomly
* Draw prize randomly
*
*
...
@@ -355,7 +365,7 @@ public class GameFrame extends JFrame {
...
@@ -355,7 +365,7 @@ public class GameFrame extends JFrame {
randomLoc
=
randomLoc
-
1
;
randomLoc
=
randomLoc
-
1
;
if
(
firstPrize
)
{
if
(
firstPrize
)
{
g2d
.
drawImage
(
bullet2
,
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
null
);
g2d
.
drawImage
(
bullet2
,
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
null
);
prizes
.
add
(
new
Prize
(
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
15
,
15
,
bullet2
.
toString
()));
prizes
.
add
(
new
Prize
(
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
15
,
15
,
bullet2
.
toString
()));
firstPrize
=
false
;
firstPrize
=
false
;
lastX
=
prizeLoc
.
get
(
randomLoc
);
lastX
=
prizeLoc
.
get
(
randomLoc
);
lastY
=
prizeLoc
.
get
(
randomLoc
+
1
);
lastY
=
prizeLoc
.
get
(
randomLoc
+
1
);
...
@@ -364,31 +374,35 @@ public class GameFrame extends JFrame {
...
@@ -364,31 +374,35 @@ public class GameFrame extends JFrame {
}
else
if
(
renderCount
!=
renderCountLimit
&&
!
Controller
.
getPrize
)
{
}
else
if
(
renderCount
!=
renderCountLimit
&&
!
Controller
.
getPrize
)
{
g2d
.
drawImage
(
lastPrize
,
lastX
,
lastY
,
null
);
g2d
.
drawImage
(
lastPrize
,
lastX
,
lastY
,
null
);
}
else
{
}
else
{
Controller
.
getPrize
=
false
;
Controller
.
getPrize
=
false
;
if
(
randomNum
==
1
)
{
if
(
randomNum
==
1
)
{
g2d
.
drawImage
(
shield
,
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
null
);
g2d
.
drawImage
(
shield
,
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
null
);
prizes
.
clear
();
prizes
.
clear
();
prizes
.
add
(
new
Prize
(
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
10
,
10
,
shield
.
toString
()));
prizes
.
add
(
new
Prize
(
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
10
,
10
,
shield
.
toString
()));
lastPrize
=
shield
;
lastPrize
=
shield
;
}
if
(
randomNum
==
0
)
{
}
if
(
randomNum
==
0
)
{
g2d
.
drawImage
(
life
,
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
null
);
g2d
.
drawImage
(
life
,
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
null
);
prizes
.
clear
();
prizes
.
clear
();
prizes
.
add
(
new
Prize
(
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
10
,
10
,
life
.
toString
()));
prizes
.
add
(
new
Prize
(
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
10
,
10
,
life
.
toString
()));
lastPrize
=
life
;
lastPrize
=
life
;
}
if
(
randomNum
==
3
)
{
}
if
(
randomNum
==
3
)
{
g2d
.
drawImage
(
bullet3
,
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
null
);
g2d
.
drawImage
(
bullet3
,
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
null
);
prizes
.
clear
();
prizes
.
clear
();
prizes
.
add
(
new
Prize
(
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
10
,
10
,
bullet3
.
toString
()));
prizes
.
add
(
new
Prize
(
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
10
,
10
,
bullet3
.
toString
()));
lastPrize
=
bullet3
;
lastPrize
=
bullet3
;
}
if
(
randomNum
==
2
)
{
}
if
(
randomNum
==
2
)
{
g2d
.
drawImage
(
laser
,
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
null
);
g2d
.
drawImage
(
laser
,
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
null
);
prizes
.
clear
();
prizes
.
clear
();
prizes
.
add
(
new
Prize
(
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
10
,
10
,
laser
.
toString
()));
prizes
.
add
(
new
Prize
(
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
10
,
10
,
laser
.
toString
()));
lastPrize
=
laser
;
lastPrize
=
laser
;
}
if
(
randomNum
==
4
)
{
}
if
(
randomNum
==
4
)
{
g2d
.
drawImage
(
bullet2
,
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
null
);
g2d
.
drawImage
(
bullet2
,
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
null
);
prizes
.
clear
();
prizes
.
clear
();
prizes
.
add
(
new
Prize
(
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
10
,
10
,
bullet2
.
toString
()));
prizes
.
add
(
new
Prize
(
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
10
,
10
,
bullet2
.
toString
()));
lastPrize
=
bullet2
;
lastPrize
=
bullet2
;
}
}
lastX
=
prizeLoc
.
get
(
randomLoc
);
lastX
=
prizeLoc
.
get
(
randomLoc
);
...
@@ -396,5 +410,7 @@ public class GameFrame extends JFrame {
...
@@ -396,5 +410,7 @@ public class GameFrame extends JFrame {
renderCountLimit
=
renderCount
+
300
;
renderCountLimit
=
renderCount
+
300
;
}
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment