From 180c99c6b09d181b2d0de1ab283705c7801c1b12 Mon Sep 17 00:00:00 2001 From: JIAXI3 <jiaxi3@student.unimelb.edu.au> Date: Sat, 3 Sep 2022 13:24:20 +0800 Subject: [PATCH] 1. Add the wall class, draw and manage code. 2. Add the sinkhole class, draw and manage code. 3. Add code to control player movement within world boundaries. --- src/Player.java | 56 ++++++++++++++++++++++++++++++++++++---- src/ShadowDimension.java | 50 ++++++++++++++++++++++++++++++----- src/Sinkhole.java | 34 ++++++++++++++++++++++++ src/Wall.java | 34 ++++++++++++++++++++++++ 4 files changed, 162 insertions(+), 12 deletions(-) create mode 100644 src/Sinkhole.java create mode 100644 src/Wall.java diff --git a/src/Player.java b/src/Player.java index a5a30a9..4213e76 100644 --- a/src/Player.java +++ b/src/Player.java @@ -1,20 +1,34 @@ import bagel.DrawOptions; import bagel.Font; import bagel.Image; +import bagel.util.Point; +/** + * Player Code for SWEN20003 Project 1, Semester 2, 2022 + * <p> + * Manage world info. + * Manage player life. + * Player move. + * Draw player. + * + * @JIAXI3 + */ public class Player { - public int X = 0; - public int Y = 0; - private final static Image FaceLeftImage = new Image("res/faeLeft.png"); private final static Image FaceRightImage = new Image("res/faeRight.png"); - - private static int Life = 100; + private static final int Life = 100; private static Image Img; private final Font LifeFont = new Font("res/frostbite.ttf", 30); private final DrawOptions LifeHighColor = new DrawOptions(); private final DrawOptions LifeMidColor = new DrawOptions(); private final DrawOptions LifeLowColor = new DrawOptions(); + public int X = 0; + public int Y = 0; + + public int WORLD_TOP = 0; + public int WORLD_LEFT = 0; + public int WORLD_BOTTOM = 0; + public int WORLD_RIGHT = 0; public Player() { /*Init player image*/ @@ -26,6 +40,38 @@ public class Player { LifeLowColor.setBlendColour(1, 0, 0); } + public Point GetPos() { + return new Point(X, Y); + } + + public void MoveUp() { + /*Player moves up in world, step = 2*/ + if (Y > (WORLD_TOP + 2)) { + Y -= 2; + } + } + + public void MoveDown() { + /*Player moves down in world, step = 2*/ + if (Y < (WORLD_BOTTOM - 2)) { + Y += 2; + } + } + + public void MoveLeft() { + /*Player moves left in world, step = 2*/ + if (X > (WORLD_LEFT + 2)) { + X -= 2; + } + } + + public void MoveRight() { + /*Player moves right in world, step = 2*/ + if (X < (WORLD_RIGHT - 2)) { + X += 2; + } + } + public void Update() { /*Show play image*/ Img.drawFromTopLeft(X, Y); diff --git a/src/ShadowDimension.java b/src/ShadowDimension.java index 957bc31..d931a4c 100644 --- a/src/ShadowDimension.java +++ b/src/ShadowDimension.java @@ -8,9 +8,12 @@ import java.nio.file.Paths; /** * Skeleton Code for SWEN20003 Project 1, Semester 2, 2022 * <p> - * Please enter your name below + * Init and show Welcome. + * Load CSV file. + * Get keyboard input and act accordingly. + * Game state manage. * - * @author + * @JIAXI3 */ @@ -18,13 +21,14 @@ public class ShadowDimension extends AbstractGame { private final static int WINDOW_WIDTH = 1024; private final static int WINDOW_HEIGHT = 768; private final static String GAME_TITLE = "SHADOW DIMENSION"; + private static int game_state = 0; private final Image BACKGROUND_IMAGE = new Image("res/background0.png"); private final Font title = new Font("res/frostbite.ttf", 75); private final Font hint = new Font("res/frostbite.ttf", 40); - - private static int game_state = 0; - Player player = new Player(); + Wall wall = new Wall(); + Sinkhole hole = new Sinkhole(); + public ShadowDimension() { super(WINDOW_WIDTH, WINDOW_HEIGHT, GAME_TITLE); @@ -63,6 +67,20 @@ public class ShadowDimension extends AbstractGame { /*Set player pos*/ player.X = Integer.parseInt(line[1]); player.Y = Integer.parseInt(line[2]); + } else if (line[0].equals("Wall")) { + /*Set wall pos*/ + wall.Add(Integer.parseInt(line[1]), Integer.parseInt(line[2])); + } else if (line[0].equals("Sinkhole")) { + /*Set sinkhole pos*/ + hole.Add(Integer.parseInt(line[1]), Integer.parseInt(line[2])); + } else if (line[0].equals("TopLeft")) { + /*Set world top left pos*/ + player.WORLD_LEFT = Integer.parseInt(line[1]); + player.WORLD_TOP = Integer.parseInt(line[2]); + } else if (line[0].equals("BottomRight")) { + /*Set world top bottom right pos*/ + player.WORLD_RIGHT = Integer.parseInt(line[1]); + player.WORLD_BOTTOM = Integer.parseInt(line[2]); } //System.out.println(csv[i]); } @@ -76,7 +94,8 @@ public class ShadowDimension extends AbstractGame { protected void update(Input input) { /*State for game * 0:Init and show welcom page - * 1:Show Play page*/ + * 1:Show Play page + * */ switch (game_state) { case 0: { /*Draw Text*/ @@ -97,9 +116,26 @@ public class ShadowDimension extends AbstractGame { } case 1: { + /*Get key input and move player*/ + if (input.isDown(Keys.UP)) { + player.MoveUp(); + } else if (input.isDown(Keys.DOWN)) { + player.MoveDown(); + } else if (input.isDown(Keys.RIGHT)) { + player.MoveRight(); + } else if (input.isDown(Keys.LEFT)) { + 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; @@ -111,7 +147,7 @@ public class ShadowDimension extends AbstractGame { } } - + /*Game exit*/ if (input.wasPressed(Keys.ESCAPE)) { Window.close(); } diff --git a/src/Sinkhole.java b/src/Sinkhole.java new file mode 100644 index 0000000..afa84be --- /dev/null +++ b/src/Sinkhole.java @@ -0,0 +1,34 @@ +import bagel.Image; + +import java.util.ArrayList; + +/** + * Sinkhole Code for SWEN20003 Project 1, Semester 2, 2022 + * <p> + * Manage sinkhole info. + * Draw sinkhole. + * Determine the overlap between player and sinkhole. + * + * @JIAXI3 + */ + +public class Sinkhole { + private final Image Img = new Image("res/sinkhole.png"); + private final ArrayList Pos = new ArrayList(); + + public void Sinkhole() { + } + + public void Update() { + /*Gets all the hole position of the ArrayList for drawing*/ + 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)); + } +} diff --git a/src/Wall.java b/src/Wall.java new file mode 100644 index 0000000..edf2514 --- /dev/null +++ b/src/Wall.java @@ -0,0 +1,34 @@ +import bagel.Image; + +import java.util.ArrayList; + +/** + * Wall Code for SWEN20003 Project 1, Semester 2, 2022 + * <p> + * Manage walls info. + * Draw walls. + * Determine the overlap between player and wall. + * + * @JIAXI3 + */ +public class Wall { + private final Image Img = new Image("res/wall.png"); + private final ArrayList Pos = new ArrayList(); + + public void Wall() { + } + + public void Update() { + /*Gets all the wall locations of the ArrayList for drawing*/ + 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 wall position to arraylist*/ + Pos.add(new bagel.util.Point(x, y)); + } +} -- GitLab