Pac-Man/src/Map.java

93 lines
3.2 KiB
Java
Raw Normal View History

2020-02-17 16:29:50 +01:00
import java.io.BufferedReader;
import java.io.FileReader;
// Diese Klasse ist zust<73>ndig f<>r das Laden der Karte
// In der Textdatei map.txt enthalten im "assets" - Ordner kann man anhand von '#' Positionen von Bricks festlegen. Dort wo sich kein '#' befindet, ist also eine L<>cke.
public class Map {
public static Brick bricks[][] = new Brick[35][35]; // Zweidimensionales Array f<>r alle Bricks
public static Point points[][] = new Point[35][35];
2020-06-06 16:22:04 +02:00
public static BigPoint bigpoints[][] = new BigPoint[35][35];
2020-06-06 13:32:49 +02:00
// Zweidimensionale Arrays siehe
// https://de.wikipedia.org/wiki/Feld_(Datentyp)#Mehrdimensional_/_in-sich-mehrdimensional
2020-02-17 16:29:50 +01:00
BufferedReader reader; // reader zum Einlesen der Text Datei
2020-04-30 21:18:21 +02:00
2020-02-17 16:29:50 +01:00
Player player;
Ghost ghosts[];
public static int ghost_posX[] = new int[4];
public static int ghost_posY[] = new int[4];
2020-06-06 15:23:14 +02:00
public static int maxScore;
public static int pac_posX;
public static int pac_posY;
2020-02-17 16:29:50 +01:00
String line; // String in dem eingelsene Zeilen der Datei gespeichert werden
public Map(Player player, Ghost ghosts[]) { // Erstellen des Konstruktors
2020-04-30 21:18:21 +02:00
2020-02-17 16:29:50 +01:00
this.ghosts = ghosts;
this.player = player;
}
2020-04-30 21:18:21 +02:00
public void mapping() {
2020-02-17 16:29:50 +01:00
try {
reader = new BufferedReader(new FileReader("assets/map.txt")); // Einlesen der .txt Datei
2020-04-30 21:18:21 +02:00
2020-02-17 16:29:50 +01:00
for (int i = 0; i < 35; i++) { // f<>r die ersten 35 Zeilen der Datei:
String line = reader.readLine(); // Einlesen der jeweiligen Zeile
for (int j = 0; j < 35; j++) { // f<>r die ersten 35 Zeichen der jeweiligen Zeile
try {
if (line.charAt(j) == '#') { // Erkennen ob sich dort ein '#' befindet
bricks[i][j] = new Brick(10 + 20 * j, 10 + 20 * i); // wenn ja, soll an der Stelle ein neuer
// Brick erstellt werden
} else if (line.charAt(j) == 'P') {
player.setPos('x', 10 + 20 * j);
player.setPos('y', 10 + 20 * i);
pac_posX = 10 + 20 * j;
pac_posY = 10 + 20 * i;
2020-02-17 16:29:50 +01:00
} else if (line.charAt(j) == '0') {
ghosts[0].setPos('x', 10 + 20 * j);
ghosts[0].setPos('y', 10 + 20 * i);
ghost_posX[0] = 10 + 20 * j;
ghost_posY[0] = 10 + 20 * i;
2020-02-17 16:29:50 +01:00
} else if (line.charAt(j) == '1') {
ghosts[1].setPos('x', 10 + 20 * j);
ghosts[1].setPos('y', 10 + 20 * i);
ghost_posX[1] = 10 + 20 * j;
ghost_posY[1] = 10 + 20 * i;
2020-02-17 16:29:50 +01:00
} else if (line.charAt(j) == '2') {
ghosts[2].setPos('x', 10 + 20 * j);
ghosts[2].setPos('y', 10 + 20 * i);
ghost_posX[2] = 10 + 20 * j;
ghost_posY[2] = 10 + 20 * i;
2020-02-17 16:29:50 +01:00
} else if (line.charAt(j) == '3') {
ghosts[3].setPos('x', 10 + 20 * j);
ghosts[3].setPos('y', 10 + 20 * i);
ghost_posX[3] = 10 + 20 * j;
ghost_posY[3] = 10 + 20 * i;
2020-06-06 13:32:49 +02:00
} else if (line.charAt(j) == ' ') {
points[i][j] = new Point(10 + 20 * j, 10 + 20 * i);
2020-06-06 15:23:14 +02:00
maxScore++;
2020-06-06 16:22:04 +02:00
} else if (line.charAt(j) == '.') {
bigpoints[i][j] = new BigPoint(10 + 20 * j, 10 + 20 * i);
2020-04-30 21:18:21 +02:00
}
2020-02-17 16:29:50 +01:00
} catch (Exception e) {
2020-04-30 21:18:21 +02:00
2020-02-17 16:29:50 +01:00
}
}
}
} catch (Exception e) { // Falls etwas mit dem Einlesen der Datei schiefl<66>uft:
System.out.println(
"There seems to be an error with the file of the map. Pls use hashes to indicate positions of Walls. ERROR: "
+ e);
}
}
}