Skip to content
Snippets Groups Projects
Commit 45328d28 authored by Dennis Eisold's avatar Dennis Eisold
Browse files

Player is working

parent 4088440d
No related branches found
No related tags found
No related merge requests found
......@@ -4,12 +4,13 @@ public class GameField {
public int width, height;
private GameTile[][] field;
GameField(int width, int height, String stringField) {
GameField(int width, String stringField) {
this.width = width;
this.height = height;
field = new GameTile[width][height];
this.height = stringField.length() /width;
for (int y = 0; y < height; ++y) {
field = new GameTile[width][this.height];
for (int y = 0; y < this.height; ++y) {
for (int x = 0; x < width; ++x) {
switch (stringField.charAt(x + y * width)) {
......@@ -34,7 +35,6 @@ public class GameField {
for (int x = 0; x < this.field.length; x++) {
sb.append(this.field[x][y]);
}
sb.append("\n");
}
return sb.toString();
......
......@@ -3,16 +3,8 @@ package de.itstall;
public class GameMain {
public static void main(String[] args) {
String field = "########" +
"# #" +
"# #" +
"### #" +
"# #" +
"########";
GameField gf = new GameField(8, 6, field);
System.out.println(gf);
World w = new World();
System.out.println(w.draw());
}
}
package de.itstall;
public class Player extends GameTile {
Player(Position position) {
this.pos = position;
}
public String draw(String s, int worldLength) {
int index = pos.toIndex( worldLength );
char[] chars = s.toCharArray();
chars[index] = this.toString().charAt(0);
return new String(chars);
}
public void moveLeft() {
pos.setPosition(pos.getX() - 1, pos.getY());
}
public void moveRight() {
pos.setPosition(pos.getX() + 1, pos.getY());
}
public void moveUp() {
pos.setPosition(pos.getX(), pos.getY() - 1);
}
public void moveDown() {
pos.setPosition(pos.getX(), pos.getY() + 1);
}
@Override
public String toString() {
return "@";
}
}
......@@ -26,15 +26,11 @@ public class Position {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
public int toIndex(int width) {
return x + y * width;
}
}
package de.itstall;
public class World {
GameField field;
Player player;
public World() {
String canvas = "######### ## #### ## #########";
this.field = new GameField(8, canvas);
this.player = new Player(new Position(1, 1));
}
public String draw() {
StringBuilder sb = new StringBuilder();
String s = this.player.draw(this.field.toString(), this.field.width);
sb.append("\n\n\n\n\n\n\n");
for (int y = 0; y < this.field.height; y++) {
sb.append(s.substring(y * this.field.width, (y + 1) * this.field.width));
sb.append("\n");
}
return sb.toString();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment