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
d53304e0
Commit
d53304e0
authored
Aug 02, 2020
by
9831111
🙂
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add calculate bullet method to find first shot point.
parent
660d635a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
152 additions
and
84 deletions
+152
-84
GameFrame.java
src/GameFrame.java
+0
-14
GameState.java
src/GameState.java
+152
-70
No files found.
src/GameFrame.java
View file @
d53304e0
...
@@ -338,21 +338,7 @@ public class GameFrame extends JFrame {
...
@@ -338,21 +338,7 @@ public class GameFrame extends JFrame {
}
}
}
}
public
boolean
checkWallAdded
(
int
currentX
,
int
currentY
)
{
String
str
=
currentX
+
" "
+
currentY
;
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
));
}
/**
/**
* Draw prize randomly
* Draw prize randomly
...
...
src/GameState.java
View file @
d53304e0
...
@@ -33,9 +33,9 @@ public class GameState {
...
@@ -33,9 +33,9 @@ public class GameState {
private
boolean
two
=
false
;
private
boolean
two
=
false
;
private
boolean
three
=
false
;
private
boolean
three
=
false
;
private
int
step
=
0
;
private
int
step
=
0
;
boolean
PermissionUp
=
true
;
public
double
rotateAmountTank
;
boolean
PermissionDown
=
true
;
public
double
rotateAmountBullet
;
boolean
check
=
true
;
private
int
bulletTime
=
0
;
public
GameState
(
int
num
)
{
public
GameState
(
int
num
)
{
...
@@ -84,71 +84,14 @@ public class GameState {
...
@@ -84,71 +84,14 @@ public class GameState {
if
(
keyUP
)
{
if
(
keyUP
)
{
PermissionUp
=
true
;
move
(+
5
);
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
)
{
if
(
keyDOWN
)
{
PermissionDown
=
true
;
move
(-
5
);
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
)
if
(
keyLEFT
)
rotateAmount
-=
15
;
rotateAmount
-=
15
;
...
@@ -162,9 +105,93 @@ public class GameState {
...
@@ -162,9 +105,93 @@ public class GameState {
locY
=
Math
.
min
(
locY
,
50
+
((
Controller
.
row
-
1
)
/
2
)
*
50
+
(((
Controller
.
row
-
1
)
/
2
)
+
1
)
*
5
-
25
);
locY
=
Math
.
min
(
locY
,
50
+
((
Controller
.
row
-
1
)
/
2
)
*
50
+
(((
Controller
.
row
-
1
)
/
2
)
+
1
)
*
5
-
25
);
}
}
public
Wall
verticalWallCollision
()
{
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
))))
{
return
wall
;
}
}
}
return
null
;
}
public
Wall
horizontalWallCollision
()
{
for
(
Wall
wall
:
Controller
.
walls
)
{
if
(
wall
.
getWidth
()
==
50
&&
wall
.
getHeight
()
==
5
)
{
if
((
getBounds
(
locX
,
locY
).
intersects
(
new
Rectangle
((
int
)
wall
.
getX
(),
(
int
)
wall
.
getY
(),
50
,
5
))))
{
return
wall
;
}
}
}
return
null
;
}
public
boolean
isMoveAllowed
(
int
px
)
{
if
(
verticalWallCollision
()
!=
null
)
{
if
(
verticalWallCollision
().
getX
()
>
locX
&&
px
>
0
)
{
if
((
rotateAmount
<=
90
&&
rotateAmount
>=
-
90
)
||
(
rotateAmount
<=
-
270
&&
rotateAmount
>
-
360
)
||
(
rotateAmount
>=
270
&&
rotateAmount
<
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
))
{
return
false
;
}
else
return
true
;
}
if
(
verticalWallCollision
().
getX
()
>
locX
&&
px
<
0
)
{
if
((
rotateAmount
>=
90
&&
rotateAmount
<=
270
)
||
(
rotateAmount
<=
-
90
&&
rotateAmount
>=
-
270
))
{
return
false
;
}
else
return
true
;
}
if
(
verticalWallCollision
().
getX
()
<
locX
&&
px
>
0
)
{
if
((
rotateAmount
>=
90
||
rotateAmount
<=
270
)
||
(
rotateAmount
<=
-
90
&&
rotateAmount
>=
-
270
))
{
return
false
;
}
else
return
true
;
}
}
if
(
horizontalWallCollision
()
!=
null
)
{
if
(
horizontalWallCollision
().
getY
()
>
locY
&&
px
>
0
)
{
if
((
rotateAmount
<=
180
&&
rotateAmount
>=
0
)
||
(
rotateAmount
<=
-
180
&&
rotateAmount
>
-
360
))
{
System
.
out
.
println
(
"1"
);
return
false
;
}
else
return
true
;
}
if
(
horizontalWallCollision
().
getY
()
<
locY
&&
px
<
0
)
{
if
((
rotateAmount
<=
180
&&
rotateAmount
>=
0
)
||
(
rotateAmount
<=
-
180
&&
rotateAmount
>
-
360
))
{
System
.
out
.
println
(
"2"
);
return
false
;
}
else
return
true
;
}
if
(
horizontalWallCollision
().
getY
()
<
locY
&&
px
>
0
)
{
if
((
rotateAmount
>=
-
180
&&
rotateAmount
<=
0
)
||
(
rotateAmount
>=
180
&&
rotateAmount
<
360
))
{
System
.
out
.
println
(
"3"
);
return
false
;
}
else
return
true
;
}
if
(
horizontalWallCollision
().
getY
()
>
locY
&&
px
<
0
)
{
if
((
rotateAmount
>=
-
180
&&
rotateAmount
<=
0
)
||
(
rotateAmount
>=
180
&&
rotateAmount
<
360
))
{
System
.
out
.
println
(
"4"
);
return
false
;
}
else
return
true
;
}
}
return
true
;
}
public
void
move
(
int
px
)
{
public
void
move
(
int
px
)
{
double
d
;
double
d
;
if
(!
isMoveAllowed
(
px
))
px
=
0
;
if
(
rotateAmount
==
0
)
locX
+=
px
;
if
(
rotateAmount
==
0
)
locX
+=
px
;
if
(
rotateAmount
==
90
||
rotateAmount
==
-
270
)
locY
+=
px
;
if
(
rotateAmount
==
90
||
rotateAmount
==
-
270
)
locY
+=
px
;
if
(
rotateAmount
==
-
90
||
rotateAmount
==
270
)
locY
-=
px
;
if
(
rotateAmount
==
-
90
||
rotateAmount
==
270
)
locY
-=
px
;
...
@@ -217,17 +244,72 @@ public class GameState {
...
@@ -217,17 +244,72 @@ public class GameState {
}
}
}
}
public
int
calculateBullet
(
String
xy
)
{
public
void
checkWall
()
{
int
x
=
locX
;
for
(
Wall
wall
:
Controller
.
walls
)
{
int
y
=
locY
;
if
(
wall
.
getX
()
==
locX
+
5
||
wall
.
getY
()
==
locY
+
5
)
{
if
(
rotateAmountTank
==
0
)
{
keyDOWN
=
false
;
if
(
xy
.
equals
(
"x"
))
{
}
else
if
(
wall
.
getX
()
==
locX
-
5
||
wall
.
getY
()
==
locY
-
5
)
{
x
+=
20
;
keyUP
=
false
;
return
x
;
}
else
{
y
+=
10
;
return
y
;
}
}
}
}
if
(
rotateAmountTank
==
90
||
rotateAmountTank
==
-
270
)
{
if
(
xy
.
equals
(
"x"
))
{
x
+=
9
;
return
x
;
}
else
{
return
y
+
20
;
}
}
if
(
rotateAmountTank
==
-
90
||
rotateAmountTank
==
270
)
{
if
(
xy
.
equals
(
"x"
))
{
x
+=
9
;
return
x
;
}
else
{
return
y
-
20
;
}
}
if
(
rotateAmountTank
==
180
||
rotateAmountTank
==
-
180
)
{
if
(
xy
.
equals
(
"x"
))
{
return
x
;
}
else
{
y
+=
10
;
return
y
;
}
}
if
((
rotateAmountTank
>
0
&&
rotateAmountTank
<
90
)
||
(
rotateAmountTank
<
-
270
&&
rotateAmountTank
>
-
360
))
{
x
+=
10
*
Math
.
sin
(
rotateAmountTank
*
Math
.
PI
/
180
);
y
+=
10
*
Math
.
cos
(
rotateAmountTank
*
Math
.
PI
/
180
);
if
(
xy
.
equals
(
"x"
))
return
x
;
else
return
y
;
}
if
((
rotateAmountTank
>
180
&&
rotateAmountTank
<
270
)
||
(
rotateAmountTank
<
-
90
&&
rotateAmountTank
>
-
180
))
{
x
-=
10
*
Math
.
sin
(
rotateAmountTank
*
Math
.
PI
/
180
);
y
-=
10
*
Math
.
cos
(
rotateAmountTank
*
Math
.
PI
/
180
);
if
(
xy
.
equals
(
"x"
))
return
x
;
else
return
y
;
}
if
((
rotateAmountTank
>
90
&&
rotateAmountTank
<
180
)
||
(
rotateAmountTank
<
-
180
&&
rotateAmountTank
>
-
270
))
{
x
+=
25
*
Math
.
sin
(
rotateAmountTank
*
Math
.
PI
/
180
);
y
-=
20
*
Math
.
cos
(
rotateAmountTank
*
Math
.
PI
/
180
);
if
(
xy
.
equals
(
"x"
))
return
x
;
else
return
y
;
}
if
((
rotateAmountTank
>
270
&&
rotateAmountTank
<
360
)
||
(
rotateAmountTank
<
0
&&
rotateAmountTank
>
-
90
))
{
x
+=
20
*
Math
.
cos
(
rotateAmountTank
*
Math
.
PI
/
180
);
y
-=
25
*
Math
.
sin
(
rotateAmountTank
*
Math
.
PI
/
180
);
if
(
xy
.
equals
(
"x"
))
return
x
;
else
return
y
;
}
return
0
;
}
}
public
KeyListener
getKeyListener
()
{
public
KeyListener
getKeyListener
()
{
return
keyHandler
;
return
keyHandler
;
}
}
...
...
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