diff --git a/project-1/src/Point.java b/project-1/src/Point.java new file mode 100644 index 0000000000000000000000000000000000000000..dd1729396a4909d472fc1d3f0bec9eccac50d5f1 --- /dev/null +++ b/project-1/src/Point.java @@ -0,0 +1,37 @@ +import java.lang.Math; + +public class Point{ + private double x,y; + + public Point(double x, double y) { + this.x = x; + this.y = y; + } + + public double getX() { + return x; + } + + public void setX(double x) { + this.x = x; + } + + public double getY() { + return y; + } + + public void setY(double y) { + this.y = y; + } + + public double distanceTo(Point other) { + return Math.sqrt(Math.pow(this.x - other.getX(),2) + Math.pow((this.y - other.getY()) ,2)); + } + + public Point getDirection (Point other) { + double xd = (other.x-this.x)/distanceTo(other); + double yd = (other.y-this.getY())/distanceTo(other); + + return new Point(xd,yd); + } +} diff --git a/project-1/src/ShadowTreasure.java b/project-1/src/ShadowTreasure.java index 39c2fa3d1507ecc736abe6f6c2face28112a9c3f..6889fcaf3df267d45fc6d82525f6d10d218b3148 100644 --- a/project-1/src/ShadowTreasure.java +++ b/project-1/src/ShadowTreasure.java @@ -1,5 +1,4 @@ import bagel.*; -import bagel.util.Point; import java.io.BufferedReader; import java.io.FileReader; @@ -20,15 +19,17 @@ public class ShadowTreasure extends AbstractGame { private Zombie zombie; private Sandwich sandwich; private static int count=0; - private static Font dejavu = new Font("res/font/DejaVuSans-Bold.ttf",20); + private Font dejavu; public static void printInfo(double x, double y, int e) { System.out.println(df.format(x) + "," + df.format(y) + "," + e); } public ShadowTreasure() throws IOException { + super(1000, 800, "ShadowTreasure"); this.loadEnvironment("res/IO/environment.csv"); // Add code to initialize other attributes as needed + this.dejavu = new Font("res/font/DejaVuSans-Bold.ttf",20); } /** @@ -36,7 +37,7 @@ public class ShadowTreasure extends AbstractGame { */ private void loadEnvironment(String filename){ // Code here to read from the file and set up the environment - Scanner scanner = new Scanner("res/environment.csv"); + Scanner scanner = new Scanner(filename); double xcoor, ycoor; int level; while(scanner.hasNext()) { @@ -58,12 +59,12 @@ public class ShadowTreasure extends AbstractGame { ycoor = scanner.nextInt(); sandwich = new Sandwich("res/Images/Sandwich.png", new Point(xcoor, ycoor)); break; + } } - // draw background at position 0,0 - Image bg = new Image("res/images/background.png"); - bg.draw(0,0); - } - } + scanner.close(); + // draw background at position 0,0 + Image bg = new Image("res/images/background.png"); + bg.draw(0,0); } /** @@ -72,12 +73,22 @@ public class ShadowTreasure extends AbstractGame { @Override public void update(Input input) { // Logic to update the game, as per specification must go here - if(count==0) { - Player player = new Player("res/images/player.png",new Point(0,0), 2); - System.out.println(player.getEnergy()); + if(player.getPos().distanceTo(zombie.getPos()) < 50) { + player.addEnergy(-3); + System.exit(0); + } else if(player.getPos().distanceTo(sandwich.getPos())<50) { + player.addEnergy(5); + sandwich = null; + } + if(player.getEnergy() >= 3) { + Point dir = player.getPos().getDirection(zombie.getPos()); + player.getPos().setX(player.getPos().getX()+dir.getX()*player.getStepsize()); + player.getPos().setY(player.getPos().getY()+dir.getY()*player.getStepsize()); + } else { + Point dir = player.getPos().getDirection(sandwich.getPos()); + player.getPos().setX(player.getPos().getX()+dir.getX()*player.getStepsize()); + player.getPos().setY(player.getPos().getY()+dir.getY()*player.getStepsize()); } - count++; - // constant info on window dejavu.drawString(String.format("energy: %d",player.getEnergy()),20,760); } diff --git a/project-1/src/test.java b/project-1/src/test.java new file mode 100644 index 0000000000000000000000000000000000000000..9aacf9e875bea8cb400b53468e7423bdf79d3327 --- /dev/null +++ b/project-1/src/test.java @@ -0,0 +1,5 @@ +public class test { + public static void main(String[] args) { + + } +}