win-condition + ghost killen + feared

master
leventius imperatus 2020-06-07 13:47:17 +02:00
parent 565092908a
commit f7ed737e23
6 changed files with 104 additions and 33 deletions

View File

@ -27,4 +27,4 @@
# ########## ## ########## #
# ########## ## ########## #
# #
############################
############################

BIN
assets/feared.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 505 B

Binary file not shown.

Binary file not shown.

View File

@ -33,7 +33,7 @@ public class Game {
@SuppressWarnings("unused")
private long fps = 60;
private int windowSizeX = 800; // Größe des Frame in x-Richtung
private int windowSizeX = 880; // Größe des Frame in x-Richtung
private int windowSizeY = 800; // Größe des Frame in y-Richtung
private int frameLocationX = 100; // Position des Frame auf dem Bildschirm in x-Richtung
@ -124,7 +124,8 @@ public class Game {
for (int j = 0; j < 35; j++) { // für jeden Brick
if (Map.bigpoints[j][i] != null) { // Damit kein Fehler auftritt wegen nicht vorhandenen Bricks
panel.add(Map.bigpoints[j][i]); // Der jeweilige Brick wird zum panel hinzugefügt
Map.bigpoints[j][i].setBounds(Map.bigpoints[j][i].xPos, Map.bigpoints[j][i].yPos, 20, 20); // Rendern des
Map.bigpoints[j][i].setBounds(Map.bigpoints[j][i].xPos, Map.bigpoints[j][i].yPos, 20, 20); // Rendern
// des
}
}
@ -134,8 +135,17 @@ public class Game {
lastT = System.nanoTime(); // delta time
while (true) { // Hauptschleife
frames ++;
frames++;
if (ghosts[0].feared == true) {
if (Ghost.fearedTimer != 0) {
Ghost.fearedTimer--;
} else {
for (int i = 0; i < ghosts.length; i++) {
ghosts[i].feared = false;
}
}
}
dt = System.nanoTime() - lastT; // delta time
lastT = System.nanoTime(); // delta time
@ -204,9 +214,8 @@ public class Game {
minn = HCost[j];
}
}
if ((frames % 2) != 0) {
for (int j = 0; j < 4; j++) {
for (int j = 0; j < 4; j++) {
if (HCost[j] == minn) {
if (j == 0) {
ghosts[i].right = true;
@ -235,10 +244,10 @@ public class Game {
ghosts[i].left = false;
ghosts[i].up = false;
break;
}
}
}
}
}
// Kollision von Pac-Man mit Bricks:
@ -283,11 +292,32 @@ public class Game {
Map.points[j][i] = null;
score++;
score += 150;
scoreLabel.setText(Integer.toString(score));
if(score == Map.maxScore) {
System.out.println("YOU WON!!!");
}
}
}
}
for (int i = 0; i < 35; i++) {
for (int j = 0; j < 35; j++) {
if (Map.bigpoints[j][i] != null) {
if (player.getPos('x', 0) < Map.bigpoints[j][i].xPos + 6
&& player.getPos('x', 0) > Map.bigpoints[j][i].xPos - 6
&& player.getPos('y', 0) < Map.bigpoints[j][i].yPos + 6
&& player.getPos('y', 0) > Map.bigpoints[j][i].yPos - 6) {
Map.bigpoints[j][i].setBounds(0, 0, 0, 0);
Map.bigpoints[j][i] = null;
score += 1000;
scoreLabel.setText(Integer.toString(score));
for (i = 0; i < 4; i++) {
ghosts[i].feared = true;
Ghost.fearedTimer = 1000;
}
}
}
}
@ -300,19 +330,25 @@ public class Game {
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.getPos('y', 0) > ghosts[i].getPos('y', 0) - 20)
if (ghosts[0].feared) {
player.lives--;
ghosts[i].setPos('x', Map.ghost_posX[i]);
ghosts[i].setPos('y', Map.ghost_posY[i]);
score += 1000;
} else {
player.lives--;
liveLabels[player.lives].setBounds(0, 0, 0, 0);
if (player.lives == 0) {
System.exit(0);
}
delay(200);
for (int j = 0; j < ghosts.length; j++) {
ghosts[j].setPos('x', Map.ghost_posX[j]);
ghosts[j].setPos('y', Map.ghost_posY[j]);
liveLabels[player.lives].setBounds(0, 0, 0, 0);
if (player.lives == 0) {
System.exit(0);
}
delay(200);
for (int j = 0; j < ghosts.length; j++) {
ghosts[j].setPos('x', Map.ghost_posX[j]);
ghosts[j].setPos('y', Map.ghost_posY[j]);
}
player.setPos('x', Map.pac_posX);
player.setPos('y', Map.pac_posY);
player.up = false;
@ -320,18 +356,42 @@ public class Game {
player.right = false;
player.left = false;
}
}
fps = 1000000000 / dt;
// System.out.println("fps: " + fps);
delay(delaytimer); // Ein delay zum Ende der Hauptschleife
if (frames % 100 == 0) {
boolean empty = true;
for (int i = 0; i < 35; i++) {
for (int j = 0; j < 35; j++) {
if (Map.points[j][i] != null) {
empty = false;
}
}
}
fps = 1000000000 / dt;
// System.out.println("fps: " + fps);
delay(delaytimer); // Ein delay zum Ende der Hauptschleife
for (int i = 0; i < 35; i++) {
for (int j = 0; j < 35; j++) {
if (Map.bigpoints[j][i] != null) {
empty = false;
}
}
}
if (empty) {
scoreLabel.setText("YOU WON");
delay(1000);
System.exit(0);
}
}
}
}
// Methode zum verzögern (warten) in ms
public void delay(int time) {

View File

@ -11,8 +11,10 @@ public class Ghost extends Picture { // Die
public boolean down;
public boolean left;
public boolean right;
private int HCost;
public boolean feared = false;
public static int fearedTimer;
private double HCost;
private int type;
public Ghost(int type) {
@ -40,6 +42,7 @@ public class Ghost extends Picture { // Die
}
public int getHCost(Player player, int mod_x, int mod_y) {
if (!feared) {
if (type == 0) {
HCost = (int) Math.sqrt(Math.pow((((xPos - 10) / 20) + mod_x) - ((player.getPos('x', 0) - 10) / 20), 2)
@ -54,9 +57,17 @@ public class Ghost extends Picture { // Die
HCost = (int) Math.sqrt(Math.pow((((xPos - 10) / 20) + mod_x) - ((player.getPos('x', 0) - 10) / 20), 2)
+ Math.pow((((yPos - 10) / 20) + mod_y) - ((player.getPos('y', 0) + 100 - 10) / 20), 2));
}
return HCost;
}
else {
HCost = 10 * (1 / (Math.sqrt(Math.pow((((xPos - 10) / 20) + mod_x) - ((player.getPos('x', 0) - 10) / 20), 2)
+ Math.pow((((yPos - 10) / 20) + mod_y) - ((player.getPos('y', 0) - 10) / 20), 2))));
}
return (int) HCost;
}
public int getPos(char coordinate, long dt) { // Hier kommt die zuvor erwähnte delta time ins Spiel
if (coordinate == 'x') { // Auslesen der 'x' - Koordinate: