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
3c1f24f1
Commit
3c1f24f1
authored
Aug 03, 2020
by
nargessalehi98
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Computer Player logic.
parent
7362786b
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
339 additions
and
158 deletions
+339
-158
GameState.java
src/GameState.java
+339
-158
No files found.
src/GameState.java
View file @
3c1f24f1
/*** In The Name of Allah ***/
import
org.w3c.dom.css.RGBColor
;
import
javax.imageio.ImageIO
;
import
java.awt.*
;
import
java.awt.event.KeyAdapter
;
import
java.awt.event.KeyEvent
;
...
...
@@ -12,7 +11,11 @@ import java.awt.event.MouseListener;
import
java.awt.event.MouseMotionListener
;
import
java.awt.geom.AffineTransform
;
import
java.awt.geom.Ellipse2D
;
import
java.awt.geom.Rectangle2D
;
import
java.awt.image.BufferedImage
;
import
java.io.File
;
import
java.io.IOException
;
import
java.util.ArrayList
;
/**
* This class holds the state of game and all of its elements.
...
...
@@ -23,38 +26,64 @@ import java.awt.image.BufferedImage;
public
class
GameState
{
public
int
locX
,
locY
,
diam
;
public
boolean
gameOver
;
private
boolean
keyUP
,
keyDOWN
,
keyRIGHT
,
keyLEFT
;
public
int
pcX
,
pcY
;
public
boolean
gameOver
,
keyM
;
public
boolean
keyUP
,
keyDOWN
,
keyRIGHT
,
keyLEFT
;
private
boolean
mousePress
;
private
int
mouseX
,
mouseY
;
private
KeyHandler
keyHandler
;
private
MouseHandler
mouseHandler
;
public
double
rotateAmount
;
public
double
rotateAmountTank
;
public
double
rotateAmountPC
;
private
boolean
one
=
false
;
private
boolean
two
=
false
;
private
boolean
three
=
false
;
private
int
step
=
0
;
public
double
rotateAmountTank
;
private
boolean
left
;
private
boolean
right
;
private
boolean
up
;
private
boolean
down
;
boolean
PermissionUp
=
true
;
boolean
PermissionDown
=
true
;
public
double
rotateAmountBullet
;
private
int
bulletTime
=
0
;
private
int
tankTime
=
0
;
public
ArrayList
<
Bullet
>
bullets
;
public
GameState
(
int
num
)
{
locX
=
100
;
locY
=
100
;
diam
=
42
;
rotateAmount
=
0
;
diam
=
32
;
pcX
=
40
;
pcY
=
100
;
rotateAmountTank
=
0
;
rotateAmountBullet
=
0
;
gameOver
=
false
;
//
keyUP
=
false
;
keyDOWN
=
false
;
keyRIGHT
=
false
;
keyLEFT
=
false
;
keyM
=
false
;
//
up
=
true
;
down
=
true
;
left
=
true
;
right
=
true
;
//
mousePress
=
false
;
mouseX
=
0
;
mouseY
=
0
;
//
keyHandler
=
new
KeyHandler
();
mouseHandler
=
new
MouseHandler
();
if
(
num
==
1
)
{
one
=
true
;
}
...
...
@@ -64,48 +93,14 @@ public class GameState {
if
(
num
==
3
)
{
three
=
true
;
}
keyHandler
=
new
KeyHandler
();
mouseHandler
=
new
MouseHandler
();
}
public
Rectangle
getBounds
(
int
locX
,
int
locY
)
{
return
new
Rectangle
(
locX
,
locY
,
25
,
25
);
bullets
=
new
ArrayList
<>();
}
/**
* The method which updates the game state.
*/
public
void
update
()
throws
AWTException
{
if
(
rotateAmount
>=
360
||
rotateAmount
<=
-
360
)
rotateAmount
=
0
;
if
(
mousePress
)
{
locY
=
mouseY
-
diam
/
2
;
locX
=
mouseX
-
diam
/
2
;
}
if
(
keyUP
)
{
move
(+
5
);
}
if
(
keyDOWN
)
{
move
(-
5
);
}
if
(
keyLEFT
)
rotateAmount
-=
15
;
if
(
keyRIGHT
)
rotateAmount
+=
15
;
locX
=
Math
.
max
(
locX
,
40
);
locX
=
Math
.
min
(
locX
,
20
+
(((
Controller
.
col
-
1
)
/
2
)
*
50
)
+
(((
Controller
.
col
-
1
)
/
2
)
+
1
)
*
5
-
25
);
locY
=
Math
.
max
(
locY
,
70
);
locY
=
Math
.
min
(
locY
,
50
+
((
Controller
.
row
-
1
)
/
2
)
*
50
+
(((
Controller
.
row
-
1
)
/
2
)
+
1
)
*
5
-
25
);
public
Rectangle
getBounds
(
int
locX
,
int
locY
)
{
return
new
Rectangle
(
locX
,
locY
,
25
,
25
);
}
public
Wall
verticalWallCollision
()
{
for
(
Wall
wall
:
Controller
.
walls
)
{
if
(
wall
.
getWidth
()
==
5
&&
wall
.
getHeight
()
==
50
)
{
...
...
@@ -132,24 +127,24 @@ public class GameState {
if
(
verticalWallCollision
()
!=
null
)
{
if
(
verticalWallCollision
().
getX
()
>
locX
&&
px
>
0
)
{
if
((
rotateAmount
<=
90
&&
rotateAmount
>=
-
90
)
||
(
rotateAmount
<=
-
270
&&
rotateAmount
>
-
360
)
||
(
rotateAmount
>=
270
&&
rotateAmount
<
360
))
{
if
((
rotateAmount
Tank
<=
90
&&
rotateAmountTank
>=
-
90
)
||
(
rotateAmountTank
<=
-
270
&&
rotateAmountTank
>
-
360
)
||
(
rotateAmountTank
>=
270
&&
rotateAmountTank
<
360
))
{
return
false
;
}
else
return
true
;
}
if
(
verticalWallCollision
().
getX
()
<
locX
&&
px
<
0
)
{
if
((
rotateAmount
<=
90
&&
rotateAmount
>=
-
90
)
||
(
rotateAmount
<=
-
270
&&
rotateAmount
>
-
360
)
||
(
rotateAmount
>=
270
&&
rotateAmount
<
360
))
{
if
((
rotateAmount
Tank
<=
90
&&
rotateAmountTank
>=
-
90
)
||
(
rotateAmountTank
<=
-
270
&&
rotateAmountTank
>
-
360
)
||
(
rotateAmountTank
>=
270
&&
rotateAmountTank
<
360
))
{
return
false
;
}
else
return
true
;
}
if
(
verticalWallCollision
().
getX
()
>
locX
&&
px
<
0
)
{
if
((
rotateAmount
>=
90
&&
rotateAmount
<=
270
)
||
(
rotateAmount
<=
-
90
&&
rotateAmount
>=
-
270
))
{
if
((
rotateAmount
Tank
>=
90
&&
rotateAmountTank
<=
270
)
||
(
rotateAmountTank
<=
-
90
&&
rotateAmountTank
>=
-
270
))
{
return
false
;
}
else
return
true
;
}
if
(
verticalWallCollision
().
getX
()
<
locX
&&
px
>
0
)
{
if
((
rotateAmount
>=
90
||
rotateAmount
<=
270
)
||
(
rotateAmount
<=
-
90
&&
rotateAmount
>=
-
270
))
{
if
((
rotateAmount
Tank
>=
90
||
rotateAmountTank
<=
270
)
||
(
rotateAmountTank
<=
-
90
&&
rotateAmountTank
>=
-
270
))
{
return
false
;
}
else
return
true
;
}
...
...
@@ -157,29 +152,22 @@ public class GameState {
}
if
(
horizontalWallCollision
()
!=
null
)
{
if
(
horizontalWallCollision
().
getY
()
>
locY
&&
px
>
0
)
{
if
((
rotateAmount
<=
180
&&
rotateAmount
>=
0
)
||
(
rotateAmount
<=
-
180
&&
rotateAmount
>
-
360
))
{
System
.
out
.
println
(
"1"
);
if
((
rotateAmountTank
<=
180
&&
rotateAmountTank
>=
0
)
||
(
rotateAmountTank
<=
-
180
&&
rotateAmountTank
>
-
360
))
{
return
false
;
}
else
return
true
;
}
if
(
horizontalWallCollision
().
getY
()
<
locY
&&
px
<
0
)
{
if
((
rotateAmount
<=
180
&&
rotateAmount
>=
0
)
||
(
rotateAmount
<=
-
180
&&
rotateAmount
>
-
360
))
{
System
.
out
.
println
(
"2"
);
if
((
rotateAmountTank
<=
180
&&
rotateAmountTank
>=
0
)
||
(
rotateAmountTank
<=
-
180
&&
rotateAmountTank
>
-
360
))
{
return
false
;
}
else
return
true
;
}
if
(
horizontalWallCollision
().
getY
()
<
locY
&&
px
>
0
)
{
if
((
rotateAmount
>=
-
180
&&
rotateAmount
<=
0
)
||
(
rotateAmount
>=
180
&&
rotateAmount
<
360
))
{
System
.
out
.
println
(
"3"
);
if
((
rotateAmountTank
>=
-
180
&&
rotateAmountTank
<=
0
)
||
(
rotateAmountTank
>=
180
&&
rotateAmountTank
<
360
))
{
return
false
;
}
else
return
true
;
}
if
(
horizontalWallCollision
().
getY
()
>
locY
&&
px
<
0
)
{
if
((
rotateAmount
>=
-
180
&&
rotateAmount
<=
0
)
||
(
rotateAmount
>=
180
&&
rotateAmount
<
360
))
{
System
.
out
.
println
(
"4"
);
if
((
rotateAmountTank
>=
-
180
&&
rotateAmountTank
<=
0
)
||
(
rotateAmountTank
>=
180
&&
rotateAmountTank
<
360
))
{
return
false
;
}
else
return
true
;
}
...
...
@@ -188,63 +176,220 @@ public class GameState {
return
true
;
}
public
void
move
(
int
px
)
{
double
d
;
public
void
pcUpdate
()
{
if
(!
isMoveAllowed
(
px
))
px
=
0
;
for
(
Wall
wall
:
Controller
.
walls
)
{
if
(
rotateAmount
==
0
)
locX
+=
px
;
if
(
rotateAmount
==
90
||
rotateAmount
==
-
270
)
locY
+=
px
;
if
(
rotateAmount
==
-
90
||
rotateAmount
==
270
)
locY
-=
px
;
if
(
rotateAmount
==
180
||
rotateAmount
==
-
180
)
locX
-=
px
;
if
(
wall
.
getWidth
()
==
5
&&
wall
.
getHeight
()
==
50
)
{
if
(
getBounds
(
pcX
,
pcY
).
intersects
(
new
Rectangle
((
int
)
wall
.
getX
(),
(
int
)
wall
.
getY
(),
(
int
)
wall
.
getWidth
(),
(
int
)
wall
.
getHeight
()))
&&
wall
.
getX
()
<
pcX
)
{
left
=
false
;
System
.
out
.
println
(
"3"
);
break
;
}
if
(
getBounds
(
pcX
,
pcY
).
intersects
(
new
Rectangle
((
int
)
wall
.
getX
(),
(
int
)
wall
.
getY
(),
(
int
)
wall
.
getWidth
(),
(
int
)
wall
.
getHeight
()))
&&
wall
.
getX
()
>
pcX
)
{
right
=
false
;
System
.
out
.
println
(
"4"
);
break
;
}
if
((
rotateAmount
>
0
&&
rotateAmount
<
90
)
||
(
rotateAmount
<
-
270
&&
rotateAmount
>
-
360
))
{
if
(
px
>
0
)
{
locX
+=
px
*
Math
.
cos
(
rotateAmount
*
Math
.
PI
/
180
);
locY
+=
px
*
Math
.
sin
(
rotateAmount
*
Math
.
PI
/
180
);
}
else
{
locX
+=
px
*
Math
.
cos
(
rotateAmount
*
Math
.
PI
/
180
);
locY
+=
px
*
Math
.
sin
(
rotateAmount
*
Math
.
PI
/
180
);
}
if
(
wall
.
getWidth
()
==
50
&&
wall
.
getHeight
()
==
5
)
{
if
(
getBounds
(
pcX
,
pcY
).
intersects
(
new
Rectangle
((
int
)
wall
.
getX
(),
(
int
)
wall
.
getY
(),
(
int
)
wall
.
getWidth
(),
(
int
)
wall
.
getHeight
()))
&&
wall
.
getY
()
>
pcY
)
{
down
=
false
;
System
.
out
.
println
(
"2"
);
break
;
}
if
(
getBounds
(
pcX
,
pcY
).
intersects
(
new
Rectangle
((
int
)
wall
.
getX
(),
(
int
)
wall
.
getY
(),
(
int
)
wall
.
getWidth
(),
(
int
)
wall
.
getHeight
()))
&&
wall
.
getY
()
<
pcY
)
{
System
.
out
.
println
(
"1"
);
up
=
false
;
break
;
}
if
((
rotateAmount
>
90
&&
rotateAmount
<
180
)
||
(
rotateAmount
<
-
180
&&
rotateAmount
>
-
270
))
{
if
(
px
>
0
)
{
d
=
rotateAmount
-
90
;
locX
-=
px
*
Math
.
sin
(
d
*
Math
.
PI
/
180
);
locY
+=
px
*
Math
.
cos
(
d
*
Math
.
PI
/
180
);
}
if
(!
up
&&
!
down
&&
!
left
&&
!
right
)
{
if
(
getBounds
(
pcX
,
pcY
).
intersects
(
new
Rectangle
((
int
)
wall
.
getX
(),
(
int
)
wall
.
getY
(),
(
int
)
wall
.
getWidth
(),
(
int
)
wall
.
getHeight
()))
&&
wall
.
getX
()
<
pcX
)
{
left
=
false
;
System
.
out
.
println
(
"33"
);
}
else
{
d
=
rotateAmount
+
270
;
locX
-=
px
*
Math
.
sin
(
d
*
Math
.
PI
/
180
);
locY
+=
px
*
Math
.
cos
(
d
*
Math
.
PI
/
180
);
left
=
true
;
}
if
(
getBounds
(
pcX
,
pcY
).
intersects
(
new
Rectangle
((
int
)
wall
.
getX
(),
(
int
)
wall
.
getY
(),
(
int
)
wall
.
getWidth
(),
(
int
)
wall
.
getHeight
()))
&&
wall
.
getX
()
>
pcX
)
{
right
=
false
;
System
.
out
.
println
(
"44"
);
}
else
right
=
true
;
if
(
wall
.
getWidth
()
==
50
&&
wall
.
getHeight
()
==
5
)
{
if
(
getBounds
(
pcX
,
pcY
).
intersects
(
new
Rectangle
((
int
)
wall
.
getX
(),
(
int
)
wall
.
getY
(),
(
int
)
wall
.
getWidth
(),
(
int
)
wall
.
getHeight
()))
&&
wall
.
getY
()
>
pcY
)
{
down
=
false
;
System
.
out
.
println
(
"22"
);
break
;
}
else
down
=
true
;
if
(
getBounds
(
pcX
,
pcY
).
intersects
(
new
Rectangle
((
int
)
wall
.
getX
(),
(
int
)
wall
.
getY
(),
(
int
)
wall
.
getWidth
(),
(
int
)
wall
.
getHeight
()))
&&
wall
.
getY
()
<
pcY
)
{
System
.
out
.
println
(
"11"
);
up
=
false
;
break
;
}
else
up
=
true
;
}
}
if
((
rotateAmount
>
180
&&
rotateAmount
<
270
)
||
(
rotateAmount
<
-
90
&&
rotateAmount
>
-
180
))
{
if
(
px
>
0
)
{
d
=
rotateAmount
-
180
;
locX
-=
px
*
Math
.
cos
(
d
*
Math
.
PI
/
180
);
locY
-=
px
*
Math
.
sin
(
d
*
Math
.
PI
/
180
);
}
else
{
d
=
rotateAmount
+
180
;
locX
-=
px
*
Math
.
cos
(
d
*
Math
.
PI
/
180
);
locY
-=
px
*
Math
.
sin
(
d
*
Math
.
PI
/
180
);
}
if
(
up
)
{
pcY
-=
2
;
rotateAmountPC
=
270
;
}
else
if
(
down
)
{
pcY
+=
2
;
rotateAmountPC
=
90
;
}
else
if
(
right
)
{
pcX
-=
2
;
rotateAmountPC
=
180
;
}
else
if
(
left
)
{
pcX
+=
2
;
rotateAmountPC
=
0
;
}
if
((
rotateAmount
>
270
&&
rotateAmount
<
360
)
||
(
rotateAmount
<
0
&&
rotateAmount
>
-
90
))
{
if
(
px
>
0
)
{
d
=
rotateAmount
-
270
;
locX
+=
px
*
Math
.
sin
(
d
*
Math
.
PI
/
180
);
locY
-=
px
*
Math
.
cos
(
d
*
Math
.
PI
/
180
);
}
else
{
d
=
rotateAmount
+
90
;
locX
+=
px
*
Math
.
sin
(
d
*
Math
.
PI
/
180
);
locY
-=
px
*
Math
.
cos
(
d
*
Math
.
PI
/
180
);
// pcX = Math.max(pcX, 40);
// pcX = Math.min(pcX, 20 + (((Controller.col - 1) / 2) * 50) + (((Controller.col - 1) / 2) + 1) * 5 - 25);
//
// pcY = Math.max(pcY, 70);
// pcY = Math.min(pcY, 50 + ((Controller.row - 1) / 2) * 50 + (((Controller.row - 1) / 2) + 1) * 5 - 25);
}
/**
* The method which updates the game state.
*/
public
void
update
()
{
if
(
rotateAmountTank
>=
360
||
rotateAmountTank
<=
-
360
)
rotateAmountTank
=
0
;
if
(
mousePress
)
{
locY
=
mouseY
-
diam
/
2
;
locX
=
mouseX
-
diam
/
2
;
}
if
(
keyUP
)
{
calculateBullet
(
"x"
);
calculateBullet
(
"y"
);
PermissionUp
=
true
;
for
(
Wall
wall
:
Controller
.
walls
)
{
if
(
wall
.
getWidth
()
==
5
&&
wall
.
getHeight
()
==
50
)
{
if
((
getBounds
(
locX
,
locY
).
intersects
(
new
Rectangle
((
int
)
wall
.
getX
(),
(
int
)
wall
.
getY
(),
5
,
50
))))
{
//&&
// ((rotateAmount == 0 && locX < wall.getX()) || ((rotateAmount == 180 || rotateAmount == -180) && locX > wall.getX()))
PermissionUp
=
false
;
break
;
}
}
if
(
wall
.
getWidth
()
==
50
&&
wall
.
getHeight
()
==
5
)
{
if
((
getBounds
(
locX
,
locY
).
intersects
(
new
Rectangle
((
int
)
wall
.
getX
(),
(
int
)
wall
.
getY
(),
50
,
5
))))
{
// &&
// (((rotateAmount == +90 || rotateAmount == -270) && locY < wall.getY()) || (((rotateAmount == -90 || rotateAmount == -270) && locY > wall.getY())))
PermissionUp
=
false
;
break
;
}
}
}
if
(
PermissionUp
)
move
(+
5
);
for
(
Wall
wall
:
Controller
.
walls
)
if
(!
PermissionDown
&&
(!(
getBounds
(
locX
,
locY
).
intersects
(
new
Rectangle
((
int
)
wall
.
getX
(),
(
int
)
wall
.
getY
(),
50
,
5
)))
||
!(
getBounds
(
locX
,
locY
).
intersects
(
new
Rectangle
((
int
)
wall
.
getX
(),
(
int
)
wall
.
getY
(),
5
,
50
)))))
{
move
(+
5
);
break
;
}
}
if
(
keyDOWN
)
{
calculateBullet
(
"x"
);
calculateBullet
(
"y"
);
PermissionDown
=
true
;
for
(
Wall
wall
:
Controller
.
walls
)
{
if
(
wall
.
getWidth
()
==
5
&&
wall
.
getHeight
()
==
50
)
{
if
((
getBounds
(
locX
,
locY
).
intersects
(
new
Rectangle
((
int
)
wall
.
getX
(),
(
int
)
wall
.
getY
(),
5
,
50
))))
{
//&&
// ((((rotateAmount == 0) || locX >= wall.getX())) || (((rotateAmount == -180 || rotateAmount == 180) && locX <= wall.getX())))
PermissionDown
=
false
;
break
;
}
}
if
(
wall
.
getWidth
()
==
50
&&
wall
.
getHeight
()
==
5
)
{
if
((
getBounds
(
locX
,
locY
).
intersects
(
new
Rectangle
((
int
)
wall
.
getX
(),
(
int
)
wall
.
getY
(),
50
,
5
))))
{
// &&
// (((rotateAmount == -90 || rotateAmount == 270) && locY<wall.getY()))|| (((rotateAmount==90||rotateAmount==-270)&& locY>wall.getY()))
PermissionDown
=
false
;
break
;
}
}
}
if
(
PermissionDown
)
move
(-
5
);
for
(
Wall
wall
:
Controller
.
walls
)
if
(!
PermissionUp
&&
(!(
getBounds
(
locX
,
locY
).
intersects
(
new
Rectangle
((
int
)
wall
.
getX
(),
(
int
)
wall
.
getY
(),
50
,
5
)))
||
!(
getBounds
(
locX
,
locY
).
intersects
(
new
Rectangle
((
int
)
wall
.
getX
(),
(
int
)
wall
.
getY
(),
5
,
50
)))))
{
move
(-
5
);
break
;
}
}
for
(
Prize
prize
:
Controller
.
prizes
)
{
if
((
getBounds
(
locX
,
locY
).
intersects
(
new
Rectangle
((
int
)
prize
.
getX
(),
(
int
)
prize
.
getY
(),
prize
.
getWidth
(),
prize
.
getHeight
()))))
{
Controller
.
getPrize
=
true
;
Controller
.
tanks
.
get
(
0
).
setPrize
(
prize
.
getName
());
}
}
if
(
keyLEFT
)
rotateAmountTank
-=
15
;
if
(
keyRIGHT
)
rotateAmountTank
+=
15
;
locX
=
Math
.
max
(
locX
,
40
);
locX
=
Math
.
min
(
locX
,
20
+
(((
Controller
.
col
-
1
)
/
2
)
*
50
)
+
(((
Controller
.
col
-
1
)
/
2
)
+
1
)
*
5
-
25
);
locY
=
Math
.
max
(
locY
,
70
);
locY
=
Math
.
min
(
locY
,
50
+
((
Controller
.
row
-
1
)
/
2
)
*
50
+
(((
Controller
.
row
-
1
)
/
2
)
+
1
)
*
5
-
25
);
}
public
void
fire
(
Graphics2D
g2d
)
{
if
(
keyM
&&
Controller
.
tanks
.
size
()
>
0
)
{
if
(
rotateAmountBullet
>=
360
||
rotateAmountBullet
<=
-
360
)
rotateAmountBullet
=
0
;
Bullet
bullet
=
new
Bullet
(
calculateBullet
(
"x"
),
calculateBullet
(
"y"
),
rotateAmountBullet
,
30
);
bullets
.
add
(
bullet
);
bulletCollision
(
bullets
.
get
(
0
));
shotBullet
(
8
,
bullets
.
get
(
0
));
g2d
.
fillOval
(
bullets
.
get
(
0
).
x
,
bullets
.
get
(
0
).
y
,
7
,
7
);
if
(
bullets
.
size
()
>
5
&&
Controller
.
tanks
.
size
()
>
0
)
checkTankDestroyed
(
Controller
.
tanks
.
get
(
0
),
bullets
.
get
(
0
));
if
(
bulletTime
>
60
)
{
bullets
.
get
(
0
).
x
=
calculateBullet
(
"x"
);
bullets
.
get
(
0
).
y
=
calculateBullet
(
"y"
);
bullets
.
get
(
0
).
rotateAmountBullet
=
rotateAmountTank
;
bullets
.
clear
();
keyM
=
false
;
bulletTime
=
0
;
}
bulletTime
++;
}
}
public
int
calculateBullet
(
String
xy
)
{
int
x
=
locX
;
int
y
=
locY
;
...
...
@@ -309,64 +454,62 @@ public class GameState {
}
return
0
;
}
public
void
shotBullet
(
int
px
,
Bullet
bullet
)
{
public
void
move
(
int
px
)
{
double
d
;
double
r
=
rotateAmountBullet
;
if
(!
isMoveAllowed
(
px
))
px
=
0
;
if
(
r
==
0
)
bullet
.
x
+=
px
;
if
(
r
==
90
||
r
==
-
270
)
bullet
.
y
+=
px
;
if
(
r
==
-
90
||
r
==
270
)
bullet
.
y
-=
px
;
if
(
r
==
180
||
r
==
-
180
)
bullet
.
x
-=
px
;
if
(
r
otateAmountTank
==
0
)
locX
+=
px
;
if
(
r
otateAmountTank
==
90
||
rotateAmountTank
==
-
270
)
locY
+=
px
;
if
(
r
otateAmountTank
==
-
90
||
rotateAmountTank
==
270
)
locY
-=
px
;
if
(
r
otateAmountTank
==
180
||
rotateAmountTank
==
-
180
)
locX
-=
px
;
if
((
r
>
0
&&
r
<
90
)
||
(
r
<
-
270
&&
r
>
-
360
))
{
if
((
r
otateAmountTank
>
0
&&
rotateAmountTank
<
90
)
||
(
rotateAmountTank
<
-
270
&&
rotateAmountTank
>
-
360
))
{
if
(
px
>
0
)
{
bullet
.
x
+=
px
*
Math
.
cos
(
r
*
Math
.
PI
/
180
);
bullet
.
y
+=
px
*
Math
.
sin
(
r
*
Math
.
PI
/
180
);
locX
+=
px
*
Math
.
cos
(
rotateAmountTank
*
Math
.
PI
/
180
);
locY
+=
px
*
Math
.
sin
(
rotateAmountTank
*
Math
.
PI
/
180
);
}
else
{
bullet
.
x
+=
px
*
Math
.
cos
(
r
*
Math
.
PI
/
180
);
bullet
.
y
+=
px
*
Math
.
sin
(
r
*
Math
.
PI
/
180
);
locX
+=
px
*
Math
.
cos
(
rotateAmountTank
*
Math
.
PI
/
180
);
locY
+=
px
*
Math
.
sin
(
rotateAmountTank
*
Math
.
PI
/
180
);
}
}
if
((
r
>
90
&&
r
<
180
)
||
(
r
<
-
180
&&
r
>
-
270
))
{
if
((
rotateAmountTank
>
90
&&
rotateAmountTank
<
180
)
||
(
rotateAmountTank
<
-
180
&&
rotateAmountTank
>
-
270
))
{
if
(
px
>
0
)
{
d
=
r
-
90
;
bullet
.
x
-=
px
*
Math
.
sin
(
d
*
Math
.
PI
/
180
);
bullet
.
y
+=
px
*
Math
.
cos
(
d
*
Math
.
PI
/
180
);
d
=
r
otateAmountTank
-
90
;
locX
-=
px
*
Math
.
sin
(
d
*
Math
.
PI
/
180
);
locY
+=
px
*
Math
.
cos
(
d
*
Math
.
PI
/
180
);
}
else
{
d
=
r
+
270
;
bullet
.
x
-=
px
*
Math
.
sin
(
d
*
Math
.
PI
/
180
);
bullet
.
y
+=
px
*
Math
.
cos
(
d
*
Math
.
PI
/
180
);
d
=
r
otateAmountTank
+
270
;
locX
-=
px
*
Math
.
sin
(
d
*
Math
.
PI
/
180
);
locY
+=
px
*
Math
.
cos
(
d
*
Math
.
PI
/
180
);
}
}
if
((
r
>
180
&&
r
<
270
)
||
(
r
<
-
90
&&
r
>
-
180
))
{
if
((
rotateAmountTank
>
180
&&
rotateAmountTank
<
270
)
||
(
rotateAmountTank
<
-
90
&&
rotateAmountTank
>
-
180
))
{
if
(
px
>
0
)
{
d
=
r
-
180
;
bullet
.
x
-=
px
*
Math
.
cos
(
d
*
Math
.
PI
/
180
);
bullet
.
y
-=
px
*
Math
.
sin
(
d
*
Math
.
PI
/
180
);
d
=
r
otateAmountTank
-
180
;
locX
-=
px
*
Math
.
cos
(
d
*
Math
.
PI
/
180
);
locY
-=
px
*
Math
.
sin
(
d
*
Math
.
PI
/
180
);
}
else
{
d
=
r
+
180
;
bullet
.
x
-=
px
*
Math
.
cos
(
d
*
Math
.
PI
/
180
);
bullet
.
y
-=
px
*
Math
.
sin
(
d
*
Math
.
PI
/
180
);
d
=
r
otateAmountTank
+
180
;
locX
-=
px
*
Math
.
cos
(
d
*
Math
.
PI
/
180
);
locY
-=
px
*
Math
.
sin
(
d
*
Math
.
PI
/
180
);
}
}
if
((
r
>
270
&&
r
<
360
)
||
(
r
<
0
&&
r
>
-
90
))
{
if
((
rotateAmountTank
>
270
&&
rotateAmountTank
<
360
)
||
(
rotateAmountTank
<
0
&&
rotateAmountTank
>
-
90
))
{
if
(
px
>
0
)
{
d
=
r
-
270
;
bullet
.
x
+=
px
*
Math
.
sin
(
d
*
Math
.
PI
/
180
);
bullet
.
y
-=
px
*
Math
.
cos
(
d
*
Math
.
PI
/
180
);
d
=
r
otateAmountTank
-
270
;
locX
+=
px
*
Math
.
sin
(
d
*
Math
.
PI
/
180
);
locY
-=
px
*
Math
.
cos
(
d
*
Math
.
PI
/
180
);
}
else
{
d
=
r
+
90
;
bullet
.
x
+=
px
*
Math
.
sin
(
d
*
Math
.
PI
/
180
);
bullet
.
y
-=
px
*
Math
.
cos
(
d
*
Math
.
PI
/
180
);
d
=
r
otateAmountTank
+
90
;
locX
+=
px
*
Math
.
sin
(
d
*
Math
.
PI
/
180
);
locY
-=
px
*
Math
.
cos
(
d
*
Math
.
PI
/
180
);
}
}
}
public
void
bulletCollision
(
Bullet
bullet
)
{
for
(
Wall
wall
:
Controller
.
walls
)
{
Rectangle
rectangle
=
new
Rectangle
(
wall
.
getX
(),
wall
.
getY
(),
wall
.
getWidth
(),
wall
.
getHeight
());
...
...
@@ -396,6 +539,7 @@ public class GameState {
}
}
}
public
void
checkTankDestroyed
(
Tank
tankToCheck
,
Bullet
bullet
)
{
Rectangle
rectangle
=
new
Rectangle
(
tankToCheck
.
getX
(),
tankToCheck
.
getY
(),
25
,
25
);
Ellipse2D
ellipse2D
=
new
Ellipse2D
.
Double
(
bullet
.
x
,
bullet
.
y
,
7
,
7
);
...
...
@@ -411,35 +555,64 @@ public class GameState {
}
}
public
void
fire
(
Graphics2D
g2d
)
{
if
(
keyM
&&
Controller
.
tanks
.
size
()
>
0
)
{
if
(
rotateAmountBullet
>=
360
||
rotateAmountBullet
<=
-
360
)
rotateAmountBullet
=
0
;
public
void
shotBullet
(
int
px
,
Bullet
bullet
)
{
Bullet
bullet
=
new
Bullet
(
calculateBullet
(
"x"
),
calculateBullet
(
"y"
),
rotateAmountBullet
,
30
);
bullets
.
add
(
bullet
);
bulletCollision
(
bullets
.
get
(
0
));
double
d
;
double
r
=
rotateAmountBullet
;
shotBullet
(
8
,
bullets
.
get
(
0
));
g2d
.
fillOval
(
bullets
.
get
(
0
).
x
,
bullets
.
get
(
0
).
y
,
7
,
7
);
if
(
r
==
0
)
bullet
.
x
+=
px
;
if
(
r
==
90
||
r
==
-
270
)
bullet
.
y
+=
px
;
if
(
r
==
-
90
||
r
==
270
)
bullet
.
y
-=
px
;
if
(
r
==
180
||
r
==
-
180
)
bullet
.
x
-=
px
;
if
(
bullets
.
size
()
>
5
&&
Controller
.
tanks
.
size
()
>
0
)
checkTankDestroyed
(
Controller
.
tanks
.
get
(
0
),
bullets
.
get
(
0
));
if
((
r
>
0
&&
r
<
90
)
||
(
r
<
-
270
&&
r
>
-
360
))
{
if
(
px
>
0
)
{
bullet
.
x
+=
px
*
Math
.
cos
(
r
*
Math
.
PI
/
180
);
bullet
.
y
+=
px
*
Math
.
sin
(
r
*
Math
.
PI
/
180
);
}
else
{
bullet
.
x
+=
px
*
Math
.
cos
(
r
*
Math
.
PI
/
180
);
bullet
.
y
+=
px
*
Math
.
sin
(
r
*
Math
.
PI
/
180
);
}
}
if
(
bulletTime
>
60
)
{
if
((
r
>
90
&&
r
<
180
)
||
(
r
<
-
180
&&
r
>
-
270
))
{
if
(
px
>
0
)
{
d
=
r
-
90
;
bullet
.
x
-=
px
*
Math
.
sin
(
d
*
Math
.
PI
/
180
);
bullet
.
y
+=
px
*
Math
.
cos
(
d
*
Math
.
PI
/
180
);
}
else
{
d
=
r
+
270
;
bullet
.
x
-=
px
*
Math
.
sin
(
d
*
Math
.
PI
/
180
);
bullet
.
y
+=
px
*
Math
.
cos
(
d
*
Math
.
PI
/
180
);
}
}
bullets
.
get
(
0
).
x
=
calculateBullet
(
"x"
);
bullets
.
get
(
0
).
y
=
calculateBullet
(
"y"
);
bullets
.
get
(
0
).
rotateAmountBullet
=
rotateAmountTank
;
bullets
.
clear
();
keyM
=
false
;
bulletTime
=
0
;
if
((
r
>
180
&&
r
<
270
)
||
(
r
<
-
90
&&
r
>
-
180
))
{
if
(
px
>
0
)
{
d
=
r
-
180
;
bullet
.
x
-=
px
*
Math
.
cos
(
d
*
Math
.
PI
/
180
);
bullet
.
y
-=
px
*
Math
.
sin
(
d
*
Math
.
PI
/
180
);
}
else
{
d
=
r
+
180
;
bullet
.
x
-=
px
*
Math
.
cos
(
d
*
Math
.
PI
/
180
);
bullet
.
y
-=
px
*
Math
.
sin
(
d
*
Math
.
PI
/
180
);
}
bulletTime
++;
}
if
((
r
>
270
&&
r
<
360
)
||
(
r
<
0
&&
r
>
-
90
))
{
if
(
px
>
0
)
{
d
=
r
-
270
;
bullet
.
x
+=
px
*
Math
.
sin
(
d
*
Math
.
PI
/
180
);
bullet
.
y
-=
px
*
Math
.
cos
(
d
*
Math
.
PI
/
180
);
}
else
{
d
=
r
+
90
;
bullet
.
x
+=
px
*
Math
.
sin
(
d
*
Math
.
PI
/
180
);
bullet
.
y
-=
px
*
Math
.
cos
(
d
*
Math
.
PI
/
180
);
}
}
}
public
KeyListener
getKeyListener
()
{
...
...
@@ -479,6 +652,11 @@ public class GameState {
case
KeyEvent
.
VK_ESCAPE
:
gameOver
=
true
;
break
;
case
KeyEvent
.
VK_M
:
rotateAmountBullet
=
rotateAmountTank
;
new
AudioPlayer
(
"sound effects/cannon.wav"
,
0
);
keyM
=
true
;
break
;
}
}
if
(
two
)
{
...
...
@@ -519,6 +697,8 @@ public class GameState {
break
;
}
}
}
@Override
...
...
@@ -537,6 +717,8 @@ public class GameState {
case
KeyEvent
.
VK_RIGHT
:
keyRIGHT
=
false
;
break
;
case
KeyEvent
.
VK_M
:
break
;
}
}
if
(
two
)
{
...
...
@@ -572,7 +754,6 @@ public class GameState {
}
}
}
}
/**
...
...
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