2020-02-17 16:29:50 +01:00
|
|
|
|
import java.awt.Color;
|
|
|
|
|
|
2020-06-04 19:54:41 +02:00
|
|
|
|
import java.util.*;
|
|
|
|
|
|
2020-02-17 16:29:50 +01:00
|
|
|
|
import javax.swing.JFrame;
|
|
|
|
|
import javax.swing.JPanel;
|
|
|
|
|
|
2020-06-04 19:54:41 +02:00
|
|
|
|
import java.lang.Math;
|
|
|
|
|
|
2020-02-17 16:29:50 +01:00
|
|
|
|
// In dieser Klasse findet der gr<67><72>te Teil des Spiels statt:
|
|
|
|
|
// Rendern von Map, Geistern, Pac-Man etc.
|
|
|
|
|
// <20>berpr<70>fung von Kollisionen verschiedener Elemente (der komplizierteste Teil des Programms)
|
|
|
|
|
|
|
|
|
|
public class Game {
|
|
|
|
|
|
|
|
|
|
// Deklaration der sichtbaren Elemente
|
|
|
|
|
JFrame frame;
|
|
|
|
|
JPanel panel;
|
|
|
|
|
Player player;
|
|
|
|
|
Ghost ghosts[];
|
|
|
|
|
Map map;
|
|
|
|
|
|
|
|
|
|
// Delta time: siehe https://en.wikipedia.org/wiki/Delta_timing
|
|
|
|
|
private long dt;
|
|
|
|
|
private long lastT;
|
2020-05-21 16:12:32 +02:00
|
|
|
|
|
2020-06-04 19:54:41 +02:00
|
|
|
|
private int delaytimer = 10;
|
2020-05-21 16:12:32 +02:00
|
|
|
|
|
2020-05-11 23:14:09 +02:00
|
|
|
|
@SuppressWarnings("unused")
|
2020-05-11 22:50:09 +02:00
|
|
|
|
private long fps = 60;
|
2020-02-17 16:29:50 +01:00
|
|
|
|
|
|
|
|
|
private int windowSizeX = 800; // Gr<47><72>e des Frame in x-Richtung
|
|
|
|
|
private int windowSizeY = 800; // Gr<47><72>e des Frame in y-Richtung
|
|
|
|
|
|
|
|
|
|
private int frameLocationX = 100; // Position des Frame auf dem Bildschirm in x-Richtung
|
|
|
|
|
private int frameLocationY = 100; // Position des Frame auf dem Bildschirm in x-Richtung
|
|
|
|
|
// oben links (0|0)
|
|
|
|
|
// nach rechts --> x wird gr<67><72>er
|
|
|
|
|
// nach unten --> y wird gr<67><72>er
|
|
|
|
|
|
2020-06-04 19:54:41 +02:00
|
|
|
|
int minn;
|
|
|
|
|
int[] HCost = new int[4];
|
|
|
|
|
|
2020-02-17 16:29:50 +01:00
|
|
|
|
public Game() { // Erstellen des Konstruktors (Was soll passieren, sobald dieses Klasse
|
|
|
|
|
// aufgerufen wird?)
|
|
|
|
|
|
|
|
|
|
frame = new JFrame(); // Fenster
|
|
|
|
|
|
|
|
|
|
panel = new JPanel(); // darauf werden alle sichtbaren Elemente gespeichert
|
|
|
|
|
|
2020-06-04 19:54:41 +02:00
|
|
|
|
ghosts = new Ghost[4]; // 4 Geister
|
2020-05-21 16:12:32 +02:00
|
|
|
|
|
2020-06-04 19:54:41 +02:00
|
|
|
|
boolean[][] ghost_possible = new boolean[ghosts.length][4];
|
2020-02-17 16:29:50 +01:00
|
|
|
|
|
|
|
|
|
player = new Player(); // Pac-Man
|
|
|
|
|
|
|
|
|
|
panel.add(player); // Pac-Man wird dem Panel hinzugef<65>gt
|
|
|
|
|
frame.addKeyListener(player); // KeyListener wird hinzugef<65>gt, man kann nun Pac-Maan mit der tastatur steuern
|
|
|
|
|
|
|
|
|
|
// Einstellen des Frame bez<65>glich Gr<47><72>e und Position
|
|
|
|
|
frame.setSize(windowSizeX, windowSizeY);
|
|
|
|
|
frame.setLocation(frameLocationX, frameLocationY);
|
|
|
|
|
|
|
|
|
|
frame.setVisible(false);
|
|
|
|
|
frame.setTitle("Pac-Man"); // Der Titel des Frame wird auf "Pac-Man" gesetzt
|
|
|
|
|
frame.setResizable(false); // Man kann die Gr<47><72>e des Frame nicht ver<65>ndern
|
|
|
|
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Wenn der Frame geschlossen wird, wird auch das Programm
|
|
|
|
|
// beendet
|
|
|
|
|
|
|
|
|
|
// Der Inhalt des Panels wird auf dem Frame angezeigt:
|
|
|
|
|
frame.setContentPane(panel);
|
|
|
|
|
frame.getContentPane().setLayout(null);
|
|
|
|
|
|
|
|
|
|
panel.setBackground(Color.BLACK); // Der Hintergrund ist schwarz
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < ghosts.length; i++) { // f<>r jeden Geist:
|
|
|
|
|
ghosts[i] = new Ghost(i); // Erstellen des jeweiligen Geistes
|
|
|
|
|
panel.add(ghosts[i]); // Hinzuf<75>gen zum Panel des jeiligen Geistes
|
|
|
|
|
ghosts[i].setBounds(ghosts[i].getPos('x', 0), ghosts[i].getPos('y', 0), 20, 20);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
map = new Map(player, ghosts); // Map auf der gespielt wird
|
|
|
|
|
map.mapping();
|
|
|
|
|
|
|
|
|
|
player.setBounds(player.getPos('x', 0), player.getPos('y', 0), 20, 20); // Pac-Man wird das erste Mal gerendert
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 35; i++) { // f<>r jeden Brick
|
|
|
|
|
for (int j = 0; j < 35; j++) { // f<>r jeden Brick
|
|
|
|
|
if (Map.bricks[j][i] != null) { // Damit kein Fehler auftritt wegen nicht vorhandenen Bricks
|
|
|
|
|
panel.add(Map.bricks[j][i]); // Der jeweilige Brick wird zum panel hinzugef<65>gt
|
|
|
|
|
Map.bricks[j][i].setBounds(Map.bricks[j][i].xPos, Map.bricks[j][i].yPos, 20, 20); // Rendern des
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
frame.setVisible(true);
|
|
|
|
|
|
|
|
|
|
lastT = System.nanoTime(); // delta time
|
|
|
|
|
|
|
|
|
|
while (true) { // Hauptschleife
|
|
|
|
|
|
|
|
|
|
dt = System.nanoTime() - lastT; // delta time
|
|
|
|
|
lastT = System.nanoTime(); // delta time
|
|
|
|
|
|
|
|
|
|
// Zu Beginn jeden Schleifendurchlaufs wird die Position von Pac-Man neu
|
|
|
|
|
// gesetzt:
|
|
|
|
|
player.setLocation(player.getPos('x', dt), player.getPos('y', dt));
|
|
|
|
|
|
|
|
|
|
// Aktualiesieren der Positionen der Geister:
|
2020-06-04 19:54:41 +02:00
|
|
|
|
//for (int i = 0; i < ghosts.length; i++) {
|
|
|
|
|
// ghosts[i].setLocation(ghosts[i].getPos('x', dt), ghosts[i].getPos('y', dt));
|
|
|
|
|
//}
|
2020-02-17 16:29:50 +01:00
|
|
|
|
|
2020-05-21 16:12:32 +02:00
|
|
|
|
for (int i = 0; i < ghosts.length; i++) {
|
2020-06-04 19:54:41 +02:00
|
|
|
|
|
|
|
|
|
ghost_possible[i][0] = false;
|
|
|
|
|
ghost_possible[i][1] = false;
|
|
|
|
|
ghost_possible[i][2] = false;
|
|
|
|
|
ghost_possible[i][3] = false;
|
|
|
|
|
|
|
|
|
|
if ((Map.bricks[conv(ghosts[i].yPos)][conv(ghosts[i].xPos) + 1] == null && Map.bricks[conv(ghosts[i].yPos + 19)][conv(ghosts[i].xPos) + 1] == null) || (ghosts[i].xPos + 10) % 20 != 0 ){
|
|
|
|
|
ghost_possible[i][0] = true;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if ((Map.bricks[conv(ghosts[i].yPos)][conv(ghosts[i].xPos) - 1] == null && Map.bricks[conv(ghosts[i].yPos + 19)][conv(ghosts[i].xPos) - 1] == null) || (ghosts[i].xPos + 10) % 20 != 0 ){
|
|
|
|
|
ghost_possible[i][1] = true;
|
2020-05-21 16:12:32 +02:00
|
|
|
|
|
|
|
|
|
}
|
2020-06-04 19:54:41 +02:00
|
|
|
|
if ((Map.bricks[conv(ghosts[i].yPos) - 1][conv(ghosts[i].xPos)] == null && Map.bricks[conv(ghosts[i].yPos) - 1][conv(ghosts[i].xPos + 19)] == null) || (ghosts[i].yPos + 10) % 20 != 0 ){
|
|
|
|
|
ghost_possible[i][2] = true;
|
2020-05-21 16:12:32 +02:00
|
|
|
|
}
|
2020-06-04 19:54:41 +02:00
|
|
|
|
if ((Map.bricks[conv(ghosts[i].yPos) + 1][conv(ghosts[i].xPos)] == null && Map.bricks[conv(ghosts[i].yPos) + 1][conv(ghosts[i].xPos + 19)] == null) || (ghosts[i].yPos + 10) % 20 != 0 ){
|
|
|
|
|
ghost_possible[i][3] = true;
|
2020-05-21 16:12:32 +02:00
|
|
|
|
}
|
2020-06-04 19:54:41 +02:00
|
|
|
|
|
|
|
|
|
System.out.println(HCost[0] + " " + HCost[1] + " " + HCost[2] + " " + HCost[3]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HCost[0] = ghosts[i].getHCost(player, 1, 0);
|
|
|
|
|
HCost[1] = ghosts[i].getHCost(player, -1, 0);
|
|
|
|
|
HCost[2] = ghosts[i].getHCost(player, 0, -1);
|
|
|
|
|
HCost[3] = ghosts[i].getHCost(player, 0, 1);
|
|
|
|
|
//
|
|
|
|
|
int minn = 10000;
|
|
|
|
|
|
|
|
|
|
if (ghosts[i].up == true) {
|
|
|
|
|
HCost[3] = 10000;
|
2020-05-21 16:12:32 +02:00
|
|
|
|
}
|
2020-06-04 19:54:41 +02:00
|
|
|
|
if (ghosts[i].down == true) {
|
|
|
|
|
HCost[2] = 10000;
|
|
|
|
|
}
|
|
|
|
|
if (ghosts[i].left == true) {
|
|
|
|
|
HCost[0] = 10000;
|
|
|
|
|
}
|
|
|
|
|
if (ghosts[i].right == true) {
|
|
|
|
|
HCost[1] = 10000;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-21 16:12:32 +02:00
|
|
|
|
|
2020-06-04 19:54:41 +02:00
|
|
|
|
for (int j = 0; j < 4; j++) {
|
|
|
|
|
if (ghost_possible[i][j] == false) {
|
|
|
|
|
HCost[j] = 10500;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-21 16:12:32 +02:00
|
|
|
|
|
2020-06-04 19:54:41 +02:00
|
|
|
|
for (int j = 0; j < 4; j++) {
|
|
|
|
|
if (minn > HCost[j]) {
|
|
|
|
|
minn = HCost[j];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//System.out.println(minn);
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
for (int j = 0; j < 4; j++) {
|
|
|
|
|
if (HCost[j] == minn) {
|
|
|
|
|
if (j == 0) {
|
|
|
|
|
ghosts[i].right = true;
|
|
|
|
|
ghosts[i].left = false;
|
|
|
|
|
ghosts[i].down = false;
|
|
|
|
|
ghosts[i].up = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (j == 1) {
|
|
|
|
|
ghosts[i].left = true;
|
|
|
|
|
ghosts[i].right = false;
|
|
|
|
|
ghosts[i].down = false;
|
|
|
|
|
ghosts[i].up = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (j == 2) {
|
|
|
|
|
ghosts[i].up = true;
|
|
|
|
|
ghosts[i].right = false;
|
|
|
|
|
ghosts[i].left = false;
|
|
|
|
|
ghosts[i].down = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (j == 3) {
|
|
|
|
|
ghosts[i].down = true;
|
|
|
|
|
ghosts[i].right = false;
|
|
|
|
|
ghosts[i].left = false;
|
|
|
|
|
ghosts[i].up = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-05-21 16:12:32 +02:00
|
|
|
|
}
|
2020-02-17 16:29:50 +01:00
|
|
|
|
|
2020-06-04 19:54:41 +02:00
|
|
|
|
|
2020-02-17 16:29:50 +01:00
|
|
|
|
// Kollision von Pac-Man mit Bricks:
|
|
|
|
|
for (int i = 0; i < 35; i++) { // f<>r jeden Brick
|
|
|
|
|
for (int j = 0; j < 35; j++) { // f<>r jeden Brick
|
|
|
|
|
if (Map.bricks[j][i] != null) { // Damit kein Fehler auftritt wegen nicht vorhandenen Bricks
|
|
|
|
|
if (player.getPos('x', 0) < Map.bricks[j][i].xPos + 20
|
|
|
|
|
&& player.getPos('x', 0) > Map.bricks[j][i].xPos - 20
|
|
|
|
|
&& player.getPos('y', 0) < Map.bricks[j][i].yPos + 20
|
|
|
|
|
&& player.getPos('y', 0) > Map.bricks[j][i].yPos - 20) {
|
|
|
|
|
if (player.left) {
|
|
|
|
|
player.setPos('x', Map.bricks[j][i].xPos + 20);
|
2020-05-10 14:25:46 +02:00
|
|
|
|
player.setPos('y', Map.bricks[j][i].yPos);
|
2020-02-17 16:29:50 +01:00
|
|
|
|
player.left = false;
|
|
|
|
|
} else if (player.right) {
|
|
|
|
|
player.setPos('x', Map.bricks[j][i].xPos - 20);
|
2020-05-10 14:25:46 +02:00
|
|
|
|
player.setPos('y', Map.bricks[j][i].yPos);
|
2020-02-17 16:29:50 +01:00
|
|
|
|
player.right = false;
|
|
|
|
|
} else if (player.up) {
|
|
|
|
|
player.setPos('y', Map.bricks[j][i].yPos + 20);
|
2020-05-10 14:25:46 +02:00
|
|
|
|
player.setPos('x', Map.bricks[j][i].xPos);
|
2020-02-17 16:29:50 +01:00
|
|
|
|
player.up = false;
|
|
|
|
|
} else if (player.down) {
|
|
|
|
|
player.setPos('y', Map.bricks[j][i].yPos - 20);
|
2020-05-10 14:25:46 +02:00
|
|
|
|
player.setPos('x', Map.bricks[j][i].xPos);
|
2020-02-17 16:29:50 +01:00
|
|
|
|
player.down = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-21 16:12:32 +02:00
|
|
|
|
|
2020-05-10 14:25:46 +02:00
|
|
|
|
player.calcDir(0); // Berechnen wo Pac-Man als n<>chstes hin soll
|
2020-02-17 16:29:50 +01:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < ghosts.length; i++) {
|
2020-06-04 19:54:41 +02:00
|
|
|
|
ghosts[i].setLocation(ghosts[i].getPos('x', dt), ghosts[i].getPos('y', dt));
|
2020-02-17 16:29:50 +01:00
|
|
|
|
if (player.getPos('x', 0) < ghosts[i].getPos('x', 0) + 20
|
|
|
|
|
&& player.getPos('x', 0) > ghosts[i].getPos('x', 0) - 20
|
|
|
|
|
&& player.getPos('y', 0) < ghosts[i].getPos('y', 0) + 20
|
|
|
|
|
&& player.getPos('y', 0) > ghosts[i].getPos('y', 0) - 20) {
|
|
|
|
|
|
|
|
|
|
player.lives--;
|
|
|
|
|
System.out.println(player.lives);
|
|
|
|
|
map.mapping();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-11 22:50:09 +02:00
|
|
|
|
fps = 1000000000 / dt;
|
2020-05-21 16:12:32 +02:00
|
|
|
|
|
2020-05-11 22:50:09 +02:00
|
|
|
|
// System.out.println("fps: " + fps + " | delaytimer: " + delaytimer);
|
2020-05-21 16:12:32 +02:00
|
|
|
|
|
2020-05-11 22:50:09 +02:00
|
|
|
|
delay(delaytimer); // Ein delay zum Ende der Hauptschleife
|
2020-05-21 16:12:32 +02:00
|
|
|
|
|
2020-02-17 16:29:50 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Methode zum verz<72>gern (warten) in ms
|
|
|
|
|
public void delay(int time) {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(time);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-04 19:54:41 +02:00
|
|
|
|
private int conv(float a) { //90.0 --> 4
|
|
|
|
|
int b = (int) (a - 10 )/ 20;
|
|
|
|
|
return b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-02-17 16:29:50 +01:00
|
|
|
|
}
|