From 8b11a552a5037270e38fd0806831fc32a9241ba3 Mon Sep 17 00:00:00 2001 From: Yixin Cai <yixcai@student.unimelb.edu.au> Date: Fri, 23 Apr 2021 18:41:31 +1000 Subject: [PATCH] Initial commit --- bagel.iml | 26 -------------------------- src/Background.java | 1 + src/Entity.java | 7 +++++-- src/Player.java | 7 +++++-- src/ShadowTreasure.java | 15 ++++++--------- 5 files changed, 17 insertions(+), 39 deletions(-) delete mode 100644 bagel.iml diff --git a/bagel.iml b/bagel.iml deleted file mode 100644 index 1d041d2..0000000 --- a/bagel.iml +++ /dev/null @@ -1,26 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> - <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_14"> - <output url="file://$MODULE_DIR$/target/classes" /> - <output-test url="file://$MODULE_DIR$/target/test-classes" /> - <content url="file://$MODULE_DIR$"> - <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> - <excludeFolder url="file://$MODULE_DIR$/target" /> - </content> - <orderEntry type="inheritedJdk" /> - <orderEntry type="sourceFolder" forTests="false" /> - <orderEntry type="library" name="Maven: io.github.eleanor-em:bagel:1.9.3" level="project" /> - <orderEntry type="library" name="Maven: org.lwjgl:lwjgl:3.2.2" level="project" /> - <orderEntry type="library" name="Maven: org.lwjgl:lwjgl-assimp:3.2.2" level="project" /> - <orderEntry type="library" name="Maven: org.lwjgl:lwjgl-glfw:3.2.2" level="project" /> - <orderEntry type="library" name="Maven: org.lwjgl:lwjgl-openal:3.2.2" level="project" /> - <orderEntry type="library" name="Maven: org.lwjgl:lwjgl-opengl:3.2.2" level="project" /> - <orderEntry type="library" name="Maven: org.lwjgl:lwjgl-stb:3.2.2" level="project" /> - <orderEntry type="library" name="Maven: org.lwjgl:lwjgl:natives-macos:3.2.2" level="project" /> - <orderEntry type="library" name="Maven: org.lwjgl:lwjgl-assimp:natives-macos:3.2.2" level="project" /> - <orderEntry type="library" name="Maven: org.lwjgl:lwjgl-glfw:natives-macos:3.2.2" level="project" /> - <orderEntry type="library" name="Maven: org.lwjgl:lwjgl-openal:natives-macos:3.2.2" level="project" /> - <orderEntry type="library" name="Maven: org.lwjgl:lwjgl-opengl:natives-macos:3.2.2" level="project" /> - <orderEntry type="library" name="Maven: org.lwjgl:lwjgl-stb:natives-macos:3.2.2" level="project" /> - </component> -</module> \ No newline at end of file diff --git a/src/Background.java b/src/Background.java index 4d46fee..e28637b 100644 --- a/src/Background.java +++ b/src/Background.java @@ -7,6 +7,7 @@ public class Background{ this.image = new Image(image); } + // to draw the background of game public void drawBackground(){ image.drawFromTopLeft(0,0); } diff --git a/src/Entity.java b/src/Entity.java index 200dd1f..c74ef03 100644 --- a/src/Entity.java +++ b/src/Entity.java @@ -7,32 +7,35 @@ public class Entity { private double PositionY; private final Image image; + // set up entity public Entity(double PositionX, double PositionY, String image ){ this.image = new Image(image); this.PositionX = PositionX; this.PositionY = PositionY; }; + // to draw the entity in game public void drawEntity(){ this.image.draw(this.getPositionX(), this.getPositionY()); } + // get position public double getPositionX(){ return this.PositionX; } - public double getPositionY(){ return this.PositionY; } + // set up position public void setPositionX(double x){ this.PositionX = x; } - public void setPositionY(double y){ this.PositionY = y; } + // calculate the distance between two entity public double distanceFrom(Entity ohterEntity){ double dx = this.getPositionX() - ohterEntity.getPositionX(); double dy = this.getPositionY() - ohterEntity.getPositionY(); diff --git a/src/Player.java b/src/Player.java index 7eaaf47..8c0d83c 100644 --- a/src/Player.java +++ b/src/Player.java @@ -1,22 +1,24 @@ public class Player extends Entity { private int energyLevel; - private static final int stepSize = 10; - + // set up the player public Player(double PositionX, double PositionY, String image, int energyLevel) { super(PositionX, PositionY, image); this.energyLevel = energyLevel; } + // get energy level from player public int getEnergyLevel() { return this.energyLevel; } + // set energy level public void setEnergyLevel(int energyLeve){ this.energyLevel = energyLeve; } + // check meet to other entity public boolean meet(Entity entity){ if(this.distanceFrom(entity) < 50){ return true; @@ -24,6 +26,7 @@ public class Player extends Entity { return false; } + // move to other entity public void moveTo(Entity entity){ double xd = (entity.getPositionX() - this.getPositionX())/this.distanceFrom(entity); double yd = (entity.getPositionY() - this.getPositionY())/this.distanceFrom(entity); diff --git a/src/ShadowTreasure.java b/src/ShadowTreasure.java index 49d5859..7c76be2 100755 --- a/src/ShadowTreasure.java +++ b/src/ShadowTreasure.java @@ -16,7 +16,7 @@ public class ShadowTreasure extends AbstractGame { private static DecimalFormat df = new DecimalFormat("0.00"); - // + // Set up the attributes for game private Player player; private Zambie zambie; private Sandwich sandwich; @@ -42,20 +42,16 @@ public class ShadowTreasure extends AbstractGame { try(BufferedReader file = new BufferedReader(new FileReader(filename))){ String[] newLine = file.readLine().split(","); + //read each row from file and set up the entities and background this.player = new Player(Double.parseDouble(newLine[1]),Double.parseDouble(newLine[2]), "res/images/player.png", Integer.parseInt(newLine[3])); - newLine = file.readLine().split(","); - this.zambie = new Zambie(Double.parseDouble(newLine[1]),Double.parseDouble(newLine[2]), "res/images/zombie.png"); - newLine = file.readLine().split(","); - this.sandwich = new Sandwich(Double.parseDouble(newLine[1]),Double.parseDouble(newLine[2]), "res/images/sandwich.png"); - this.background = new Background("res/images/background.png"); - this.font = new Font( "res/font/DejaVuSans-Bold.ttf",20); + // draw the attributes and energy level this.background.drawBackground(); this.player.drawEntity(); this.zambie.drawEntity(); @@ -74,6 +70,8 @@ public class ShadowTreasure extends AbstractGame { @Override public void update(Input input) { // Logic to update the game, as per specification must go here + + // draw the attributes and energy level this.background.drawBackground(); this.player.drawEntity(); this.zambie.drawEntity(); @@ -82,6 +80,7 @@ public class ShadowTreasure extends AbstractGame { } this.font.drawString("energy:" + this.player.getEnergyLevel(), 20, 760); + // follow the interaction logic and update energy in each tick, than output the location and energy level if( this.countFrame == 10){ if( this.player.meet(this.zambie)){ this.player.setEnergyLevel(this.player.getEnergyLevel() - 3); @@ -94,13 +93,11 @@ public class ShadowTreasure extends AbstractGame { }else{ printInfo(this.player.getPositionX(), this.player.getPositionY(), this.player.getEnergyLevel()); } - if( this.player.getEnergyLevel() >= 3){ this.player.moveTo(this.zambie); }else{ this.player.moveTo(this.sandwich); } - this.countFrame = 0; } this.countFrame += 1; -- GitLab