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
cfdf5e7d
Commit
cfdf5e7d
authored
Aug 01, 2020
by
nargessalehi98
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add setPrize method.
parent
8ed3c704
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
92 additions
and
39 deletions
+92
-39
GameFrame.java
src/GameFrame.java
+92
-39
No files found.
src/GameFrame.java
View file @
cfdf5e7d
/*** In The Name of Allah ***/
/*** In The Name of Allah ***/
import
javax.imageio.ImageIO
;
import
javax.imageio.ImageIO
;
import
javax.sound.midi.Soundbank
;
import
javax.sound.sampled.*
;
import
javax.swing.*
;
import
javax.swing.*
;
import
java.applet.AudioClip
;
import
java.awt.*
;
import
java.awt.*
;
import
java.awt.event.MouseAdapter
;
import
java.awt.event.MouseAdapter
;
import
java.awt.event.MouseEvent
;
import
java.awt.event.MouseEvent
;
...
@@ -38,14 +41,29 @@ public class GameFrame extends JFrame {
...
@@ -38,14 +41,29 @@ public class GameFrame extends JFrame {
private
BufferedImage
image5
;
private
BufferedImage
image5
;
private
BufferedImage
background
;
private
BufferedImage
background
;
private
BufferedImage
taken
;
private
BufferedImage
taken
;
public
BufferedImage
bullet2
;
public
BufferedImage
bullet3
;
public
BufferedImage
life
;
public
BufferedImage
laser
;
public
BufferedImage
shield
;
public
ArrayList
<
Integer
>
prizeLoc
;
public
int
realMapSize
;
public
int
row
;
public
int
row
;
public
int
col
;
public
int
col
;
long
start
=
System
.
currentTimeMillis
();
long
start
=
System
.
currentTimeMillis
();
long
startSec
=
start
/
1000
;
long
startSec
=
start
/
1000
;
public
long
renderCount
=
0
;
public
long
renderCountLimit
=
0
;
int
lastX
=
120
;
int
lastY
=
120
;
boolean
firstPrize
=
true
;
boolean
getPrize
=
false
;
public
BufferedImage
lastPrize
;
private
Graphics2D
g2d
;
private
Graphics2D
g2d
;
private
BufferStrategy
bufferStrategy
;
private
BufferStrategy
bufferStrategy
;
private
ArrayList
<
Wall
>
walls
;
private
ArrayList
<
Wall
>
walls
;
private
ArrayList
<
Prize
>
prizes
;
private
boolean
allWallsAdded
=
false
;
private
boolean
allWallsAdded
=
false
;
public
GameFrame
(
String
title
)
throws
AWTException
,
IOException
{
public
GameFrame
(
String
title
)
throws
AWTException
,
IOException
{
...
@@ -59,8 +77,11 @@ public class GameFrame extends JFrame {
...
@@ -59,8 +77,11 @@ public class GameFrame extends JFrame {
Controller
.
walls
=
walls
;
Controller
.
walls
=
walls
;
Controller
.
g2d
=
g2d
;
Controller
.
g2d
=
g2d
;
Controller
.
taken
=
taken
;
Controller
.
taken
=
taken
;
prizeLoc
=
new
ArrayList
<>();
prizes
=
new
ArrayList
<>();
Controller
.
prizes
=
prizes
;
Controller
.
getPrize
=
getPrize
;
setPrize
(
g2d
);
try
{
try
{
image1
=
ImageIO
.
read
(
new
File
(
"Tank_dark.png"
));
image1
=
ImageIO
.
read
(
new
File
(
"Tank_dark.png"
));
...
@@ -69,6 +90,11 @@ public class GameFrame extends JFrame {
...
@@ -69,6 +90,11 @@ public class GameFrame extends JFrame {
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"
));
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"
));
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
System
.
out
.
println
(
e
);
System
.
out
.
println
(
e
);
...
@@ -150,7 +176,8 @@ public class GameFrame extends JFrame {
...
@@ -150,7 +176,8 @@ 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
,
GameState
state1
,
GameState
state2
)
{
private
void
doRendering
(
Graphics2D
g2d
,
GameState
state
,
GameState
state1
,
GameState
state2
)
throws
IOException
{
renderCount
++;
// 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
);
...
@@ -161,15 +188,12 @@ public class GameFrame extends JFrame {
...
@@ -161,15 +188,12 @@ 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, state1.rotateAmount), state1.locX, state1.locY, null);
//g2d.drawImage(rotate(image1, state1.rotateAmount), state1.locX, state1.locY, null);
setTanks
(
3
,
g2d
,
state
,
state1
,
state2
);
setTanks
(
3
,
g2d
,
state
,
state1
,
state2
);
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
);
}
}
...
@@ -279,7 +303,12 @@ public class GameFrame extends JFrame {
...
@@ -279,7 +303,12 @@ public class GameFrame extends JFrame {
g2d
.
setColor
(
Color
.
black
);
g2d
.
setColor
(
Color
.
black
);
currentX
+=
5
;
currentX
+=
5
;
}
else
{
}
else
{
if
(
firstPrize
)
{
prizeLoc
.
add
(
currentX
+
25
);
prizeLoc
.
add
(
currentY
+
25
);
}
currentX
+=
50
;
currentX
+=
50
;
}
}
}
}
}
}
...
@@ -292,6 +321,9 @@ public class GameFrame extends JFrame {
...
@@ -292,6 +321,9 @@ public class GameFrame extends JFrame {
}
catch
(
Exception
ee
)
{
}
catch
(
Exception
ee
)
{
ee
.
printStackTrace
();
ee
.
printStackTrace
();
}
}
if
(
firstPrize
)
{
realMapSize
=
prizeLoc
.
size
();
}
}
}
public
boolean
checkWallAdded
(
int
currentX
,
int
currentY
)
{
public
boolean
checkWallAdded
(
int
currentX
,
int
currentY
)
{
...
@@ -310,39 +342,60 @@ public class GameFrame extends JFrame {
...
@@ -310,39 +342,60 @@ public class GameFrame extends JFrame {
}
}
public
void
setPrize
(
Graphics2D
g2d
)
throws
IOException
{
/**
long
now
=
System
.
currentTimeMillis
();
* Draw prize randomly
now
=
now
/
1000
;
*
int
randNum
=
(
int
)
(
Math
.
random
()*(
5
));
* @param g2d graphic2D of game
System
.
out
.
println
(
randNum
);
* @throws IOException
if
(
now
-
startSec
==
15
)
{
*/
BufferedImage
bullet2
;
public
void
setPrize
(
Graphics2D
g2d
)
{
BufferedImage
bullet3
;
int
randomLoc
=
(
int
)
(
Math
.
random
()
*
(
realMapSize
));
BufferedImage
life
;
int
randomNum
=
(
int
)
(
Math
.
random
()
*
(
5
));
BufferedImage
laser
;
BufferedImage
shield
;
if
(
randomLoc
%
2
!=
0
)
bullet2
=
ImageIO
.
read
(
new
File
(
"2B.png"
));
randomLoc
=
randomLoc
-
1
;
bullet3
=
ImageIO
.
read
(
new
File
(
"3B.png"
));
if
(
firstPrize
)
{
life
=
ImageIO
.
read
(
new
File
(
"life.png"
));
g2d
.
drawImage
(
bullet2
,
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
null
);
laser
=
ImageIO
.
read
(
new
File
(
"laser.png"
));
prizes
.
add
(
new
Prize
(
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
15
,
15
,
bullet2
.
toString
()));
shield
=
ImageIO
.
read
(
new
File
(
"shield.png"
));
firstPrize
=
false
;
int
x
,
y
;
lastX
=
prizeLoc
.
get
(
randomLoc
);
// if(randNum==0){
lastY
=
prizeLoc
.
get
(
randomLoc
+
1
);
// g2d.drawImage(bullet2,,,null);
renderCountLimit
=
renderCount
+
180
;
// }
lastPrize
=
bullet2
;
// if(randNum==1){
}
else
if
(
renderCount
!=
renderCountLimit
&&
!
Controller
.
getPrize
)
{
// g2d.drawImage(bullet3,,,null);
g2d
.
drawImage
(
lastPrize
,
lastX
,
lastY
,
null
);
// }
}
else
{
// if(randNum==2){
Controller
.
getPrize
=
false
;
// g2d.drawImage(life,,,null);
if
(
randomNum
==
1
)
{
// }
g2d
.
drawImage
(
shield
,
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
null
);
// if(randNum==3){
prizes
.
clear
();
// g2d.drawImage(laser,,,null);
prizes
.
add
(
new
Prize
(
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
10
,
10
,
shield
.
toString
()));
// }
lastPrize
=
shield
;
// if(randNum==4){
}
if
(
randomNum
==
0
)
{
// g2d.drawImage(shield,,,null);
g2d
.
drawImage
(
life
,
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
null
);
// }
prizes
.
clear
();
prizes
.
add
(
new
Prize
(
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
10
,
10
,
life
.
toString
()));
lastPrize
=
life
;
}
if
(
randomNum
==
3
)
{
g2d
.
drawImage
(
bullet3
,
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
null
);
prizes
.
clear
();
prizes
.
add
(
new
Prize
(
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
10
,
10
,
bullet3
.
toString
()));
lastPrize
=
bullet3
;
}
if
(
randomNum
==
2
)
{
g2d
.
drawImage
(
laser
,
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
null
);
prizes
.
clear
();
prizes
.
add
(
new
Prize
(
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
10
,
10
,
laser
.
toString
()));
lastPrize
=
laser
;
}
if
(
randomNum
==
4
)
{
g2d
.
drawImage
(
bullet2
,
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
null
);
prizes
.
clear
();
prizes
.
add
(
new
Prize
(
prizeLoc
.
get
(
randomLoc
),
prizeLoc
.
get
(
randomLoc
+
1
),
10
,
10
,
bullet2
.
toString
()));
lastPrize
=
bullet2
;
}
lastX
=
prizeLoc
.
get
(
randomLoc
);
lastY
=
prizeLoc
.
get
(
randomLoc
+
1
);
renderCountLimit
=
renderCount
+
180
;
}
}
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