diff --git a/GUANCHAOH-project2.7z b/GUANCHAOH-project2.7z deleted file mode 100644 index aa94977e1b1f15f03e5434a488c74412f4783828..0000000000000000000000000000000000000000 Binary files a/GUANCHAOH-project2.7z and /dev/null differ diff --git a/GUANCHAOH-project2/GUANCHAOH-project2.iml b/GUANCHAOH-project2/GUANCHAOH-project2.iml new file mode 100644 index 0000000000000000000000000000000000000000..917cee11be4b6a683ee30eda93d46bc027c3e189 --- /dev/null +++ b/GUANCHAOH-project2/GUANCHAOH-project2.iml @@ -0,0 +1,30 @@ +<?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$/src" isTestSource="false" /> + </content> + <orderEntry type="inheritedJdk" /> + <orderEntry type="sourceFolder" forTests="false" /> + <orderEntry type="library" exported="" name="bagel-0.1-SNAPSHOT" level="project" /> + <orderEntry type="module-library" exported=""> + <library> + <CLASSES> + <root url="jar://$MODULE_DIR$/lib/bagel-0.1-SNAPSHOT.jar!/" /> + </CLASSES> + <JAVADOC /> + <SOURCES /> + </library> + </orderEntry> + <orderEntry type="module-library" exported=""> + <library> + <CLASSES> + <root url="jar://$MODULE_DIR$/lib/bagel-1.9.3.jar!/" /> + </CLASSES> + <JAVADOC /> + <SOURCES /> + </library> + </orderEntry> + </component> +</module> \ No newline at end of file diff --git a/Iib/bagel-0.1-SNAPSHOT.jar b/Iib/bagel-0.1-SNAPSHOT.jar new file mode 100644 index 0000000000000000000000000000000000000000..7971ca9e1c1ad59e287794ee370250934dd0f6ec Binary files /dev/null and b/Iib/bagel-0.1-SNAPSHOT.jar differ diff --git a/Iib/bagel-1.9.3.jar b/Iib/bagel-1.9.3.jar new file mode 100644 index 0000000000000000000000000000000000000000..dc497269cd5788d8e79e9df96a9fff69c0c922c5 Binary files /dev/null and b/Iib/bagel-1.9.3.jar differ diff --git a/res/IO/environment.csv b/res/IO/environment.csv new file mode 100644 index 0000000000000000000000000000000000000000..eadd60de4d98994724b8781ac99f587c4998c5d4 --- /dev/null +++ b/res/IO/environment.csv @@ -0,0 +1,8 @@ +Player,700,100,2 +Zombie,100,200 +Zombie,150,300 +Zombie,650,600 +Treasure,160,130 +Sandwich,500,650 +Sandwich,400,500 +Sandwich,600,150 diff --git a/res/IO/output.csv b/res/IO/output.csv new file mode 100644 index 0000000000000000000000000000000000000000..85fe369c7e9c1b3371d78060acbcfb60b6bd13f7 --- /dev/null +++ b/res/IO/output.csv @@ -0,0 +1,10 @@ +700.00,100.00 +646.00,451.19 +647.61,511.17 +649.22,571.15 +289.80,350.98 +233.43,330.43 +177.07,309.87 +207.56,303.82 +164.39,262.15 +121.22,220.48 diff --git a/res/font/DejaVuSans-Bold.ttf b/res/font/DejaVuSans-Bold.ttf new file mode 100644 index 0000000000000000000000000000000000000000..c0d8e5e462df7f268efba09f6931e37daad151e1 Binary files /dev/null and b/res/font/DejaVuSans-Bold.ttf differ diff --git a/res/image/Thumbs.db b/res/image/Thumbs.db new file mode 100644 index 0000000000000000000000000000000000000000..6f9f6aad13bc7cb5ce313b93452ba74b0b7c2984 Binary files /dev/null and b/res/image/Thumbs.db differ diff --git a/res/image/background.png b/res/image/background.png new file mode 100644 index 0000000000000000000000000000000000000000..a6601996a24eebf1b568ad8e05d2e137a030b52e Binary files /dev/null and b/res/image/background.png differ diff --git a/res/image/player.png b/res/image/player.png new file mode 100644 index 0000000000000000000000000000000000000000..103a2ff1c64ccd5bf18b9ef8b2ca444dbdf204d1 Binary files /dev/null and b/res/image/player.png differ diff --git a/res/image/sandwich.png b/res/image/sandwich.png new file mode 100644 index 0000000000000000000000000000000000000000..87e12ec3db6704914262fd359822407fb9ecd172 Binary files /dev/null and b/res/image/sandwich.png differ diff --git a/res/image/shot.png b/res/image/shot.png new file mode 100644 index 0000000000000000000000000000000000000000..7d94f37bc501cdb64c1b0038bf2fd7cabe4c8cd3 Binary files /dev/null and b/res/image/shot.png differ diff --git a/res/image/treasure.png b/res/image/treasure.png new file mode 100644 index 0000000000000000000000000000000000000000..4ac3ccb54c8992f272e2f7f9d0a4c072444d02f8 Binary files /dev/null and b/res/image/treasure.png differ diff --git a/res/image/zombie.png b/res/image/zombie.png new file mode 100644 index 0000000000000000000000000000000000000000..41519c41828d76f5cdfbd9493025d3867347d926 Binary files /dev/null and b/res/image/zombie.png differ diff --git a/src/Bullet.java b/src/Bullet.java new file mode 100644 index 0000000000000000000000000000000000000000..8b75b4275ea7a112eb10132d28c36aad43b67132 --- /dev/null +++ b/src/Bullet.java @@ -0,0 +1,38 @@ +import bagel.Image; +import bagel.util.Point; + +public class Bullet { + private final Image image = new Image("res/images/bullet.png"); + + private Point pos; + + private Zombie deszom; + private double desX; + private double desY; + + public static final double STEP_SIZE = 25; + + public Bullet(double x, double y, double desX, double desY, Zombie deszom) { + this.pos = new Point(x, y); + this.desX = desX; + this.desY = desY; + this.deszom = deszom; + } + + public Point getPos() { + return pos; + } + + public void draw() { + image.drawFromTopLeft(pos.x, pos.y); + } + + public void update() { + if (deszom.shooted(this)) { + Player.setbullet(); + } else { + this.pos = new Point(this.pos.x + STEP_SIZE * this.desX, this.pos.y + STEP_SIZE * this.desY); + + } + } +} diff --git a/src/Player.java b/src/Player.java new file mode 100644 index 0000000000000000000000000000000000000000..2ca43d8e5b735ffc74e2fd98f0030b2bea13d20e --- /dev/null +++ b/src/Player.java @@ -0,0 +1,142 @@ +import bagel.DrawOptions; +import bagel.Font; +import bagel.Image; +import bagel.util.Colour; +import bagel.util.Point; +import bagel.util.Vector2; + +import java.util.ArrayList; + +public class Player { + + public static final String FILENAME = "res/images/player.png"; + + public static final double STEP_SIZE = 10; + + private static final int LOWENERGY = 3; + + private static final Vector2 ZERO_VECTOR = new Vector2(0, 0); + + private final Font FONT = new Font("res/font/DejaVuSans-Bold.ttf", 20); + private final DrawOptions OPT = new DrawOptions(); + + private final Image image; + + private Point pos; + private double directionX; + private double directionY; + + private int energy; + private static int isbullet = 0; + private static Bullet bullet; + + public Player(double x, double y, int energy) { + this.image = new Image(FILENAME); + this.pos = new Point(x, y); + this.energy = energy; + } + + public Point getPos() { + return this.pos; + } + + public int getEnergy() { + return this.energy; + } + + public void pointTo(Point dest) { + this.directionX = dest.x - this.pos.x; + this.directionY = dest.y - this.pos.y; + normalizeD(); + } + + public void normalizeD() { + double len = Math.sqrt(Math.pow(this.directionX, 2) + Math.pow(this.directionY, 2)); + this.directionX /= len; + this.directionY /= len; + } + + public void update(ShadowTreasure tomb) { + Zombie zombie = this.nearstZombie(tomb.getZombie()); + Sandwich sandwich = this.nearstSandwich(tomb.getSandwich()); + if (zombie == null) { + pointTo(tomb.getTreasure().getPos()); + tomb.setEndOfGame(true); + } else { + if (zombie.meets(this) || isbullet == 1) { + shoot(zombie.getPos().x, zombie.getPos().y, zombie); + tomb.setEndOfGame(true); + } else if (sandwich.meets(this)) { + eatSandwich(); + sandwich.setVisible(false); + } + + if (this.energy >= LOWENERGY) { + pointTo(this.nearstZombie(tomb.getZombie()).getPos()); + } else { + pointTo(this.nearstSandwich(tomb.getSandwich()).getPos()); + } + } + this.pos = new Point(this.pos.x + STEP_SIZE * this.directionX, this.pos.y + STEP_SIZE * this.directionY); + } + + public void render() { + image.drawFromTopLeft(pos.x, pos.y); + FONT.drawString("energy: " + energy, 20, 760, OPT.setBlendColour(Colour.BLACK)); + } + + public void eatSandwich() { + energy += 5; + } + + public void reachZombie() { + energy -= 3; + } + + public double dis(Point pos) { + //return Math.sqrt(Math.pow(this.pos.x-x,2) + Math.pow(this.pos.y-y,2)); + return this.pos.distanceTo(pos); + } + + public Zombie nearstZombie(ArrayList<Zombie> zombielist) { + int max = Integer.MAX_VALUE; + Zombie nearst = null; + Point zombiepos; + for (Zombie zombie : zombielist) { + zombiepos = zombie.getPos(); + if (this.dis(zombiepos) < max) + nearst = zombie; + } + return nearst; + } + + public Sandwich nearstSandwich(ArrayList<Sandwich> Sandwichlist) { + int max = Integer.MAX_VALUE; + Sandwich nearst = null; + Point sandwichpos; + for (Sandwich sandwich : Sandwichlist) { + sandwichpos = sandwich.getPos(); + if (this.dis(sandwichpos) < max) + nearst = sandwich; + } + return nearst; + } + + public void shoot(double desx, double desy, Zombie deszom) { + if (isbullet == 0) { + this.isbullet = 1; + Bullet bullet = new Bullet(this.pos.x, this.pos.y, desx, desy, deszom); + bullet.draw(); + this.energy -= 3; + } else { + bullet.update(); + bullet.draw(); + } + + } + + public static void setbullet() { + isbullet = 0; + bullet = null; + } +} diff --git a/src/Record.java b/src/Record.java new file mode 100644 index 0000000000000000000000000000000000000000..85a2d261c55049e4cade1816e885c6341ecc61aa --- /dev/null +++ b/src/Record.java @@ -0,0 +1,23 @@ +import bagel.util.Point; + +public class Record { + private Point pos; + private int energy; + + public Record(Point p, int e) { + this.pos = p; + this.energy = e; + } + + public double getPosX() { + return pos.x; + } + + public double getPosY() { + return pos.y; + } + + public int getEnergy() { + return energy; + } +} diff --git a/src/Sandwich.java b/src/Sandwich.java new file mode 100644 index 0000000000000000000000000000000000000000..55186b2700a60aa57f1be1c13fa25526e7079fda --- /dev/null +++ b/src/Sandwich.java @@ -0,0 +1,49 @@ +import bagel.Image; +import bagel.util.Point; + +public class Sandwich { + // image + private final Image image = new Image("res/images/sandwich.png"); + private boolean visible; + + // render position + private Point pos; + + public Sandwich(double x, double y) { + this.pos = new Point(x, y); + this.visible = true; + } + + public Point getPos() { + return pos; + } + + public boolean isVisible() { + return visible; + } + + public void setVisible(boolean visiblility) { + this.visible = visiblility; + } + + // render image + public void draw() { + if (visible) { + image.drawFromTopLeft(pos.x, pos.y); + } + } + + public boolean meets(Player player) { + boolean hasMet = false; + + if (isVisible()) { + double distanceToPlayer = player.getPos().distanceTo(pos); + if (distanceToPlayer < ShadowTreasure.CLOSENESS) { + hasMet = true; + } + } + return hasMet; + } + + +} diff --git a/src/ShadowTreasure.java b/src/ShadowTreasure.java new file mode 100644 index 0000000000000000000000000000000000000000..5ae6e8af31f06c3a9c39d26f2ff928a8681d3285 --- /dev/null +++ b/src/ShadowTreasure.java @@ -0,0 +1,107 @@ +import bagel.*; + +import java.io.*; +import java.text.DecimalFormat; +import java.util.ArrayList; + +public class ShadowTreasure extends AbstractGame { + + private final Image Backgroubd = new Image("res/images/brackground.png"); + public static final int CLOSENESS = 150; + + private static DecimalFormat df = new DecimalFormat("0.00"); + + private final int TICK_CYCLE = 10; + private int tick; + + private Player player; + private Treasure treasure; + private ArrayList<Sandwich> sandwitch = new ArrayList<Sandwich>(); + private ArrayList<Zombie> zombie = new ArrayList<Zombie>(); + + private boolean endOfGame; + + public ShadowTreasure() throws IOException { + this.loadEnvironment("res/IO/environment.csv"); + this.tick = 1; + this.endOfGame = false; + System.out.println(player.getPos().x + "," + player.getPos().y + "," + player.getEnergy()); + } + + public ArrayList<Sandwich> getSandwich() { + return sandwitch; + } + + public ArrayList<Zombie> getZombie() { + return zombie; + } + + public Treasure getTreasure() { + return treasure; + } + + public void setEndOfGame(boolean endOfGame) { + this.endOfGame = endOfGame; + } + + public void loadEnvironment(String filename) { + try (BufferedReader reader = new BufferedReader(new FileReader(filename))) { + String line; + while ((line = reader.readLine()) != null) { + String[] parts = line.split(","); + String type = parts[0]; + type = type.replaceAll("[^a-zA-Z0-9]", ""); + int x = Integer.parseInt((parts[1])); + int y = Integer.parseInt((parts[2])); + switch (type) { + case "Player": + this.player = new Player(x, y, Integer.parseInt(parts[3])); + break; + case "Zombie": + this.zombie.add(new Zombie(x, y)); + break; + case "Sandwich": + this.sandwitch.add(new Sandwich(x, y)); + break; + case "Treasure": + this.treasure = new Treasure(x, y); + default: + throw new BagelError("Unknown type:" + type); + } + } + } catch (IOException e) { + e.printStackTrace(); + System.exit(-1); + } + } + + @Override + protected void update(Input input) { + if (this.endOfGame || input.wasPressed(Keys.ESCAPE)) { + Window.close(); + } else { + Backgroubd.drawFromTopLeft(0, 0); + if (tick > TICK_CYCLE) { + player.update(this); + tick = 1; + System.out.println(df.format(player.getPos().x) + "," + df.format(player.getPos().y) + "," + player.getEnergy()); + } + tick++; + for (Sandwich sand : sandwitch) { + sand.draw(); + } + for (Zombie zom : zombie) { + zom.draw(); + } + treasure.draw(); + player.render(); + } + } + + public static void main(String[] args) throws IOException { + ShadowTreasure game = new ShadowTreasure(); + game.run(); + } + + +} diff --git a/src/Treasure.java b/src/Treasure.java new file mode 100644 index 0000000000000000000000000000000000000000..f1561f23e44cf86795f1d83bdafe7a9c7827c0bd --- /dev/null +++ b/src/Treasure.java @@ -0,0 +1,20 @@ +import bagel.Image; +import bagel.util.Point; + +public class Treasure { + private final Image image = new Image("res/images/treasure.png"); + + private Point pos; + + public Treasure(double x, double y) { + this.pos = new Point(x, y); + } + + public Point getPos() { + return pos; + } + + public void draw() { + image.drawFromTopLeft(pos.x, pos.y); + } +} diff --git a/src/Zombie.java b/src/Zombie.java new file mode 100644 index 0000000000000000000000000000000000000000..0efb7ff7a9bb8c8248c436d73a1a6ab52767bdf4 --- /dev/null +++ b/src/Zombie.java @@ -0,0 +1,45 @@ +import bagel.Image; +import bagel.util.Point; + +public class Zombie { + + // image and type + private final Image image = new Image("res/images/zombie.png"); + + // render position + private Point pos; + private int vis = 1; + + public Zombie(double x, double y) { + this.pos = new Point(x, y); + } + + public Point getPos() { + return pos; + } + + // render image + public void draw() { + if (this.vis == 1) + image.drawFromTopLeft(pos.x, pos.y); + } + + public boolean meets(Player player) { + boolean hasMet = false; + double distanceToPlayer = player.getPos().distanceTo(pos); + if (distanceToPlayer < ShadowTreasure.CLOSENESS) { + hasMet = true; + } + return hasMet; + } + + public boolean shooted(Bullet bullet) { + boolean hasshot = false; + double distanceToBullet = bullet.getPos().distanceTo(pos); + if (distanceToBullet < 50) { + hasshot = true; + this.vis = 0; + } + return hasshot; + } +} diff --git a/test/ test1/environment.csv b/test/ test1/environment.csv new file mode 100644 index 0000000000000000000000000000000000000000..916c71d6074969fd68bb82acb795b835cd2fc633 --- /dev/null +++ b/test/ 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/test/ test1/output.csv b/test/ test1/output.csv new file mode 100644 index 0000000000000000000000000000000000000000..062873bc791d58c919a322121d89fc5ec59203aa --- /dev/null +++ b/test/ test1/output.csv @@ -0,0 +1,54 @@ +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,2 +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,7 +325.86,218.71,4 diff --git a/test/test2/environment.csv b/test/test2/environment.csv new file mode 100644 index 0000000000000000000000000000000000000000..94d91f71ce8f4a6e6f6582439a2a6c4346d0fe32 --- /dev/null +++ b/test/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/test/test2/output.csv b/test/test2/output.csv new file mode 100644 index 0000000000000000000000000000000000000000..8fd27742b73803f50848852a0f79ad6d03929230 --- /dev/null +++ b/test/test2/output.csv @@ -0,0 +1,40 @@ +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,3 +510.11,448.17,0