diff --git a/.idea/encodings.xml b/.idea/encodings.xml index 082896b1a32f37c562f3b2c09bbab8b3ad6b7e2d..8c4dc07abe12acf7923c431042d41a38bc07ff33 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -2,5 +2,7 @@ <project version="4"> <component name="Encoding"> <file url="file://$PROJECT_DIR$/res/IO/environment.csv" charset="GBK" /> + <file url="file://$PROJECT_DIR$/test/test1/environment.csv" charset="GBK" /> + <file url="file://$PROJECT_DIR$/test/test2/environment.csv" charset="GBK" /> </component> </project> \ No newline at end of file diff --git a/bagel.iml b/bagel.iml index bb67b4e1de0124283d12a2911f01ac2b569e3b23..caf8fb73274a747e1bf4904c9ef3caf553038885 100644 --- a/bagel.iml +++ b/bagel.iml @@ -6,6 +6,7 @@ <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" /> <excludeFolder url="file://$MODULE_DIR$/target" /> </content> <orderEntry type="inheritedJdk" /> diff --git a/src/Player.java b/src/Player.java index 1161b2e4218250ae12e0686b179b4011d8d7f34c..15f8e888e21cc0bcb9125a11524dd6beb0dfc1c4 100644 --- a/src/Player.java +++ b/src/Player.java @@ -13,10 +13,11 @@ public class Player extends Entity{ } protected void updateEnergy(int amount) { - energy.add(amount); + this.energy.add(amount); } protected void eat(Sandwich sandwich) { + this.updateEnergy(+5); sandwich.eaten(); } } diff --git a/src/ShadowTreasure.java b/src/ShadowTreasure.java index 1f01ca33beac48f412f4b725bb36d2814fc65395..814a4bf73a0c175eeccfd7aa70c3fdcc91846893 100644 --- a/src/ShadowTreasure.java +++ b/src/ShadowTreasure.java @@ -1,4 +1,5 @@ import bagel.*; +import bagel.util.Colour; import bagel.util.Vector2; import java.io.*; @@ -12,11 +13,14 @@ import java.util.Scanner; * An example Bagel game. */ public class ShadowTreasure extends AbstractGame { - private final Image background; + 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"); @@ -26,9 +30,8 @@ public class ShadowTreasure extends AbstractGame { } public ShadowTreasure() throws IOException { - this.loadEnvironment("res/IO/environment.csv"); + this.loadEnvironment(filename); // Add code to initialize other attributes as needed - background = new Image("res/images/background.png"); } /** @@ -59,6 +62,7 @@ public class ShadowTreasure extends AbstractGame { break; } } + printInfo(player.getX(), player.getY(), player.getEnergy()); } /** @@ -67,47 +71,75 @@ public class ShadowTreasure extends AbstractGame { @Override public void update(Input input) { // Logic to update the game, as per specification must go here - double speed = 10, x, y; + 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 (input.wasPressed(Keys.ESCAPE)) { - Window.close(); - - } else if (playerToZombie <= 50) { - player.updateEnergy(-3); - Window.close(); // terminate game if player meets zombie - - } else if (playerToSandwich <= 50 && !sandwich.isEaten()) { - player.eat(sandwich); - player.updateEnergy(+5); - - } else if (sandwich.isEaten() || (distDiff < 0 && energy >= 3)) { + 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); - } else if (distDiff >= 0 || player.getEnergy() < 3) { + 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); - energyLevel.drawString("energy: " + Integer.toString(player.getEnergy()), - 20, 760); 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()); } diff --git a/target/classes/Player.class b/target/classes/Player.class index f73a38e0a46071064afac16b858a2daf947e5336..3a0c3f5d4eb3b2875a140b8a0a29834fcc91c597 100644 Binary files a/target/classes/Player.class and b/target/classes/Player.class differ diff --git a/target/classes/ShadowTreasure.class b/target/classes/ShadowTreasure.class index 9302a6e71f79cb6282827b663cb98188e068aee7..44025b593df3de73b399bf3b39f28aa5ff878a1b 100644 Binary files a/target/classes/ShadowTreasure.class and b/target/classes/ShadowTreasure.class differ diff --git a/target/classes/test1/environment.csv b/target/classes/test1/environment.csv new file mode 100644 index 0000000000000000000000000000000000000000..99f4061217923093bbeec74f5304f5a141c714f1 --- /dev/null +++ b/target/classes/test1/environment.csv @@ -0,0 +1,3 @@ +Player,650,100,2 +Zombie,300,200 +Sandwich,500,400 \ No newline at end of file diff --git a/target/classes/test1/output.csv b/target/classes/test1/output.csv new file mode 100644 index 0000000000000000000000000000000000000000..331f2949d096b29343572835ee807a617e1e88de --- /dev/null +++ b/target/classes/test1/output.csv @@ -0,0 +1,53 @@ +650.00,100.00,2 +645.53,108.94,2 +641.06,117.89,2 +636.58,126.83,2 +632.11,135.78,2 +627.64,144.72,2 +623.17,153.67,2 +618.70,162.61,2 +614.22,171.55,2 +609.75,180.50,2 +605.28,189.44,2 +600.81,198.39,2 +596.33,207.33,2 +591.86,216.28,2 +587.39,225.22,2 +582.92,234.16,2 +578.45,243.11,2 +573.97,252.05,2 +569.50,261.00,2 +565.03,269.94,2 +560.56,278.89,2 +556.09,287.83,2 +551.61,296.77,2 +547.14,305.72,2 +542.67,314.66,2 +538.20,323.61,2 +533.72,332.55,2 +529.25,341.50,2 +524.78,350.44,2 +520.31,359.38,7 +512.21,353.52,7 +504.10,347.66,7 +496.00,341.80,7 +487.90,335.94,7 +479.80,330.08,7 +471.70,324.21,7 +463.59,318.35,7 +455.49,312.49,7 +447.39,306.63,7 +439.29,300.77,7 +431.19,294.91,7 +423.08,289.05,7 +414.98,283.18,7 +406.88,277.32,7 +398.78,271.46,7 +390.68,265.60,7 +382.57,259.74,7 +374.47,253.88,7 +366.37,248.02,7 +358.27,242.15,7 +350.17,236.29,7 +342.06,230.43,7 +333.96,224.57,4 diff --git a/target/classes/test2/environment.csv b/target/classes/test2/environment.csv new file mode 100644 index 0000000000000000000000000000000000000000..1988788df9eb191b98835825becdab71d1122546 --- /dev/null +++ b/target/classes/test2/environment.csv @@ -0,0 +1,3 @@ +Player,650,100,3 +Zombie,500,500 +Sandwich,300,200 \ No newline at end of file diff --git a/target/classes/test2/output.csv b/target/classes/test2/output.csv new file mode 100644 index 0000000000000000000000000000000000000000..85bdc5c3a6f2228b4c3a76410bbe6a043b63634e --- /dev/null +++ b/target/classes/test2/output.csv @@ -0,0 +1,39 @@ +650.00,100.00,3 +646.49,109.36,3 +642.98,118.73,3 +639.47,128.09,3 +635.96,137.45,3 +632.44,146.82,3 +628.93,156.18,3 +625.42,165.54,3 +621.91,174.91,3 +618.40,184.27,3 +614.89,193.63,3 +611.38,203.00,3 +607.87,212.36,3 +604.35,221.72,3 +600.84,231.09,3 +597.33,240.45,3 +593.82,249.81,3 +590.31,259.18,3 +586.80,268.54,3 +583.29,277.90,3 +579.78,287.27,3 +576.26,296.63,3 +572.75,305.99,3 +569.24,315.36,3 +565.73,324.72,3 +562.22,334.08,3 +558.71,343.45,3 +555.20,352.81,3 +551.69,362.17,3 +548.17,371.54,3 +544.66,380.90,3 +541.15,390.26,3 +537.64,399.63,3 +534.13,408.99,3 +530.62,418.35,3 +527.11,427.72,3 +523.60,437.08,3 +520.08,446.44,3 +516.57,455.81,0 diff --git a/test/test1/environment.csv b/test/test1/environment.csv index 79fea88a9ab126ee1e10f0828bcff2047ec182f7..99f4061217923093bbeec74f5304f5a141c714f1 100644 --- a/test/test1/environment.csv +++ b/test/test1/environment.csv @@ -1,3 +1,3 @@ -Player,650,100,2 +Player,650,100,2 Zombie,300,200 Sandwich,500,400 \ No newline at end of file diff --git a/test/test2/environment.csv b/test/test2/environment.csv index 114e4b5d2caee833c42560d1813c65d6bb319405..1988788df9eb191b98835825becdab71d1122546 100644 --- a/test/test2/environment.csv +++ b/test/test2/environment.csv @@ -1,3 +1,3 @@ -Player,650,100,3 +Player,650,100,3 Zombie,500,500 Sandwich,300,200 \ No newline at end of file