Skip to content
Snippets Groups Projects
Commit 180c99c6 authored by jiaxi liu's avatar jiaxi liu
Browse files

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.
parent e70bd79b
No related branches found
No related tags found
No related merge requests found
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);
......
......@@ -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();
}
......
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));
}
}
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));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment