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

Initialize src folder from my project 1

parent df38effd
No related branches found
No related tags found
No related merge requests found
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/../../../../../../../../../:\Users\14159\Desktop\study\oosd\project2\yiya-project-2\.idea/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager">
<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="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/yiya-project-2.iml" filepath="$PROJECT_DIR$/yiya-project-2.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
File added
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) {
this.energy.add(amount);
}
protected void eat(Sandwich sandwich) {
this.updateEnergy(+5);
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.Colour;
import bagel.util.Vector2;
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 String filename = "res/IO/environment.csv";
private final Image background = new Image("res/images/background.png");
private Player player;
private Zombie zombie;
private Sandwich sandwich;
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
private static DecimalFormat df = new DecimalFormat("0.00");
public static void printInfo(double x, double y, int e) {
System.out.println(df.format(x) + "," + df.format(y) + "," + e);
}
public ShadowTreasure() throws IOException {
this.loadEnvironment(filename);
// Add code to initialize other attributes as needed
}
/**
* Load from input file
*/
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;
}
}
printInfo(player.getX(), player.getY(), player.getEnergy());
}
/**
* Performs a state update.
*/
@Override
public void update(Input input) {
// Logic to update the game, as per specification must go here
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 playerToSandwich = player.getPosition().distanceTo(sandwich.getPosition());
double distDiff = playerToZombie - playerToSandwich;
int energy = player.getEnergy();
Vector2 direction;
if (sandwich.isEaten() || (distDiff <= 0 && energy >= 3)) {
// if (sandwich.isEaten() || 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);
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
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);
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);
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 = 0;
printInfo(player.getX(), player.getY(), player.getEnergy());
}
/**
* The entry point for the program.
*/
public static void main(String[] args) throws IOException {
ShadowTreasure game = new ShadowTreasure();
game.run();
}
}
public class Zombie extends Entity{
protected Zombie(String filename, double x, double y) {
super(filename, x, y);
}
}
\ No newline at end of file
<?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$/res" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" type="java-resource" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ 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