Browse Source

Merge branch 'main' of https://gitlab.cs.hs-fulda.de/fdai7012/pacmayham

remotes/origin/playerRefactoring
fdai7753 11 months ago
parent
commit
d0421e1ae1
  1. 2
      .settings/org.eclipse.core.resources.prefs
  2. 6
      src/main/java/pacmanGame/GameManager.java
  3. 6
      src/main/java/pacmanGame/Program.java
  4. 208
      src/main/java/pacmanGame/VisualizerPlainTextUltra.java
  5. 1
      src/test/java/pacmanTests/GameManagerTest.java

2
.settings/org.eclipse.core.resources.prefs

@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding//src/main/java/pacmanGame/VisualizerPlainTextUltra.java=UTF-8

6
src/main/java/pacmanGame/GameManager.java

@ -25,7 +25,7 @@ public class GameManager {
ghosts[i] = new Ghost(this, i); ghosts[i] = new Ghost(this, i);
} }
map = new Map(Map.mapClassic, this); map = new Map(Map.mapClassic, this);
visualizer = new VisualizerPlainText(this);
visualizer = new VisualizerPlainTextUltra(this);
player = new Player(this); player = new Player(this);
player.Spawn(); player.Spawn();
for (int i = 0; i < ghosts.length; i++) { for (int i = 0; i < ghosts.length; i++) {
@ -37,9 +37,9 @@ public class GameManager {
public void handleGhostCollision() { public void handleGhostCollision() {
livesRemaining--; // Reduziere verbleibende leben um 1 livesRemaining--; // Reduziere verbleibende leben um 1
failedAttempts++; // Erhöhe die Anzahl der Fehlversuche
failedAttempts++; // Erh�he die Anzahl der Fehlversuche
if(livesRemaining <= 0) { if(livesRemaining <= 0) {
gameOver(); //wenn Keine Leben mehr übrig sind.
gameOver(); //wenn Keine Leben mehr �brig sind.
} }
} }

6
src/main/java/pacmanGame/Program.java

@ -54,15 +54,15 @@ public class Program {
frame = new JFrame("PacmaaaAAAYYYHAAAaaam"); frame = new JFrame("PacmaaaAAAYYYHAAAaaam");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 685);
frame.setSize(750, 950);
textArea = new JTextArea("..."); textArea = new JTextArea("...");
textArea.setEditable(false); textArea.setEditable(false);
textArea.setFocusable(false); textArea.setFocusable(false);
textArea.setBackground(Color.black); textArea.setBackground(Color.black);
textArea.setForeground(Color.blue);
textArea.setForeground(Color.yellow);
Font font = new Font("Consolas", Font.PLAIN, 15);
Font font = new Font("Consolas", Font.PLAIN, 10);
textArea.setFont(font); textArea.setFont(font);
frame.add(textArea, BorderLayout.CENTER); frame.add(textArea, BorderLayout.CENTER);

208
src/main/java/pacmanGame/VisualizerPlainTextUltra.java

@ -0,0 +1,208 @@
package pacmanGame;
import java.util.HashMap;
public class VisualizerPlainTextUltra implements Visualizer {
public boolean showScore = true;
public final int playerAnimationTime = 2;
private String output;
public GameManager gameManager;
public HashMap<String, String[]> sprites = new HashMap<String,String[]>(){{
this.put("empty", new String[] {
" ",
" "
});
this.put("dot", new String[] {
" . ",
" "
});
this.put("wall", new String[] {
"|''|",
"|__|"
});
this.put("cherry", new String[] {
" /\\ ",
" 0O "
});
}};
public final String[][] ghostSprites = {
{
"/--\\",
"~~~~"
},
{
"/°°\\",
"~~~~"
},
{
"/^^\\",
"~~~~"
},
{
"/oo\\",
"~~~~"
},
{
"/''\\",
"~~~~"
},
{
"/**\\",
"~~~~"
},
{
"/00\\",
"~~~~"
},
{
"/++\\",
"~~~~"
},
{
"/..\\",
"~~~~"
},
{
"/§§\\",
"~~~~" }
};
public final String[] playerSprite = {
"o88o",
"°88°"
};
public final String[][] playerAltSprites = {
{
"o o",
"°oo°"
},
{
"o°°o",
"° °"
},
{
" °°o",
" oo°"
},
{
"o°° ",
"°oo "
}
};
public VisualizerPlainTextUltra(GameManager gameManager) {
this.gameManager = gameManager;
}
@Override
public Object GetOutput() {
return output;
}
public void Update() {
output = "";
if(showScore) {
output = "\nSCORE: " + gameManager.score + "\n\n";
}
int lineBufferSize = 2;
String[] lineBuffer = new String[lineBufferSize];
Map map = gameManager.map;
for(int y = 0; y < map.size.y; y++) {
if(gameManager.isPaused && y > 10 && y < 20) {
for(int i = 0; i < lineBufferSize; i++) {
if(y == 15 && i == 0) {
output += " Paused! \n";
}
else{
output += "\n";
}
}
}
else {
for(int i = 0; i < lineBufferSize; i++) {
lineBuffer[i] = "";
}
for(int x = 0; x < map.size.x; x++) {
Cell cell = map.GetCell(new Vector2(x, map.size.y - 1 - y));
boolean containsGhosts = false;
boolean containsPlayer = false;
int ghostIndex = -1;
for( int i = 0; i < gameManager.ghosts.length; i++) {
if(gameManager.ghosts[i].position.equals(cell.pos)) {
containsGhosts = true;
ghostIndex = i;
}
}
if(gameManager.player.position.equals(cell.pos)) {
containsPlayer = true;
}
if(containsPlayer) {
if((gameManager.time / playerAnimationTime) % 2 == 0) {
int directionIndex = 0;
if(gameManager.player.direction.equals(new Vector2(0, 1))) {
directionIndex = 0;
}
else if(gameManager.player.direction.equals(new Vector2(0, -1))) {
directionIndex = 1;
}
else if(gameManager.player.direction.equals(new Vector2(-1, 0))) {
directionIndex = 2;
}
else if(gameManager.player.direction.equals(new Vector2(1, 0))) {
directionIndex = 3;
}
lineBuffer = AttachToBuffer(lineBuffer, playerAltSprites[directionIndex]);
}
else {
lineBuffer = AttachToBuffer(lineBuffer, playerSprite);
}
}
else if(containsGhosts) {
lineBuffer = AttachToBuffer(lineBuffer, ghostSprites[ghostIndex]);
}
else if (sprites.containsKey(cell.type)) {
lineBuffer = AttachToBuffer(lineBuffer, sprites.get(cell.type));
}
else {
System.out.println("unknown type" + cell.type);
}
}
for(int i = 0; i < lineBufferSize; i++) {
output += lineBuffer[i] + "\n";
}
}
}
}
public String[] AttachToBuffer(String[] buffer, String[] sprite) {
for(int i = 0; i < buffer.length; i++) {
buffer[i] += sprite[i];
}
return buffer;
}
}

1
src/test/java/pacmanTests/GameManagerTest.java

@ -34,6 +34,7 @@ class GameManagerTest {
// arrange // arrange
GameManager gameManager = new GameManager(); GameManager gameManager = new GameManager();
gameManager.visualizer = new VisualizerPlainText(gameManager);
VisualizerPlainText vpt = (VisualizerPlainText) gameManager.visualizer; VisualizerPlainText vpt = (VisualizerPlainText) gameManager.visualizer;
// act // act

Loading…
Cancel
Save