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

除了energy字体颜色和output to console之外,其他都做好了

parent a68041d9
No related branches found
No related tags found
No related merge requests found
Showing
with 180 additions and 9 deletions
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/res/IO/environment.csv" charset="GBK" />
</component>
</project>
\ No newline at end of file
......@@ -7,5 +7,7 @@
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_14" project-jdk-name="14" project-jdk-type="JavaSDK" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_15" default="true" project-jdk-name="openjdk-15" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$USER_HOME$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/../../.." vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
......@@ -4,6 +4,7 @@
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/res" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
......
Player,650,100,2
Player,650,100,2
Zombie,300,200
Sandwich,500,400
\ No newline at end of file
public class Energy {
private int value;
protected Energy(int energy) {
this.value = energy;
}
protected int getEnergy() {
return this.value;
}
protected void add(int amount) {
this.value += amount;
}
}
import bagel.Image;
import bagel.util.Point;
public abstract class Entity extends Image {
private double x, y;
protected Entity(String filename, double x, double y) {
super(filename);
this.x = x;
this.y = y;
}
protected Point getPosition() {
return new Point(this.x, this.y);
}
protected double getX() {
return x;
}
protected void setX(double x) {
this.x = x;
}
protected double getY() {
return y;
}
protected void setY(double y) {
this.y = y;
}
}
\ No newline at end of file
import bagel.Window;
public class Player extends Entity{
private Energy energy;
protected Player(String filename, double x, double y, int energy) {
super(filename, x, y);
this.energy = new Energy(energy);
}
protected int getEnergy() {
return energy.getEnergy();
}
protected void updateEnergy(int amount) {
energy.add(amount);
}
protected void eat(Sandwich sandwich) {
sandwich.eaten();
}
}
public class Sandwich extends Entity{
private boolean eaten = false;
protected Sandwich(String filename, double x, double y) {
super(filename, x, y);
}
protected void eaten() {
this.eaten = true;
}
protected boolean isEaten() {
return eaten;
}
}
\ No newline at end of file
import bagel.*;
import bagel.util.Vector2;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
/**
* An example Bagel game.
*/
public class ShadowTreasure extends AbstractGame {
private final Image background;
private Player player;
private Zombie zombie;
private Sandwich sandwich;
private final Font energyLevel = new Font("res/font/DejaVuSans-Bold.ttf", 20);
// for rounding double number; use this to print the location of the player
private static DecimalFormat df = new DecimalFormat("0.00");
......@@ -21,13 +28,37 @@ public class ShadowTreasure extends AbstractGame {
public ShadowTreasure() throws IOException {
this.loadEnvironment("res/IO/environment.csv");
// Add code to initialize other attributes as needed
background = new Image("res/images/background.png");
}
/**
* Load from input file
*/
private void loadEnvironment(String filename){
private void loadEnvironment(String filename) throws FileNotFoundException {
// Code here to read from the file and set up the environment
Scanner scan = new Scanner(new File(filename));
String[] row;
while (scan.hasNext()) {
row = scan.next().split(",",4);
switch (row[0]) {
case "Player" :
this.player = new Player("res/images/player.png",
Double.parseDouble(row[1]),
Double.parseDouble(row[2]),
Integer.parseInt(row[3]));
break;
case "Zombie" :
this.zombie = new Zombie("res/images/zombie.png",
Double.parseDouble(row[1]),
Double.parseDouble(row[2]));
break;
case "Sandwich" :
this.sandwich = new Sandwich("res/images/sandwich.png",
Double.parseDouble(row[1]),
Double.parseDouble(row[2]));
break;
}
}
}
/**
......@@ -36,6 +67,47 @@ 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;
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)) {
// 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) {
// 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);
}
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());
}
}
......
public class Zombie extends Entity{
protected Zombie(String filename, double x, double y) {
super(filename, x, y);
}
}
\ No newline at end of file
File added
File added
Player,650,100,2
Zombie,300,200
Sandwich,500,400
\ No newline at end of file
File added
File added
File added
File added
File added
target/classes/images/background.png

14.4 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment