Skip to content
Snippets Groups Projects
Commit a604c96d authored by Yiya Zhuang's avatar Yiya Zhuang
Browse files

我想要的version

parent 15641c3d
No related branches found
No related tags found
No related merge requests found
...@@ -2,5 +2,7 @@ ...@@ -2,5 +2,7 @@
<project version="4"> <project version="4">
<component name="Encoding"> <component name="Encoding">
<file url="file://$PROJECT_DIR$/res/IO/environment.csv" charset="GBK" /> <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> </component>
</project> </project>
\ No newline at end of file
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/res" type="java-resource" /> <sourceFolder url="file://$MODULE_DIR$/res" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" type="java-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" /> <excludeFolder url="file://$MODULE_DIR$/target" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
......
...@@ -13,10 +13,11 @@ public class Player extends Entity{ ...@@ -13,10 +13,11 @@ public class Player extends Entity{
} }
protected void updateEnergy(int amount) { protected void updateEnergy(int amount) {
energy.add(amount); this.energy.add(amount);
} }
protected void eat(Sandwich sandwich) { protected void eat(Sandwich sandwich) {
this.updateEnergy(+5);
sandwich.eaten(); sandwich.eaten();
} }
} }
import bagel.*; import bagel.*;
import bagel.util.Colour;
import bagel.util.Vector2; import bagel.util.Vector2;
import java.io.*; import java.io.*;
...@@ -12,11 +13,14 @@ import java.util.Scanner; ...@@ -12,11 +13,14 @@ import java.util.Scanner;
* An example Bagel game. * An example Bagel game.
*/ */
public class ShadowTreasure extends AbstractGame { 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 Player player;
private Zombie zombie; private Zombie zombie;
private Sandwich sandwich; private Sandwich sandwich;
private final Font energyLevel = new Font("res/font/DejaVuSans-Bold.ttf", 20); 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 // for rounding double number; use this to print the location of the player
private static DecimalFormat df = new DecimalFormat("0.00"); private static DecimalFormat df = new DecimalFormat("0.00");
...@@ -26,9 +30,8 @@ public class ShadowTreasure extends AbstractGame { ...@@ -26,9 +30,8 @@ public class ShadowTreasure extends AbstractGame {
} }
public ShadowTreasure() throws IOException { public ShadowTreasure() throws IOException {
this.loadEnvironment("res/IO/environment.csv"); this.loadEnvironment(filename);
// Add code to initialize other attributes as needed // Add code to initialize other attributes as needed
background = new Image("res/images/background.png");
} }
/** /**
...@@ -59,6 +62,7 @@ public class ShadowTreasure extends AbstractGame { ...@@ -59,6 +62,7 @@ public class ShadowTreasure extends AbstractGame {
break; break;
} }
} }
printInfo(player.getX(), player.getY(), player.getEnergy());
} }
/** /**
...@@ -67,47 +71,75 @@ public class ShadowTreasure extends AbstractGame { ...@@ -67,47 +71,75 @@ public class ShadowTreasure extends AbstractGame {
@Override @Override
public void update(Input input) { public void update(Input input) {
// Logic to update the game, as per specification must go here // 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 playerToZombie = player.getPosition().distanceTo(zombie.getPosition());
double playerToSandwich = player.getPosition().distanceTo(sandwich.getPosition()); double playerToSandwich = player.getPosition().distanceTo(sandwich.getPosition());
double distDiff = playerToZombie - playerToSandwich; double distDiff = playerToZombie - playerToSandwich;
int energy = player.getEnergy(); int energy = player.getEnergy();
Vector2 direction; Vector2 direction;
if (input.wasPressed(Keys.ESCAPE)) { if (sandwich.isEaten() || (distDiff <= 0 && energy >= 3)) {
Window.close(); // if (sandwich.isEaten() || energy >= 3) {
} 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)) {
// player move to zombie // player move to zombie
direction = zombie.getPosition().asVector().sub(player.getPosition().asVector()); direction = zombie.getPosition().asVector().sub(player.getPosition().asVector());
direction = direction.normalised(); direction = direction.normalised();
player.setX(player.getX() + speed * direction.x); player.setX(player.getX() + speed * direction.x);
player.setY(player.getY() + speed * direction.y); 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 // player move to sandwich
direction = sandwich.getPosition().asVector().sub(player.getPosition().asVector()); direction = sandwich.getPosition().asVector().sub(player.getPosition().asVector());
direction = direction.normalised(); direction = direction.normalised();
player.setX(player.getX() + speed * direction.x); player.setX(player.getX() + speed * direction.x);
player.setY(player.getY() + speed * direction.y); 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); 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()); player.draw(player.getX(), player.getY());
zombie.draw(zombie.getX(), zombie.getY()); zombie.draw(zombie.getX(), zombie.getY());
if (!sandwich.isEaten()) { if (!sandwich.isEaten()) {
sandwich.draw(sandwich.getX(), sandwich.getY()); 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());
} }
......
No preview for this file type
No preview for this file type
Player,650,100,2
Zombie,300,200
Sandwich,500,400
\ No newline at end of file
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
Player,650,100,3
Zombie,500,500
Sandwich,300,200
\ No newline at end of file
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
Player,650,100,2 Player,650,100,2
Zombie,300,200 Zombie,300,200
Sandwich,500,400 Sandwich,500,400
\ No newline at end of file
Player,650,100,3 Player,650,100,3
Zombie,500,500 Zombie,500,500
Sandwich,300,200 Sandwich,300,200
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment