diff --git a/assets/Map.txt b/assets/Map.txt index 89bbe50..f9a47bf 100644 --- a/assets/Map.txt +++ b/assets/Map.txt @@ -1,36 +1,17 @@ -############################# ##### -# # # # 0 # -# # ############### ######### ##### -# # # ## ### ##### -# # # ########## ### ########## -# # # ################# ##### ##### -# # # # # #### #### -# # # # #################### ###### -# # # # # P####### ###### -# # # # # ################## ###### -# # # # # # ######## ###### -# # # # # # ####### ## ## -# # # # # # ############# ###### ## -# # # # # ############# ## ## -# # # # ################### # ## ## -# # ################# ## -#######################123######### -# -# -# -# -# -# -# -# -# -# -# -# -# -# -# -# -# -# -Es werden nur die ersten 35 Zeilen und Zeichen dieses Dokuments benutzt. \ No newline at end of file +################################### +# # +# ############ ###### ########### # +# ###### # +# ############ ###### ########### # +# ############ ########### # +# ## ### # +# ############ 0123 ########### # +# ########## ###### ######### # +# # # P # # # +# ############ # ## # ########### # +# # ## # # +# ############ ## ########### # +# ############# ############ # +# ############# #### ############ # +# # +################################### \ No newline at end of file diff --git a/bin/.gitignore b/bin/.gitignore deleted file mode 100644 index 7cf58ac..0000000 --- a/bin/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/assets/ diff --git a/bin/Brick.class b/bin/Brick.class index 50ef0ed..98f1c69 100644 Binary files a/bin/Brick.class and b/bin/Brick.class differ diff --git a/bin/Game.class b/bin/Game.class index d9d8a4d..3f67209 100644 Binary files a/bin/Game.class and b/bin/Game.class differ diff --git a/bin/Ghost.class b/bin/Ghost.class index 7a20a84..9c699c4 100644 Binary files a/bin/Ghost.class and b/bin/Ghost.class differ diff --git a/bin/Main.class b/bin/Main.class index 1a83356..ef3f066 100644 Binary files a/bin/Main.class and b/bin/Main.class differ diff --git a/bin/Map.class b/bin/Map.class index bba0248..791a861 100644 Binary files a/bin/Map.class and b/bin/Map.class differ diff --git a/bin/Picture.class b/bin/Picture.class index f720bca..3737254 100644 Binary files a/bin/Picture.class and b/bin/Picture.class differ diff --git a/bin/Player.class b/bin/Player.class index f9425e2..ccedcbf 100644 Binary files a/bin/Player.class and b/bin/Player.class differ diff --git a/src/Game.java b/src/Game.java index 2fdf780..8b1b633 100644 --- a/src/Game.java +++ b/src/Game.java @@ -1,8 +1,12 @@ import java.awt.Color; +import java.util.*; + import javax.swing.JFrame; 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) @@ -20,7 +24,7 @@ public class Game { private long dt; private long lastT; - private int delaytimer = 9; + private int delaytimer = 10; @SuppressWarnings("unused") private long fps = 60; @@ -34,6 +38,9 @@ public class Game { // nach rechts --> x wird größer // nach unten --> y wird größer + int minn; + int[] HCost = new int[4]; + public Game() { // Erstellen des Konstruktors (Was soll passieren, sobald dieses Klasse // aufgerufen wird?) @@ -41,12 +48,9 @@ public class Game { panel = new JPanel(); // darauf werden alle sichtbaren Elemente gespeichert - ghosts = new Ghost[1]; // 4 Geister + ghosts = new Ghost[4]; // 4 Geister - boolean[] ghost_up_possible = new boolean[ghosts.length]; - boolean[] ghost_down_possible = new boolean[ghosts.length]; - boolean[] ghost_left_possible = new boolean[ghosts.length]; - boolean[] ghost_right_possible = new boolean[ghosts.length]; + boolean[][] ghost_possible = new boolean[ghosts.length][4]; player = new Player(); // Pac-Man @@ -104,38 +108,110 @@ public class Game { player.setLocation(player.getPos('x', dt), player.getPos('y', dt)); // Aktualiesieren der Positionen der Geister: - for (int i = 0; i < ghosts.length; i++) { - ghosts[i].setLocation(ghosts[i].getPos('x', dt), ghosts[i].getPos('y', dt)); - } + //for (int i = 0; i < ghosts.length; i++) { + // ghosts[i].setLocation(ghosts[i].getPos('x', dt), ghosts[i].getPos('y', dt)); + //} for (int i = 0; i < ghosts.length; i++) { + + 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[((ghosts[i].getPos('y', 0) - 10) / 20) - 1][(ghosts[i].getPos('x', 0) - 10) - / 20] == null) { - ghost_up_possible[i] = true; } - if (Map.bricks[((ghosts[i].getPos('y', 0) - 10) / 20) + 1][(ghosts[i].getPos('x', 0) - 10) - / 20] == null) { - ghost_down_possible[i] = 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; + } - if (Map.bricks[(ghosts[i].getPos('y', 0) - 10) / 20][((ghosts[i].getPos('x', 0) - 10) / 20) - - 1] == null) { - ghost_left_possible[i] = true; + 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; } - if (Map.bricks[(ghosts[i].getPos('y', 0) - 10) / 20][((ghosts[i].getPos('x', 0) - 10) / 20) - + 1] == null) { - ghost_right_possible[i] = true; + 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; } - ghost_up_possible[i] = false; - ghost_down_possible[i] = false; - ghost_left_possible[i] = false; - ghost_right_possible[i] = false; + 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; + } + if (ghosts[i].down == true) { + HCost[2] = 10000; + } + if (ghosts[i].left == true) { + HCost[0] = 10000; + } + if (ghosts[i].right == true) { + HCost[1] = 10000; + } + + + for (int j = 0; j < 4; j++) { + if (ghost_possible[i][j] == false) { + HCost[j] = 10500; + } + } + 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; + } + } + } + + } - System.out.println(ghosts[0].getHCost(player)); + // 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 @@ -170,6 +246,7 @@ public class Game { player.calcDir(0); // Berechnen wo Pac-Man als nächstes hin soll for (int i = 0; i < ghosts.length; i++) { + ghosts[i].setLocation(ghosts[i].getPos('x', dt), ghosts[i].getPos('y', dt)); 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 @@ -198,4 +275,10 @@ public class Game { e.printStackTrace(); } } + private int conv(float a) { //90.0 --> 4 + int b = (int) (a - 10 )/ 20; + return b; + } + + } diff --git a/src/Ghost.java b/src/Ghost.java index 0dad550..cc3a483 100644 --- a/src/Ghost.java +++ b/src/Ghost.java @@ -4,9 +4,16 @@ public class Ghost extends Picture { // Die private static final long serialVersionUID = -5352006665147359473L; - private int xPos; - private int yPos; + public int xPos; + public int 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) { @@ -25,14 +32,7 @@ public class Ghost extends Picture { // Die } - public int getPos(char coordinate, long dt) { - if (coordinate == 'x') { - return xPos; - } else if (coordinate == 'y') { - return yPos; - } else - return -1; - } + public void setPos(char coordinate, int newPos) { if (coordinate == 'x') { @@ -42,28 +42,45 @@ public class Ghost extends Picture { // Die } } - public void moveUp() { - } - public void moveDown() { - + /* + public void setDirection(String dir) { + direction = dir; } - - public void moveRight() { - + public String getDirection() { + return direction; } - - public void moveLeft() { + */ + public int getHCost(Player player, int mod_x, int mod_y) { - } - - public int getHCost(Player player) { - - HCost = (int) Math.sqrt(Math.pow(xPos - player.getPos('x', 0), 2) + Math.pow(yPos - player.getPos('y', 0), 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) + } else { + return -1; + } + } }