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
e7a9037d
Commit
e7a9037d
authored
Jul 26, 2020
by
nargessalehi98
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add destructible wall.
parent
729912de
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
22 deletions
+49
-22
GameFrame.java
src/GameFrame.java
+49
-22
No files found.
src/GameFrame.java
View file @
e7a9037d
...
@@ -6,10 +6,11 @@ import java.awt.*;
...
@@ -6,10 +6,11 @@ import java.awt.*;
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.BufferedReader
;
import
java.io.*
;
import
java.io.File
;
import
java.lang.reflect.Array
;
import
java.io.FileReader
;
import
java.util.ArrayList
;
import
java.io.IOException
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.Scanner
;
import
java.util.Scanner
;
/**
/**
...
@@ -37,8 +38,6 @@ public class GameFrame extends JFrame {
...
@@ -37,8 +38,6 @@ public class GameFrame extends JFrame {
private
BufferStrategy
bufferStrategy
;
private
BufferStrategy
bufferStrategy
;
private
JPanel
PlayerState
;
public
GameFrame
(
String
title
)
{
public
GameFrame
(
String
title
)
{
super
(
title
);
super
(
title
);
...
@@ -46,9 +45,6 @@ public class GameFrame extends JFrame {
...
@@ -46,9 +45,6 @@ public class GameFrame extends JFrame {
setResizable
(
false
);
setResizable
(
false
);
setSize
(
GAME_WIDTH
,
GAME_HEIGHT
);
setSize
(
GAME_WIDTH
,
GAME_HEIGHT
);
setLayout
(
new
BorderLayout
());
setLayout
(
new
BorderLayout
());
PlayerState
=
new
JPanel
();
PlayerState
.
setBackground
(
Color
.
pink
);
add
(
PlayerState
,
BorderLayout
.
SOUTH
);
try
{
try
{
image1
=
ImageIO
.
read
(
new
File
(
"Tank_dark.png"
));
image1
=
ImageIO
.
read
(
new
File
(
"Tank_dark.png"
));
...
@@ -76,7 +72,7 @@ public class GameFrame extends JFrame {
...
@@ -76,7 +72,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
)
{
public
void
render
(
GameState
state
1
,
GameState
state
,
GameState
state2
)
throws
IOException
{
// 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
...
@@ -86,7 +82,7 @@ public class GameFrame extends JFrame {
...
@@ -86,7 +82,7 @@ public class GameFrame extends JFrame {
// to make sure the strategy is validated
// to make sure the strategy is validated
Graphics2D
graphics
=
(
Graphics2D
)
bufferStrategy
.
getDrawGraphics
();
Graphics2D
graphics
=
(
Graphics2D
)
bufferStrategy
.
getDrawGraphics
();
try
{
try
{
doRendering
(
graphics
,
state
);
doRendering
(
graphics
,
state
1
,
state
,
state2
);
}
finally
{
}
finally
{
// Dispose the graphics
// Dispose the graphics
...
@@ -125,7 +121,7 @@ public class GameFrame extends JFrame {
...
@@ -125,7 +121,7 @@ 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
);
// Paint the originl image
// Paint the origin
a
l image
g2d
.
drawImage
(
image
,
0
,
0
,
null
);
g2d
.
drawImage
(
image
,
0
,
0
,
null
);
g2d
.
dispose
();
g2d
.
dispose
();
return
rotate
;
return
rotate
;
...
@@ -134,7 +130,7 @@ public class GameFrame extends JFrame {
...
@@ -134,7 +130,7 @@ public class GameFrame extends JFrame {
/**
/**
* 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
)
{
private
void
doRendering
(
Graphics2D
g2d
,
GameState
state
,
GameState
state1
,
GameState
state2
)
{
// Draw background
// Draw background
g2d
.
setColor
(
Color
.
white
);
g2d
.
setColor
(
Color
.
white
);
g2d
.
fillRect
(
0
,
0
,
GAME_WIDTH
,
GAME_HEIGHT
);
g2d
.
fillRect
(
0
,
0
,
GAME_WIDTH
,
GAME_HEIGHT
);
...
@@ -145,30 +141,40 @@ public class GameFrame extends JFrame {
...
@@ -145,30 +141,40 @@ public class GameFrame extends JFrame {
g2d
.
fillRect
(
0
,
720
,
1280
,
160
);
g2d
.
fillRect
(
0
,
720
,
1280
,
160
);
// g2d.drawImage(image1,state.locX,state.locY,null);
// g2d.drawImage(image1,state.locX,state.locY,null);
g2d
.
setColor
(
Color
.
black
);
g2d
.
setColor
(
Color
.
black
);
g2d
.
drawImage
(
rotate
(
image1
,
state
.
rotateAmount
),
state
.
locX
,
state
.
locY
,
null
);
File
accounts
=
new
File
(
"map2.txt"
);
//g2d.drawImage(rotate(image1, state1.rotateAmount), state1.locX, state1.locY, null);
setTanks
(
3
,
g2d
,
state
,
state1
,
state2
);
File
accounts
=
new
File
(
"map5.txt"
);
setName
(
g2d
,
"narges"
,
"sara"
,
"bardia"
);
setName
(
g2d
,
"narges"
,
"sara"
,
"bardia"
);
setMap
(
g2d
,
new
File
(
"map4.txt"
));
try
(
Scanner
scanner
=
new
Scanner
(
new
FileReader
(
accounts
)))
{
try
(
Scanner
scanner
=
new
Scanner
(
new
FileReader
(
accounts
)))
{
int
lineCounter
=
0
;
int
lineCounter
=
0
;
int
currentX
=
30
;
int
currentX
=
30
;
int
currentY
=
60
;
int
currentY
=
60
;
setMap
(
g2d
,
new
File
(
"map.txt"
));
while
(
scanner
.
hasNext
())
{
while
(
scanner
.
hasNext
())
{
char
[]
chars
=
scanner
.
next
().
toCharArray
();
char
[]
chars
=
scanner
.
next
().
toCharArray
();
for
(
int
k
=
0
;
k
<
chars
.
length
;
k
++)
{
for
(
int
k
=
0
;
k
<
chars
.
length
;
k
++)
{
if
(
lineCounter
%
2
==
0
)
{
if
(
lineCounter
%
2
==
0
)
{
if
(
k
%
2
==
0
)
{
if
(
k
%
2
==
0
)
{
if
(
chars
[
k
]
==
'1'
&&
k
-
1
>=
1
&&
k
+
1
<
chars
.
length
&&
chars
[
k
+
1
]==
'1'
&&
chars
[
k
-
1
]==
'1'
)
if
(
chars
[
k
]
==
'1'
)
g2d
.
fillRect
(
currentX
,
currentY
,
5
,
5
);
else
if
(
chars
[
k
]==
'2'
){
g2d
.
setColor
(
Color
.
gray
);
g2d
.
fillRect
(
currentX
,
currentY
,
5
,
5
);
g2d
.
fillRect
(
currentX
,
currentY
,
5
,
5
);
}
g2d
.
setColor
(
Color
.
black
);
currentX
+=
5
;
currentX
+=
5
;
}
}
else
{
else
{
if
(
chars
[
k
]
==
'1'
)
if
(
chars
[
k
]
==
'1'
)
g2d
.
fillRect
(
currentX
,
currentY
,
50
,
5
);
g2d
.
fillRect
(
currentX
,
currentY
,
50
,
5
);
else
if
(
chars
[
k
]==
'2'
){
g2d
.
setColor
(
Color
.
gray
);
g2d
.
fillRect
(
currentX
,
currentY
,
50
,
5
);
}
g2d
.
setColor
(
Color
.
black
);
currentX
+=
50
;
currentX
+=
50
;
}
}
}
}
...
@@ -176,8 +182,12 @@ public class GameFrame extends JFrame {
...
@@ -176,8 +182,12 @@ public class GameFrame extends JFrame {
if
(
k
%
2
==
0
)
{
if
(
k
%
2
==
0
)
{
if
(
chars
[
k
]
==
'1'
)
if
(
chars
[
k
]
==
'1'
)
g2d
.
fillRect
(
currentX
,
currentY
,
5
,
50
);
g2d
.
fillRect
(
currentX
,
currentY
,
5
,
50
);
else
if
(
chars
[
k
]==
'2'
){
g2d
.
setColor
(
Color
.
gray
);
g2d
.
fillRect
(
currentX
,
currentY
,
5
,
50
);
}
g2d
.
setColor
(
Color
.
black
);
currentX
+=
5
;
currentX
+=
5
;
}
else
{
}
else
{
currentX
+=
50
;
currentX
+=
50
;
}
}
...
@@ -226,4 +236,21 @@ public class GameFrame extends JFrame {
...
@@ -226,4 +236,21 @@ public class GameFrame extends JFrame {
g2d
.
drawImage
(
image5
,
1050
,
750
,
null
);
g2d
.
drawImage
(
image5
,
1050
,
750
,
null
);
}
}
public
void
setTanks
(
int
numOfPlayer
,
Graphics2D
g2d
,
GameState
state
,
GameState
state1
,
GameState
state2
)
{
if
(
numOfPlayer
>
0
)
{
Tank
tank
=
new
Tank
(
"tank_blue_RS.png"
);
g2d
.
drawImage
(
rotate
(
tank
.
getIcon
(),
state
.
rotateAmount
),
state
.
locX
,
state
.
locY
,
null
);
numOfPlayer
--;
if
(
numOfPlayer
>
0
)
{
Tank
tank1
=
new
Tank
(
"tank_green_RS.png"
);
g2d
.
drawImage
(
rotate
(
tank1
.
getIcon
(),
state1
.
rotateAmount
),
state1
.
locX
,
state1
.
locY
,
null
);
numOfPlayer
--;
if
(
numOfPlayer
>
0
)
{
Tank
tank2
=
new
Tank
(
"tank_red_RS.png"
);
g2d
.
drawImage
(
rotate
(
tank2
.
getIcon
(),
state2
.
rotateAmount
),
state2
.
locX
,
state2
.
locY
,
null
);
}
}
}
}
}
}
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