score hinzugefügt

master
Lenz Wiechers 2020-06-06 14:51:42 +02:00
parent ff70f1d8f2
commit 870424d1c5
4 changed files with 39 additions and 39 deletions

Binary file not shown.

Binary file not shown.

View File

@ -1,12 +1,10 @@
import java.awt.Color; import java.awt.Color;
import java.awt.Font;
import java.util.*;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
import java.lang.Math;
// In dieser Klasse findet der größte Teil des Spiels statt: // In dieser Klasse findet der größte Teil des Spiels statt:
// Rendern von Map, Geistern, Pac-Man etc. // Rendern von Map, Geistern, Pac-Man etc.
// Überprüfung von Kollisionen verschiedener Elemente (der komplizierteste Teil des Programms) // Überprüfung von Kollisionen verschiedener Elemente (der komplizierteste Teil des Programms)
@ -24,7 +22,11 @@ public class Game {
private long dt; private long dt;
private long lastT; private long lastT;
private int delaytimer = 10; private int score = 0;
private JLabel scoreLabel;
private int delaytimer = 13;
@SuppressWarnings("unused") @SuppressWarnings("unused")
private long fps = 60; private long fps = 60;
@ -54,6 +56,18 @@ public class Game {
player = new Player(); // Pac-Man player = new Player(); // Pac-Man
scoreLabel = new JLabel(Integer.toString(score));
panel.add(scoreLabel);
scoreLabel.setForeground(Color.WHITE);
scoreLabel.setBounds(710, -5, 500, 50);
Font f = new Font("Consolas", Font.BOLD, 25);
scoreLabel.setFont(f);
panel.add(player); // Pac-Man wird dem Panel hinzugefügt panel.add(player); // Pac-Man wird dem Panel hinzugefügt
frame.addKeyListener(player); // KeyListener wird hinzugefügt, man kann nun Pac-Maan mit der tastatur steuern frame.addKeyListener(player); // KeyListener wird hinzugefügt, man kann nun Pac-Maan mit der tastatur steuern
@ -115,7 +129,6 @@ public class Game {
// gesetzt: // gesetzt:
player.setLocation(player.getPos('x', dt), player.getPos('y', dt)); player.setLocation(player.getPos('x', dt), player.getPos('y', dt));
for (int i = 0; i < ghosts.length; i++) { for (int i = 0; i < ghosts.length; i++) {
ghost_possible[i][0] = false; ghost_possible[i][0] = false;
@ -146,7 +159,6 @@ public class Game {
ghost_possible[i][3] = true; ghost_possible[i][3] = true;
} }
HCost[0] = ghosts[i].getHCost(player, 1, 0); HCost[0] = ghosts[i].getHCost(player, 1, 0);
HCost[1] = ghosts[i].getHCost(player, -1, 0); HCost[1] = ghosts[i].getHCost(player, -1, 0);
HCost[2] = ghosts[i].getHCost(player, 0, -1); HCost[2] = ghosts[i].getHCost(player, 0, -1);
@ -179,7 +191,6 @@ public class Game {
} }
} }
for (int j = 0; j < 4; j++) { for (int j = 0; j < 4; j++) {
if (HCost[j] == minn) { if (HCost[j] == minn) {
if (j == 0) { if (j == 0) {
@ -252,14 +263,19 @@ public class Game {
&& player.getPos('x', 0) > Map.points[j][i].xPos - 5 && player.getPos('x', 0) > Map.points[j][i].xPos - 5
&& player.getPos('y', 0) < Map.points[j][i].yPos + 5 && player.getPos('y', 0) < Map.points[j][i].yPos + 5
&& player.getPos('y', 0) > Map.points[j][i].yPos - 5) { && player.getPos('y', 0) > Map.points[j][i].yPos - 5) {
Map.points[j][i].setBounds(0, 0, 0, 0); Map.points[j][i].setBounds(0, 0, 0, 0);
Map.points[j][i] = null;
score++;
scoreLabel.setText(Integer.toString(score));
} }
} }
} }
} }
player.calcDir(0); // Berechnen wo Pac-Man als nächstes hin soll player.calcDir(0); // Berechnen wo Pac-Man als nächstes hin soll
for (int i = 0; i < ghosts.length; i++) { for (int i = 0; i < ghosts.length; i++) {
@ -272,8 +288,8 @@ public class Game {
player.lives--; player.lives--;
System.out.println(player.lives); System.out.println(player.lives);
delay(200); delay(200);
for(int j = 0; j < ghosts.length; j++) { for (int j = 0; j < ghosts.length; j++) {
ghosts[j].setPos('x', Map.ghost_posX[j]); ghosts[j].setPos('x', Map.ghost_posX[j]);
ghosts[j].setPos('y', Map.ghost_posY[j]); ghosts[j].setPos('y', Map.ghost_posY[j]);
player.setPos('x', Map.pac_posX); player.setPos('x', Map.pac_posX);
player.setPos('y', Map.pac_posY); player.setPos('y', Map.pac_posY);
@ -282,12 +298,12 @@ public class Game {
player.right = false; player.right = false;
player.left = false; player.left = false;
} }
} }
} }
fps = 1000000000 / dt; fps = 1000000000 / dt;
// System.out.println("fps: " + fps + " | delaytimer: " + delaytimer); // System.out.println("fps: " + fps);
delay(delaytimer); // Ein delay zum Ende der Hauptschleife delay(delaytimer); // Ein delay zum Ende der Hauptschleife

View File

@ -4,16 +4,14 @@ public class Ghost extends Picture { // Die
private static final long serialVersionUID = -5352006665147359473L; private static final long serialVersionUID = -5352006665147359473L;
public int xPos; public float xPos;
public int yPos; public float yPos;
public boolean up; public boolean up;
public boolean down; public boolean down;
public boolean left; public boolean left;
public boolean right; public boolean right;
//private float speed = 0.00000007f;
private int HCost; private int HCost;
public Ghost(int index) { public Ghost(int index) {
@ -32,8 +30,6 @@ public class Ghost extends Picture { // Die
} }
public void setPos(char coordinate, int newPos) { public void setPos(char coordinate, int newPos) {
if (coordinate == 'x') { if (coordinate == 'x') {
xPos = newPos; xPos = newPos;
@ -41,43 +37,31 @@ public class Ghost extends Picture { // Die
yPos = newPos; yPos = newPos;
} }
} }
/*
public void setDirection(String dir) {
direction = dir;
}
public String getDirection() {
return direction;
}
*/
public int getHCost(Player player, int mod_x, int mod_y) { public int getHCost(Player player, int mod_x, int mod_y) {
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) - 10)/20), 2)); 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) - 10) / 20), 2));
return HCost; return HCost;
} }
public int getPos(char coordinate, long dt) { // Hier kommt die zuvor erwähnte delta time ins Spiel public int getPos(char coordinate, long dt) { // Hier kommt die zuvor erwähnte delta time ins Spiel
if (coordinate == 'x') { // Auslesen der 'x' - Koordinate: if (coordinate == 'x') { // Auslesen der 'x' - Koordinate:
if (left && dt != 0) { if (left && dt != 0) {
// xPos -= speed * dt;
xPos -= 1; xPos -= 1;
} else if (right && dt != 0) { } else if (right && dt != 0) {
// xPos += speed * dt;
xPos += 1; xPos += 1;
} }
return (int) xPos; return (int) xPos;
} else if (coordinate == 'y') { // Auslesen der 'y' - Koordinate: } else if (coordinate == 'y') { // Auslesen der 'y' - Koordinate:
if (down && dt != 0) { if (down && dt != 0) {
// yPos += speed * dt;
yPos += 1; yPos += 1;
} else if (up && dt != 0) { } else if (up && dt != 0) {
// yPos -= speed * dt;
yPos -= 1; yPos -= 1;
} }
return (int) yPos; //(int) return (int) yPos; // (int)
} else { } else {
return -1; return -1;
} }