From 45e2dd3e7caff4aaa09f2ed9bb1044bf9307d4b0 Mon Sep 17 00:00:00 2001 From: Yiya Zhuang <79792524+adaiyaa@users.noreply.github.com> Date: Thu, 29 Apr 2021 06:53:51 +1000 Subject: [PATCH] Initialize src folder from my project 1 --- .idea/.gitignore | 8 +++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 ++ src/.DS_Store | Bin 0 -> 6148 bytes src/Energy.java | 15 ++++ src/Entity.java | 32 +++++++++ src/Player.java | 23 ++++++ src/Sandwich.java | 15 ++++ src/ShadowTreasure.java | 153 ++++++++++++++++++++++++++++++++++++++++ src/Zombie.java | 5 ++ yiya-project-2.iml | 13 ++++ 12 files changed, 284 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 src/.DS_Store create mode 100644 src/Energy.java create mode 100644 src/Entity.java create mode 100644 src/Player.java create mode 100644 src/Sandwich.java create mode 100644 src/ShadowTreasure.java create mode 100644 src/Zombie.java create mode 100644 yiya-project-2.iml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..93991d9 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/../../../../../../../../../:\Users\14159\Desktop\study\oosd\project2\yiya-project-2\.idea/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..639900d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectRootManager"> + <output url="file://$PROJECT_DIR$/out" /> + </component> +</project> \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..8d9dd66 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectModuleManager"> + <modules> + <module fileurl="file://$PROJECT_DIR$/yiya-project-2.iml" filepath="$PROJECT_DIR$/yiya-project-2.iml" /> + </modules> + </component> +</project> \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="VcsDirectoryMappings"> + <mapping directory="" vcs="Git" /> + </component> +</project> \ No newline at end of file diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..24957dce42144cc115095b85bb08b4dcbdca5642 GIT binary patch literal 6148 zcmZQzU|@7AO)+F(5MW?n;9!8z4DAe90Z1N%F(jFwB8(vOz-Ht#lrw}d6fvYSBtqo~ zQ6Rm-48aT;42cXW49NP68A`z>=`mz6Br=pSBzorLCnx3PCqbQS&A`Ag<3AV>vSd_# zGz3ONU^E0qLtr!nMneD|0-(AVRR43sMMfE;Aut*OqagqZ0Z{p%0BPGZI6&zJ2n~{A zU}RtbcL5j~7+4@W7(im6J_U#dX$8?Btsoktm4Ok&0-FKW%D@QK$_Va;fb@g4GcYoM zwKFh+Z3c;hwKFh+ZDwF#glK1Agxbsq?V&J2v@<Y5v@<Y*ZHKvTlpYO%(GY-z05gOk z0IL6885nT&{~@YI$<YuP4S``90*ov!!7fhVN*TNVKy@vsK21Q9W(3vA5IrDiP_+)O oikTn-iV`rTj0}*PoRI-ilQS|ffNS#6h5#&tM(NQIpnnJe01bm4*Z=?k literal 0 HcmV?d00001 diff --git a/src/Energy.java b/src/Energy.java new file mode 100644 index 0000000..3278b00 --- /dev/null +++ b/src/Energy.java @@ -0,0 +1,15 @@ +public class Energy { + private int value; + + protected Energy(int energy) { + this.value = energy; + } + + protected int getEnergy() { + return this.value; + } + + protected void add(int amount) { + this.value += amount; + } +} diff --git a/src/Entity.java b/src/Entity.java new file mode 100644 index 0000000..8d6968f --- /dev/null +++ b/src/Entity.java @@ -0,0 +1,32 @@ +import bagel.Image; +import bagel.util.Point; + +public abstract class Entity extends Image { + private double x, y; + + protected Entity(String filename, double x, double y) { + super(filename); + this.x = x; + this.y = y; + } + + protected Point getPosition() { + return new Point(this.x, this.y); + } + + protected double getX() { + return x; + } + + protected void setX(double x) { + this.x = x; + } + + protected double getY() { + return y; + } + + protected void setY(double y) { + this.y = y; + } +} \ No newline at end of file diff --git a/src/Player.java b/src/Player.java new file mode 100644 index 0000000..15f8e88 --- /dev/null +++ b/src/Player.java @@ -0,0 +1,23 @@ +import bagel.Window; + +public class Player extends Entity{ + private Energy energy; + + protected Player(String filename, double x, double y, int energy) { + super(filename, x, y); + this.energy = new Energy(energy); + } + + protected int getEnergy() { + return energy.getEnergy(); + } + + protected void updateEnergy(int amount) { + this.energy.add(amount); + } + + protected void eat(Sandwich sandwich) { + this.updateEnergy(+5); + sandwich.eaten(); + } +} diff --git a/src/Sandwich.java b/src/Sandwich.java new file mode 100644 index 0000000..52fc084 --- /dev/null +++ b/src/Sandwich.java @@ -0,0 +1,15 @@ +public class Sandwich extends Entity{ + private boolean eaten = false; + + protected Sandwich(String filename, double x, double y) { + super(filename, x, y); + } + + protected void eaten() { + this.eaten = true; + } + + protected boolean isEaten() { + return eaten; + } +} \ No newline at end of file diff --git a/src/ShadowTreasure.java b/src/ShadowTreasure.java new file mode 100644 index 0000000..814a4bf --- /dev/null +++ b/src/ShadowTreasure.java @@ -0,0 +1,153 @@ +import bagel.*; +import bagel.util.Colour; +import bagel.util.Vector2; + +import java.io.*; +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Scanner; + + +/** + * An example Bagel game. + */ +public class ShadowTreasure extends AbstractGame { + private String filename = "res/IO/environment.csv"; + private final Image background = new Image("res/images/background.png"); + private Player player; + private Zombie zombie; + private Sandwich sandwich; + private final Font energyLevel = new Font("res/font/DejaVuSans-Bold.ttf", 20); + private final DrawOptions black = new DrawOptions().setBlendColour(Colour.BLACK); + private int frame = 0; + + // for rounding double number; use this to print the location of the player + private static DecimalFormat df = new DecimalFormat("0.00"); + + public static void printInfo(double x, double y, int e) { + System.out.println(df.format(x) + "," + df.format(y) + "," + e); + } + + public ShadowTreasure() throws IOException { + this.loadEnvironment(filename); + // Add code to initialize other attributes as needed + } + + /** + * Load from input file + */ + private void loadEnvironment(String filename) throws FileNotFoundException { + // Code here to read from the file and set up the environment + Scanner scan = new Scanner(new File(filename)); + String[] row; + while (scan.hasNext()) { + row = scan.next().split(",",4); + switch (row[0]) { + case "Player" : + this.player = new Player("res/images/player.png", + Double.parseDouble(row[1]), + Double.parseDouble(row[2]), + Integer.parseInt(row[3])); + break; + case "Zombie" : + this.zombie = new Zombie("res/images/zombie.png", + Double.parseDouble(row[1]), + Double.parseDouble(row[2])); + break; + case "Sandwich" : + this.sandwich = new Sandwich("res/images/sandwich.png", + Double.parseDouble(row[1]), + Double.parseDouble(row[2])); + break; + } + } + printInfo(player.getX(), player.getY(), player.getEnergy()); + } + + /** + * Performs a state update. + */ + @Override + public void update(Input input) { + // Logic to update the game, as per specification must go here + if (input.wasPressed(Keys.ESCAPE)) { + Window.close(); + + } else if (frame<10) { + background.draw(Window.getWidth() / 2.0, Window.getHeight() / 2.0); + player.draw(player.getX(), player.getY()); + zombie.draw(zombie.getX(), zombie.getY()); + if (!sandwich.isEaten()) { + sandwich.draw(sandwich.getX(), sandwich.getY()); + } + energyLevel.drawString("energy: " + Integer.toString(player.getEnergy()), + 20, 760, black); + frame++; + return; + } + + double speed = 10; + double playerToZombie = player.getPosition().distanceTo(zombie.getPosition()); + double playerToSandwich = player.getPosition().distanceTo(sandwich.getPosition()); + double distDiff = playerToZombie - playerToSandwich; + int energy = player.getEnergy(); + Vector2 direction; + + if (sandwich.isEaten() || (distDiff <= 0 && energy >= 3)) { +// if (sandwich.isEaten() || energy >= 3) { + // player move to zombie + direction = zombie.getPosition().asVector().sub(player.getPosition().asVector()); + direction = direction.normalised(); + player.setX(player.getX() + speed * direction.x); + player.setY(player.getY() + speed * direction.y); + playerToZombie = player.getPosition().distanceTo(zombie.getPosition()); + + if (playerToZombie <= 50) { + player.updateEnergy(-3); + Window.close(); // terminate game if player meets zombie + } +// if (playerToSandwich <= 50) { +// player.eat(sandwich); +// } + + } else if (distDiff > 0 || player.getEnergy() < 3) { +// } else if (player.getEnergy() < 3) { + // player move to sandwich + direction = sandwich.getPosition().asVector().sub(player.getPosition().asVector()); + direction = direction.normalised(); + player.setX(player.getX() + speed * direction.x); + player.setY(player.getY() + speed * direction.y); + playerToSandwich = player.getPosition().distanceTo(sandwich.getPosition()); + +// if (playerToZombie <= 50) { +// player.updateEnergy(-3); +// Window.close(); // terminate game if player meets zombie + if (playerToSandwich <= 50) { + player.eat(sandwich); + } + } + + + background.draw(Window.getWidth() / 2.0, Window.getHeight() / 2.0); + player.draw(player.getX(), player.getY()); + zombie.draw(zombie.getX(), zombie.getY()); + if (!sandwich.isEaten()) { + sandwich.draw(sandwich.getX(), sandwich.getY()); + } + energyLevel.drawString("energy: " + Integer.toString(player.getEnergy()), + 20, 760, black); + + frame = 0; + printInfo(player.getX(), player.getY(), player.getEnergy()); + } + + + /** + * The entry point for the program. + */ + public static void main(String[] args) throws IOException { + ShadowTreasure game = new ShadowTreasure(); + game.run(); + } +} diff --git a/src/Zombie.java b/src/Zombie.java new file mode 100644 index 0000000..43246a1 --- /dev/null +++ b/src/Zombie.java @@ -0,0 +1,5 @@ +public class Zombie extends Entity{ + protected Zombie(String filename, double x, double y) { + super(filename, x, y); + } +} \ No newline at end of file diff --git a/yiya-project-2.iml b/yiya-project-2.iml new file mode 100644 index 0000000..d5162cf --- /dev/null +++ b/yiya-project-2.iml @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module type="JAVA_MODULE" version="4"> + <component name="NewModuleRootManager" inherit-compiler-output="true"> + <exclude-output /> + <content url="file://$MODULE_DIR$"> + <sourceFolder url="file://$MODULE_DIR$/res" type="java-resource" /> + <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> + <sourceFolder url="file://$MODULE_DIR$/test" type="java-resource" /> + </content> + <orderEntry type="inheritedJdk" /> + <orderEntry type="sourceFolder" forTests="false" /> + </component> +</module> \ No newline at end of file -- GitLab