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.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++) {
@ -287,7 +303,7 @@ public class Game {
}
fps = 1000000000 / dt;
// System.out.println("fps: " + fps + " | delaytimer: " + delaytimer);
// System.out.println("fps: " + fps);
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;
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;
@ -42,18 +38,10 @@ public class Ghost extends Picture { // Die
}
}
/*
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;
}
@ -62,19 +50,15 @@ public class Ghost extends Picture { // Die
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)