Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
project1_new
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
jiaxi liu
project1_new
Commits
0c443629
Commit
0c443629
authored
2 years ago
by
jiaxi liu
Browse files
Options
Downloads
Patches
Plain Diff
1. Complete the wall blocking function.
2. Add player image direction.
parent
180c99c6
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/Player.java
+34
-15
34 additions, 15 deletions
src/Player.java
src/ShadowDimension.java
+36
-18
36 additions, 18 deletions
src/ShadowDimension.java
src/Sinkhole.java
+4
-1
4 additions, 1 deletion
src/Sinkhole.java
src/Wall.java
+21
-2
21 additions, 2 deletions
src/Wall.java
with
95 additions
and
36 deletions
src/Player.java
+
34
−
15
View file @
0c443629
...
...
@@ -2,6 +2,7 @@ import bagel.DrawOptions;
import
bagel.Font
;
import
bagel.Image
;
import
bagel.util.Point
;
import
bagel.util.Rectangle
;
/**
* Player Code for SWEN20003 Project 1, Semester 2, 2022
...
...
@@ -16,7 +17,9 @@ import bagel.util.Point;
public
class
Player
{
private
final
static
Image
FaceLeftImage
=
new
Image
(
"res/faeLeft.png"
);
private
final
static
Image
FaceRightImage
=
new
Image
(
"res/faeRight.png"
);
private
static
final
int
Life
=
100
;
private
static
final
int
LIFE_MAX
=
100
;
private
static
int
Life
=
LIFE_MAX
;
private
static
Image
Img
;
private
final
Font
LifeFont
=
new
Font
(
"res/frostbite.ttf"
,
30
);
private
final
DrawOptions
LifeHighColor
=
new
DrawOptions
();
...
...
@@ -40,10 +43,32 @@ public class Player {
LifeLowColor
.
setBlendColour
(
1
,
0
,
0
);
}
public
void
Clear
()
{
X
=
0
;
Y
=
0
;
WORLD_TOP
=
0
;
WORLD_LEFT
=
0
;
WORLD_BOTTOM
=
0
;
WORLD_RIGHT
=
0
;
Life
=
LIFE_MAX
;
}
public
Point
GetPos
()
{
/*Get the position of player*/
return
new
Point
(
X
,
Y
);
}
public
void
SetPos
(
Point
p
)
{
/*Set the position of player*/
X
=
(
int
)
p
.
x
;
Y
=
(
int
)
p
.
y
;
}
public
Rectangle
GetRectangle
()
{
/*Get Rectangle of player*/
return
Img
.
getBoundingBoxAt
(
GetPos
());
}
public
void
MoveUp
()
{
/*Player moves up in world, step = 2*/
if
(
Y
>
(
WORLD_TOP
+
2
))
{
...
...
@@ -62,6 +87,7 @@ public class Player {
/*Player moves left in world, step = 2*/
if
(
X
>
(
WORLD_LEFT
+
2
))
{
X
-=
2
;
Img
=
FaceLeftImage
;
}
}
...
...
@@ -69,6 +95,7 @@ public class Player {
/*Player moves right in world, step = 2*/
if
(
X
<
(
WORLD_RIGHT
-
2
))
{
X
+=
2
;
Img
=
FaceRightImage
;
}
}
...
...
@@ -76,23 +103,15 @@ public class Player {
/*Show play image*/
Img
.
drawFromTopLeft
(
X
,
Y
);
int
life_percent
=
Life
*
100
/
LIFE_MAX
;
/*Show life text*/
if
(
L
ife
>=
65
)
{
LifeFont
.
drawString
(
L
ife
+
"%"
,
20
,
25
,
LifeHighColor
);
}
else
if
(
L
ife
>=
35
)
{
LifeFont
.
drawString
(
L
ife
+
"%"
,
20
,
25
,
LifeMidColor
);
if
(
l
ife
_percent
>=
65
)
{
LifeFont
.
drawString
(
l
ife
_percent
+
"%"
,
20
,
25
,
LifeHighColor
);
}
else
if
(
l
ife
_percent
>=
35
)
{
LifeFont
.
drawString
(
l
ife
_percent
+
"%"
,
20
,
25
,
LifeMidColor
);
}
else
{
LifeFont
.
drawString
(
L
ife
+
"%"
,
20
,
25
,
LifeLowColor
);
LifeFont
.
drawString
(
l
ife
_percent
+
"%"
,
20
,
25
,
LifeLowColor
);
}
}
public
void
ToLeft
()
{
/*Changing user orientation to left*/
Img
=
FaceLeftImage
;
}
public
void
ToRight
()
{
/*Changing user orientation to right*/
Img
=
FaceRightImage
;
}
}
This diff is collapsed.
Click to expand it.
src/ShadowDimension.java
+
36
−
18
View file @
0c443629
import
bagel.*
;
import
bagel.util.Colour
;
import
bagel.util.Point
;
import
java.io.IOException
;
import
java.nio.file.Files
;
...
...
@@ -82,10 +83,24 @@ public class ShadowDimension extends AbstractGame {
player
.
WORLD_RIGHT
=
Integer
.
parseInt
(
line
[
1
]);
player
.
WORLD_BOTTOM
=
Integer
.
parseInt
(
line
[
2
]);
}
//System.out.println(csv[i]);
}
}
private
void
ImgRefresh
()
{
/*Draw the world, walls, sinkholes, players.*/
BACKGROUND_IMAGE
.
draw
(
Window
.
getWidth
()
/
2.0
,
Window
.
getHeight
()
/
2.0
);
wall
.
Update
();
hole
.
Update
();
player
.
Update
();
}
private
void
Init
()
{
/*Init for new turn.*/
player
.
Clear
();
wall
.
Clear
();
}
/**
* Performs a state update.
* allows the game to exit when the escape key is pressed.
...
...
@@ -94,12 +109,15 @@ public class ShadowDimension extends AbstractGame {
protected
void
update
(
Input
input
)
{
/*State for game
* 0:Init and show welcom page
* 1:Show Play page
* 1:First show play page
* 2:Play
* */
switch
(
game_state
)
{
case
0
:
{
/*Draw Text*/
Drawing
.
drawRectangle
(
0
,
0
,
1024
,
768
,
new
Colour
(
103.0
/
256
,
153.0
/
256
,
231.0
/
256
));
Init
();
Drawing
.
drawRectangle
(
0
,
0
,
1024
,
768
,
new
Colour
(
103.0
/
256
,
153.0
/
256
,
231.0
/
256
));
title
.
drawString
(
"SHADOW DIMENSION"
,
260
,
250
);
hint
.
drawString
(
"PRESS SPACE TO START\nUSE ARROW KEYS TO FIND GATE"
,
(
Window
.
getWidth
()
-
hint
.
getWidth
(
"USE ARROW KEYS TO FIND GATE"
)
+
90
)
/
2
,
...
...
@@ -116,6 +134,15 @@ public class ShadowDimension extends AbstractGame {
}
case
1
:
{
ImgRefresh
();
game_state
=
2
;
break
;
}
case
2
:
{
/*After the player hits the wall,
he should return to the original position and copy the player's current position.*/
Point
old_p
=
player
.
GetPos
();
/*Get key input and move player*/
if
(
input
.
isDown
(
Keys
.
UP
))
{
player
.
MoveUp
();
...
...
@@ -127,22 +154,13 @@ public class ShadowDimension extends AbstractGame {
player
.
MoveLeft
();
}
/*Show world image*/
BACKGROUND_IMAGE
.
draw
(
Window
.
getWidth
()
/
2.0
,
Window
.
getHeight
()
/
2.0
);
/*Show wall image*/
wall
.
Update
();
/*Show sinkhole image*/
hole
.
Update
();
/*Show player image*/
player
.
Update
();
break
;
/*Restore the original position after hitting the wall.*/
if
(
wall
.
InWallCheck
(
player
.
GetRectangle
()))
{
player
.
SetPos
(
old_p
);
}
default
:
{
/*Show image*/
ImgRefresh
();
break
;
}
}
...
...
This diff is collapsed.
Click to expand it.
src/Sinkhole.java
+
4
−
1
View file @
0c443629
import
bagel.Image
;
import
bagel.util.Rectangle
;
import
java.util.ArrayList
;
...
...
@@ -19,14 +20,16 @@ public class Sinkhole {
public
void
Sinkhole
()
{
}
public
void
Update
()
{
/*Gets all the hole position of the ArrayList
for
draw
ing
*/
/*Gets all the hole position of the ArrayList
and
draw*/
for
(
int
i
=
0
;
i
<
Pos
.
size
();
i
++)
{
bagel
.
util
.
Point
p
=
(
bagel
.
util
.
Point
)
Pos
.
get
(
i
);
Img
.
drawFromTopLeft
(
p
.
x
,
p
.
y
);
}
}
public
void
Add
(
int
x
,
int
y
)
{
/*Add the hole position to arraylist*/
Pos
.
add
(
new
bagel
.
util
.
Point
(
x
,
y
));
...
...
This diff is collapsed.
Click to expand it.
src/Wall.java
+
21
−
2
View file @
0c443629
import
bagel.Image
;
import
bagel.util.Rectangle
;
import
java.util.ArrayList
;
...
...
@@ -14,20 +15,38 @@ import java.util.ArrayList;
public
class
Wall
{
private
final
Image
Img
=
new
Image
(
"res/wall.png"
);
private
final
ArrayList
Pos
=
new
ArrayList
();
private
final
ArrayList
rect
=
new
ArrayList
();
public
void
Wall
()
{
public
void
Clear
()
{
/*Clear all position and Rectangle list*/
Pos
.
clear
();
rect
.
clear
();
}
public
void
Update
()
{
/*Gets all the wall locations of the ArrayList for drawing*/
rect
.
clear
();
for
(
int
i
=
0
;
i
<
Pos
.
size
();
i
++)
{
bagel
.
util
.
Point
p
=
(
bagel
.
util
.
Point
)
Pos
.
get
(
i
);
Img
.
drawFromTopLeft
(
p
.
x
,
p
.
y
);
/*Record all wall Rectangle for overlaps checking.*/
rect
.
add
(
Img
.
getBoundingBoxAt
(
p
));
}
}
public
void
Add
(
int
x
,
int
y
)
{
public
boolean
InWallCheck
(
Rectangle
r
)
{
/*Check that each wall overlaps the r(player) position*/
for
(
int
i
=
0
;
i
<
Pos
.
size
();
i
++)
{
if
(
r
.
intersects
((
Rectangle
)
rect
.
get
(
i
)))
{
return
true
;
}
}
return
false
;
}
public
void
Add
(
int
x
,
int
y
)
{
/*Add the wall position to arraylist*/
Pos
.
add
(
new
bagel
.
util
.
Point
(
x
,
y
));
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment