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
ceb1f536
Commit
ceb1f536
authored
Jul 30, 2020
by
nargessalehi98
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add prize method.
parent
ee85ed06
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
92 additions
and
50 deletions
+92
-50
GameFrame.java
src/GameFrame.java
+92
-50
No files found.
src/GameFrame.java
View file @
ceb1f536
...
@@ -11,10 +11,8 @@ import java.awt.image.BufferStrategy;
...
@@ -11,10 +11,8 @@ import java.awt.image.BufferStrategy;
import
java.awt.image.BufferedImage
;
import
java.awt.image.BufferedImage
;
import
java.io.*
;
import
java.io.*
;
import
java.lang.reflect.Array
;
import
java.lang.reflect.Array
;
import
java.util.ArrayList
;
import
java.util.*
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Scanner
;
/**
/**
* The window on which the rendering is performed.
* The window on which the rendering is performed.
...
@@ -42,22 +40,27 @@ public class GameFrame extends JFrame {
...
@@ -42,22 +40,27 @@ public class GameFrame extends JFrame {
private
BufferedImage
taken
;
private
BufferedImage
taken
;
public
int
row
;
public
int
row
;
public
int
col
;
public
int
col
;
long
start
=
System
.
currentTimeMillis
();
long
startSec
=
start
/
1000
;
private
Graphics2D
g2d
;
private
Graphics2D
g2d
;
private
BufferStrategy
bufferStrategy
;
private
BufferStrategy
bufferStrategy
;
private
ArrayList
<
Wall
>
walls
;
private
ArrayList
<
Wall
>
walls
;
private
boolean
allWallsAdded
=
false
;
public
GameFrame
(
String
title
)
throws
AWTException
{
public
GameFrame
(
String
title
)
throws
AWTException
,
IOException
{
super
(
title
);
super
(
title
);
setBackground
(
Color
.
white
);
setBackground
(
Color
.
white
);
setResizable
(
false
);
setResizable
(
false
);
setSize
(
GAME_WIDTH
,
GAME_HEIGHT
);
setSize
(
GAME_WIDTH
,
GAME_HEIGHT
);
setLayout
(
new
BorderLayout
());
setLayout
(
new
BorderLayout
());
walls
=
new
ArrayList
<>();
walls
=
new
ArrayList
<>();
Controller
.
walls
=
walls
;
Controller
.
walls
=
walls
;
Controller
.
g2d
=
g2d
;
Controller
.
g2d
=
g2d
;
Controller
.
taken
=
taken
;
Controller
.
taken
=
taken
;
setPrize
(
g2d
);
try
{
try
{
image1
=
ImageIO
.
read
(
new
File
(
"Tank_dark.png"
));
image1
=
ImageIO
.
read
(
new
File
(
"Tank_dark.png"
));
...
@@ -65,7 +68,7 @@ public class GameFrame extends JFrame {
...
@@ -65,7 +68,7 @@ public class GameFrame extends JFrame {
image3
=
ImageIO
.
read
(
new
File
(
"Tank_red.png"
));
image3
=
ImageIO
.
read
(
new
File
(
"Tank_red.png"
));
image4
=
ImageIO
.
read
(
new
File
(
"Tank_green.png"
));
image4
=
ImageIO
.
read
(
new
File
(
"Tank_green.png"
));
image5
=
ImageIO
.
read
(
new
File
(
"Tank_sand.png"
));
image5
=
ImageIO
.
read
(
new
File
(
"Tank_sand.png"
));
background
=
ImageIO
.
read
(
new
File
(
"background.png"
));
background
=
ImageIO
.
read
(
new
File
(
"background.png"
));
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
System
.
out
.
println
(
e
);
System
.
out
.
println
(
e
);
...
@@ -136,11 +139,11 @@ public class GameFrame extends JFrame {
...
@@ -136,11 +139,11 @@ 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
);
g2d
.
drawImage
(
background
,
0
,
0
,
null
);
// Paint the original image
// Paint the original image
g2d
.
drawImage
(
image
,
0
,
0
,
null
);
g2d
.
drawImage
(
image
,
0
,
0
,
null
);
g2d
.
dispose
();
g2d
.
dispose
();
this
.
g2d
=
g2d
;
this
.
g2d
=
g2d
;
return
rotate
;
return
rotate
;
}
}
...
@@ -164,7 +167,7 @@ public class GameFrame extends JFrame {
...
@@ -164,7 +167,7 @@ public class GameFrame extends JFrame {
setName
(
g2d
,
"narges"
,
"sara"
,
"bardia"
);
setName
(
g2d
,
"narges"
,
"sara"
,
"bardia"
);
setMap
(
g2d
,
new
File
(
"map
5
.txt"
));
setMap
(
g2d
,
new
File
(
"map
3
.txt"
));
drawMap
(
g2d
);
drawMap
(
g2d
);
...
@@ -183,10 +186,10 @@ public class GameFrame extends JFrame {
...
@@ -183,10 +186,10 @@ public class GameFrame extends JFrame {
}
catch
(
Exception
ee
)
{
}
catch
(
Exception
ee
)
{
ee
.
printStackTrace
();
ee
.
printStackTrace
();
}
}
this
.
row
=
row
;
this
.
row
=
row
;
this
.
col
=
column
;
this
.
col
=
column
;
Controller
.
row
=
row
;
Controller
.
row
=
row
;
Controller
.
col
=
col
;
Controller
.
col
=
col
;
}
}
public
void
setName
(
Graphics2D
g2d
,
String
player1
,
String
player2
,
String
player3
)
{
public
void
setName
(
Graphics2D
g2d
,
String
player1
,
String
player2
,
String
player3
)
{
...
@@ -195,24 +198,24 @@ public class GameFrame extends JFrame {
...
@@ -195,24 +198,24 @@ public class GameFrame extends JFrame {
player1
=
"narges"
;
player1
=
"narges"
;
g2d
.
drawString
(
player1
,
160
,
745
);
g2d
.
drawString
(
player1
,
160
,
745
);
}
}
if
(
player2
!=
null
)
{
//
if (player2 != null) {
g2d
.
drawImage
(
image3
,
450
,
750
,
null
);
//
g2d.drawImage(image3, 450, 750, null);
g2d
.
drawString
(
player2
,
455
,
745
);
//
g2d.drawString(player2, 455, 745);
}
//
}
//
if
(
player3
!=
null
)
{
//
if (player3 != null) {
g2d
.
drawImage
(
image4
,
750
,
750
,
null
);
//
g2d.drawImage(image4, 750, 750, null);
g2d
.
drawString
(
player3
,
755
,
745
);
//
g2d.drawString(player3, 755, 745);
}
//
}
//
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
)
{
public
void
setTanks
(
int
numOfPlayer
,
Graphics2D
g2d
,
GameState
state
,
GameState
state1
,
GameState
state2
)
{
//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"
);
taken
=
tank
.
getIcon
();
taken
=
tank
.
getIcon
();
g2d
.
drawImage
(
rotate
(
tank
.
getIcon
(),
state
.
rotateAmount
),
state
.
locX
,
state
.
locY
,
null
);
g2d
.
drawImage
(
rotate
(
tank
.
getIcon
(),
state
.
rotateAmount
),
state
.
locX
,
state
.
locY
,
null
);
// numOfPlayer--;
// numOfPlayer--;
...
@@ -229,7 +232,7 @@ public class GameFrame extends JFrame {
...
@@ -229,7 +232,7 @@ public class GameFrame extends JFrame {
}
}
public
void
drawMap
(
Graphics2D
g2d
)
{
public
void
drawMap
(
Graphics2D
g2d
)
{
File
accounts
=
new
File
(
"map
5
.txt"
);
File
accounts
=
new
File
(
"map
3
.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
;
...
@@ -240,28 +243,23 @@ public class GameFrame extends JFrame {
...
@@ -240,28 +243,23 @@ public class GameFrame extends JFrame {
if
(
lineCounter
%
2
==
0
)
{
if
(
lineCounter
%
2
==
0
)
{
if
(
k
%
2
==
0
)
{
if
(
k
%
2
==
0
)
{
if
(
chars
[
k
]
==
'1'
)
{
if
(
chars
[
k
]
==
'1'
)
{
Wall
wall
=
new
Wall
(
currentX
,
currentY
,
5
,
5
,
g2d
,
k
);
Wall
wall
=
new
Wall
(
currentX
,
currentY
,
5
,
5
,
g2d
);
//System.out.println("black"+currentX+"-"+currentY);
walls
.
add
(
wall
);
walls
.
add
(
wall
);
}
else
if
(
chars
[
k
]
==
'2'
)
{
}
else
if
(
chars
[
k
]
==
'2'
)
{
g2d
.
setColor
(
Color
.
lightGray
);
g2d
.
setColor
(
Color
.
lightGray
);
//System.out.println("graye"+currentX+"-"+currentY);
Wall
wall
=
new
Wall
(
currentX
,
currentY
,
5
,
5
,
g2d
);
Wall
wall
=
new
Wall
(
currentX
,
currentY
,
5
,
5
,
g2d
,
k
);
walls
.
add
(
wall
);
walls
.
add
(
wall
);
}
}
g2d
.
setColor
(
Color
.
black
);
g2d
.
setColor
(
Color
.
black
);
currentX
+=
5
;
currentX
+=
5
;
}
else
{
}
else
{
if
(
chars
[
k
]
==
'1'
)
{
if
(
chars
[
k
]
==
'1'
)
{
Wall
wall
=
new
Wall
(
currentX
,
currentY
,
50
,
5
,
g2d
,
k
);
Wall
wall
=
new
Wall
(
currentX
,
currentY
,
50
,
5
,
g2d
);
//System.out.println("black"+currentX+"-"+currentY);
walls
.
add
(
wall
);
walls
.
add
(
wall
);
}
}
else
if
(
chars
[
k
]
==
'2'
)
{
else
if
(
chars
[
k
]
==
'2'
)
{
g2d
.
setColor
(
Color
.
lightGray
);
g2d
.
setColor
(
Color
.
lightGray
);
//System.out.println("graye"+currentX+"-"+currentY);
Wall
wall
=
new
Wall
(
currentX
,
currentY
,
50
,
5
,
g2d
);
Wall
wall
=
new
Wall
(
currentX
,
currentY
,
50
,
5
,
g2d
,
k
);
walls
.
add
(
wall
);
walls
.
add
(
wall
);
}
}
g2d
.
setColor
(
Color
.
black
);
g2d
.
setColor
(
Color
.
black
);
...
@@ -271,14 +269,11 @@ public class GameFrame extends JFrame {
...
@@ -271,14 +269,11 @@ public class GameFrame extends JFrame {
if
(
lineCounter
%
2
!=
0
)
{
if
(
lineCounter
%
2
!=
0
)
{
if
(
k
%
2
==
0
)
{
if
(
k
%
2
==
0
)
{
if
(
chars
[
k
]
==
'1'
)
{
if
(
chars
[
k
]
==
'1'
)
{
Wall
wall
=
new
Wall
(
currentX
,
currentY
,
5
,
50
,
g2d
,
k
);
Wall
wall
=
new
Wall
(
currentX
,
currentY
,
5
,
50
,
g2d
);
//System.out.println("black"+currentX+"-"+currentY);
walls
.
add
(
wall
);
walls
.
add
(
wall
);
}
}
else
if
(
chars
[
k
]
==
'2'
)
{
else
if
(
chars
[
k
]
==
'2'
)
{
g2d
.
setColor
(
Color
.
lightGray
);
g2d
.
setColor
(
Color
.
lightGray
);
//System.out.println("graye"+currentX+"-"+currentY);
Wall
wall
=
new
Wall
(
currentX
,
currentY
,
5
,
50
,
g2d
);
Wall
wall
=
new
Wall
(
currentX
,
currentY
,
5
,
50
,
g2d
,
k
);
walls
.
add
(
wall
);
walls
.
add
(
wall
);
}
}
g2d
.
setColor
(
Color
.
black
);
g2d
.
setColor
(
Color
.
black
);
...
@@ -288,6 +283,7 @@ public class GameFrame extends JFrame {
...
@@ -288,6 +283,7 @@ 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
;
...
@@ -298,9 +294,55 @@ public class GameFrame extends JFrame {
...
@@ -298,9 +294,55 @@ public class GameFrame extends JFrame {
}
}
}
}
public
void
getColor
(
int
x
,
int
y
)
throws
AWTException
{
public
boolean
checkWallAdded
(
int
currentX
,
int
currentY
)
{
Robot
robot
=
new
Robot
();
String
str
=
currentX
+
" "
+
currentY
;
System
.
out
.
println
(
robot
.
getPixelColor
(
x
,
y
));
for
(
Wall
wall
:
walls
)
{
if
(
wall
.
toString
().
equals
(
str
))
return
false
;
else
return
true
;
}
}
return
true
;
}
public
void
getColor
(
int
x
,
int
y
)
throws
AWTException
{
Robot
robot
=
new
Robot
();
System
.
out
.
println
(
robot
.
getPixelColor
(
x
,
y
));
}
public
void
setPrize
(
Graphics2D
g2d
)
throws
IOException
{
long
now
=
System
.
currentTimeMillis
();
now
=
now
/
1000
;
int
randNum
=
(
int
)
(
Math
.
random
()*(
5
));
System
.
out
.
println
(
randNum
);
if
(
now
-
startSec
==
15
)
{
BufferedImage
bullet2
;
BufferedImage
bullet3
;
BufferedImage
life
;
BufferedImage
laser
;
BufferedImage
shield
;
bullet2
=
ImageIO
.
read
(
new
File
(
"2B.png"
));
bullet3
=
ImageIO
.
read
(
new
File
(
"3B.png"
));
life
=
ImageIO
.
read
(
new
File
(
"life.png"
));
laser
=
ImageIO
.
read
(
new
File
(
"laser.png"
));
shield
=
ImageIO
.
read
(
new
File
(
"shield.png"
));
int
x
,
y
;
// if(randNum==0){
// g2d.drawImage(bullet2,,,null);
// }
// if(randNum==1){
// g2d.drawImage(bullet3,,,null);
// }
// if(randNum==2){
// g2d.drawImage(life,,,null);
// }
// if(randNum==3){
// g2d.drawImage(laser,,,null);
// }
// if(randNum==4){
// g2d.drawImage(shield,,,null);
// }
}
startSec
=
now
;
}
}
}
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