diff --git a/bin/Game.class b/bin/Game.class index e0fcf3d..49c709f 100644 Binary files a/bin/Game.class and b/bin/Game.class differ diff --git a/bin/Ghost.class b/bin/Ghost.class index a0531e2..c6af23e 100644 Binary files a/bin/Ghost.class and b/bin/Ghost.class differ diff --git a/src/Game.java b/src/Game.java index 011d1de..251597e 100644 --- a/src/Game.java +++ b/src/Game.java @@ -1,12 +1,10 @@ import java.awt.Color; - -import java.util.*; +import java.awt.Font; import javax.swing.JFrame; +import javax.swing.JLabel; import javax.swing.JPanel; -import java.lang.Math; - // In dieser Klasse findet der größte Teil des Spiels statt: // Rendern von Map, Geistern, Pac-Man etc. // Überprüfung von Kollisionen verschiedener Elemente (der komplizierteste Teil des Programms) @@ -24,7 +22,11 @@ public class Game { private long dt; private long lastT; - private int delaytimer = 10; + private int score = 0; + + private JLabel scoreLabel; + + private int delaytimer = 13; @SuppressWarnings("unused") private long fps = 60; @@ -54,6 +56,18 @@ public class Game { 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 frame.addKeyListener(player); // KeyListener wird hinzugefügt, man kann nun Pac-Maan mit der tastatur steuern @@ -115,7 +129,6 @@ public class Game { // gesetzt: player.setLocation(player.getPos('x', dt), player.getPos('y', dt)); - for (int i = 0; i < ghosts.length; i++) { ghost_possible[i][0] = false; @@ -146,7 +159,6 @@ public class Game { ghost_possible[i][3] = true; } - HCost[0] = ghosts[i].getHCost(player, 1, 0); HCost[1] = ghosts[i].getHCost(player, -1, 0); HCost[2] = ghosts[i].getHCost(player, 0, -1); @@ -179,7 +191,6 @@ public class Game { } } - for (int j = 0; j < 4; j++) { if (HCost[j] == minn) { if (j == 0) { @@ -252,14 +263,19 @@ public class Game { && 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) { + 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 for (int i = 0; i < ghosts.length; i++) { @@ -272,8 +288,8 @@ public class Game { player.lives--; System.out.println(player.lives); delay(200); - for(int j = 0; j < ghosts.length; j++) { - ghosts[j].setPos('x', Map.ghost_posX[j]); + 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); @@ -282,12 +298,12 @@ public class Game { player.right = false; player.left = false; } - + } } fps = 1000000000 / dt; - // System.out.println("fps: " + fps + " | delaytimer: " + delaytimer); + // System.out.println("fps: " + fps); delay(delaytimer); // Ein delay zum Ende der Hauptschleife diff --git a/src/Ghost.java b/src/Ghost.java index cc3a483..d87b090 100644 --- a/src/Ghost.java +++ b/src/Ghost.java @@ -4,16 +4,14 @@ public class Ghost extends Picture { // Die private static final long serialVersionUID = -5352006665147359473L; - public int xPos; - public int yPos; - - + public float xPos; + public float yPos; + public boolean up; public boolean down; public boolean left; public boolean right; - //private float speed = 0.00000007f; private int HCost; public Ghost(int index) { @@ -32,8 +30,6 @@ public class Ghost extends Picture { // Die } - - public void setPos(char coordinate, int newPos) { if (coordinate == 'x') { xPos = newPos; @@ -41,43 +37,31 @@ public class Ghost extends Picture { // Die 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) { - - 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; } - + 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 (left && dt != 0) { - // xPos -= speed * dt; xPos -= 1; } else if (right && dt != 0) { - // xPos += speed * dt; xPos += 1; } return (int) xPos; } else if (coordinate == 'y') { // Auslesen der 'y' - Koordinate: if (down && dt != 0) { - // yPos += speed * dt; yPos += 1; } else if (up && dt != 0) { - // yPos -= speed * dt; yPos -= 1; } - return (int) yPos; //(int) + return (int) yPos; // (int) } else { return -1; }