From c66715c9e1f9b389f4082b8f252394699c36bcd3 Mon Sep 17 00:00:00 2001 From: zlatax <64391879+zlatax@users.noreply.github.com> Date: Sat, 3 Apr 2021 13:23:05 +0900 Subject: [PATCH] added algorithm 1 mechanic of game --- project-1/src/Point.java | 37 +++++++++++++++++++++++++++++++ project-1/src/ShadowTreasure.java | 37 ++++++++++++++++++++----------- project-1/src/test.java | 5 +++++ 3 files changed, 66 insertions(+), 13 deletions(-) create mode 100644 project-1/src/Point.java create mode 100644 project-1/src/test.java diff --git a/project-1/src/Point.java b/project-1/src/Point.java new file mode 100644 index 0000000..dd17293 --- /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 39c2fa3..6889fca 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 0000000..9aacf9e --- /dev/null +++ b/project-1/src/test.java @@ -0,0 +1,5 @@ +public class test { + public static void main(String[] args) { + + } +} -- GitLab