Skip to content
Snippets Groups Projects
Commit 72d8f9dc authored by Xincheng Miao's avatar Xincheng Miao
Browse files

Project 1 for SWEN20003

parents
Branches
No related tags found
No related merge requests found
pom.xml 0 → 100644
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>au.edu.unimelb.cis</groupId>
<artifactId>bagel</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<sourceDirectory>src/</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>io.github.eleanor-em</groupId>
<artifactId>bagel</artifactId>
<version>1.9.3</version>
</dependency>
</dependencies>
</project>
res/images/background.png

9.55 KiB

res/images/gatherer.png

590 B

res/images/tree.png

2.06 KiB

Tree,256,192
Tree,256,512
Tree,704,192
Tree,704,512
Gatherer,512,384
Gatherer,384,512
import java.io.*;
public class ReadCSV {
private String file;
private String[] nameList;
private int[] xCoordList;
private int[] yCoordList;
private static int lineNum;
// constructor for this class
public ReadCSV(String file) {
this.file = file;
lineNum = countLine();
nameList = new String[lineNum];
xCoordList = new int[lineNum];
yCoordList = new int[lineNum];
recordLine();
}
// getters for this class
public String[] getNames() { return nameList; }
public int[] getXCoords() { return xCoordList; }
public int[] getYCoords() { return yCoordList; }
public int getLineNum() { return lineNum; }
// method used for counting the number of lines
public int countLine() {
int count = 0;
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String text;
while ((text = br.readLine()) != null) {
count++;
}
} catch (Exception e) {
e.printStackTrace();
}
return count;
}
// method uesd for recording the data in each line separately
public void recordLine() {
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String text;
int count = 0;
while ((text = br.readLine()) != null) {
String cells[] = text.split(",");
nameList[count] = cells[0];
xCoordList[count] = Integer.parseInt(cells[1]);
yCoordList[count] = Integer.parseInt(cells[2]);
count++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
\ No newline at end of file
import bagel.*;
public class ShadowLife extends AbstractGame {
private Image gatherer;
private Image tree;
private Image background;
private String[] nameList;
private int[] xCoordList;
private int[] yCoordList;
private ReadCSV file;
private long time;
private int height;
private int width;
// constructor for ShadowLife
public ShadowLife() {
super(1024, 768, "Gather!");
gatherer = new Image("res/images/gatherer.png");
tree = new Image("res/images/tree.png");
background = new Image("res/images/background.png");
ReadCSV readFile = new ReadCSV("res/worlds/test.csv");
height = Window.getHeight();
width = Window.getWidth();
this.xCoordList = readFile.getXCoords();
this.yCoordList = readFile.getYCoords();
this.nameList = readFile.getNames();
file = readFile;
time = System.currentTimeMillis();
}
public static void main(String[] args) {
ShadowLife game = new ShadowLife();
game.run();
}
// update the simulation
public void update(Input input) {
boolean moved = false;
if (input.wasPressed(Keys.ESCAPE)) {
Window.close();
}
// draw background and actors
background.drawFromTopLeft(0, 0);
for (int i = 0; i < file.getLineNum(); i++) {
if (nameList[i].equals("Tree")) {
tree.draw(xCoordList[i], yCoordList[i]);
}
if (nameList[i].equals("Gatherer")) {
// move for every 1 tick
moved = false;
// 1 tick cannot be exactly 500, so after testing, choose the range 460-500
if (System.currentTimeMillis() - time > 460 &&
System.currentTimeMillis() - time < 500) {
randomMove(i);
moved = true;
}
gatherer.draw(xCoordList[i], yCoordList[i]);
}
}
if (moved) {
time = System.currentTimeMillis();
}
}
// method used for the random move of gatherers
public void randomMove(int index) {
// choose a random direction and each direction has 25% probability to occur
double direction = Math.random();
// sort the directions, 0-0.25 for right, 0.25-0.5 for up, 0.5-0.75 for down and 0.75-1 for left
if (direction < 0.25) {
xCoordList[index] += (width / 64);
} else if (direction > 0.75) {
xCoordList[index] -= (width / 64);
} else if (direction > 0.25 && direction < 0.5) {
yCoordList[index] += (height / 64);
} else {
yCoordList[index] -= (height / 64);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment