Provisorically fixed bug resulting in an empty Endscreen

Still, the screen neither shows the Labels or the images
This commit is contained in:
delvh 2020-06-18 13:08:57 +02:00
parent 30011a0788
commit ab584c187d
2 changed files with 74 additions and 79 deletions

101
src/main/dev/lh/Snake.java Executable file → Normal file
View File

@ -73,6 +73,49 @@ public class Snake implements Updateable {
tiles.add(new Point(320 - snakeSize * i, 240)); tiles.add(new Point(320 - snakeSize * i, 240));
} }
/**
* Adds the given length to the current snake object
*
* @param additional the number of tiles to add
* @since Snake 1.0
*/
public void addLength(int additional) {
Point last = tiles.get(tiles.size() - 1);
for (int i = 0; i < additional; i++)
tiles.add(last);
length += additional;
}
/**
* @return whether the snake collides with itself
* @since Snake 1.1
*/
private boolean checkSelfCollision() {
Point headIndex = tiles.get(0);
Rectangle head = new Rectangle(headIndex.x, headIndex.y, snakeSize, snakeSize);
for (int index = 1; index < tiles.size(); index++) {
Point bodyIndex = tiles.get(index);
if (head.contains(new Rectangle(bodyIndex.x, bodyIndex.y, snakeSize, snakeSize))) return true;
}
return false;
}
/**
*
* @since Snake 1.1
*/
private void gameOver() {
endscreen = new Endscreen(length);
endscreen.setVisible(true);
Main.getGame().close();
}
/**
* @return the current {@link Direction} of the snake
* @since Snake 1.0
*/
public Direction getRichtung() { return Richtung; }
@Override @Override
public void nextFrame() { public void nextFrame() {
int velX = 0, velY = 0; int velX = 0, velY = 0;
@ -101,9 +144,18 @@ public class Snake implements Updateable {
} }
// case if snake is outside of the screen or touches itself // case if snake is outside of the screen or touches itself
if (checkSelfCollision()) gameOver(); if (checkSelfCollision()) {
// TODO: the game bounds checking below appears to work on Windows, however throws a NullPointerException on Linux/UNIX systems gameOver();
// if (!Main.getGame().getBounds().contains(tiles.get(0))) gameOver(); System.out.println("Snake collided with itself.");
return;
}
// TODO: the game bounds checking below appears to work on Windows, however
// throws a NullPointerException on Linux/UNIX systems
if (!Main.getGame().getBounds().contains(tiles.get(0))) {
gameOver();
System.out.println("Snake went out of bounds.");
return;
}
// case if snake eats food // case if snake eats food
if (foodFactory.checkCollision(new Rectangle(tiles.get(0).x, tiles.get(0).y, snakeSize, snakeSize))) { if (foodFactory.checkCollision(new Rectangle(tiles.get(0).x, tiles.get(0).y, snakeSize, snakeSize))) {
@ -113,30 +165,6 @@ public class Snake implements Updateable {
} }
} }
/**
*
* @since Snake 1.1
*/
private void gameOver() {
endscreen = new Endscreen(length);
endscreen.setVisible(true);
Main.getGame().close();
}
/**
* @return whether the snake collides with itself
* @since Snake 1.1
*/
private boolean checkSelfCollision() {
Point headIndex = tiles.get(0);
Rectangle head = new Rectangle(headIndex.x, headIndex.y, snakeSize, snakeSize);
for (int index = 1; index < tiles.size(); index++) {
Point bodyIndex = tiles.get(index);
if (head.contains(new Rectangle(bodyIndex.x, bodyIndex.y, snakeSize, snakeSize))) return true;
}
return false;
}
@Override @Override
public void render(Graphics g) { public void render(Graphics g) {
g.setColor(Color.green); g.setColor(Color.green);
@ -144,28 +172,9 @@ public class Snake implements Updateable {
g.fillRect(tiles.get(i).x, tiles.get(i).y, snakeSize, snakeSize); g.fillRect(tiles.get(i).x, tiles.get(i).y, snakeSize, snakeSize);
} }
/**
* @return the current {@link Direction} of the snake
* @since Snake 1.0
*/
public Direction getRichtung() { return Richtung; }
/** /**
* @param richtung the new {@link Direction} of the snake * @param richtung the new {@link Direction} of the snake
* @since Snake 1.0 * @since Snake 1.0
*/ */
public void setRichtung(Direction richtung) { Richtung = richtung; } public void setRichtung(Direction richtung) { Richtung = richtung; }
/**
* Adds the given length to the current snake object
*
* @param additional the number of tiles to add
* @since Snake 1.0
*/
public void addLength(int additional) {
Point last = tiles.get(tiles.size() - 1);
for (int i = 0; i < additional; i++)
tiles.add(last);
length += additional;
}
} }

52
src/main/dev/lh/ui/Endscreen.java Executable file → Normal file
View File

@ -20,9 +20,9 @@ public class Endscreen extends JDialog {
private static final long serialVersionUID = -4457484397259161063L; private static final long serialVersionUID = -4457484397259161063L;
private final JPanel contentPanel = new JPanel();
private int score = 0;
private static final int goodOrBadResult = 200; private static final int goodOrBadResult = 200;
private final JPanel contentPanel = new JPanel();
private final int score;
/** /**
* Create the dialog. * Create the dialog.
@ -32,14 +32,13 @@ public class Endscreen extends JDialog {
public Endscreen(int score) { public Endscreen(int score) {
this.score = score; this.score = score;
try { try {
// readInHighscoresPoints();
// readInHighscoresPlayers();
setTitle("Endscreen"); setTitle("Endscreen");
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setBounds(100, 100, 700, 700); setBounds(100, 100, 700, 700);
getContentPane().setLayout(new BorderLayout()); getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPanel.setLayout(new BorderLayout(0, 0));
getContentPane().add(contentPanel, BorderLayout.CENTER); getContentPane().add(contentPanel, BorderLayout.CENTER);
addWindowListener(new java.awt.event.WindowAdapter() { addWindowListener(new java.awt.event.WindowAdapter() {
@ -49,31 +48,24 @@ public class Endscreen extends JDialog {
System.exit(0); System.exit(0);
} }
}); });
JButton btnNewButton = new JButton("Play again");
btnNewButton.setMnemonic(KeyEvent.VK_ENTER);
btnNewButton.addActionListener(e -> { Main.startGame(); dispose(); });
btnNewButton.setFont(new Font("Times New Roman", Font.PLAIN, 15));
contentPanel.add(btnNewButton, BorderLayout.SOUTH);
JLabel lblDeinPunktestand = new JLabel("Dein Punktestand: " + String.valueOf(score));
lblDeinPunktestand.setFont(new Font("Times New Roman", Font.PLAIN, 25));
contentPanel.add(lblDeinPunktestand, BorderLayout.NORTH);
Image resultImage = Toolkit.getDefaultToolkit()
.getImage(this.getClass().getResource((score < goodOrBadResult) ? "/Try_Again.jpg" : "/1211548-200.png"));
resultImage.flush();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
JButton btnNewButton = new JButton("Play again");
btnNewButton.setMnemonic(KeyEvent.VK_ENTER);
btnNewButton.addActionListener(e -> { Main.startGame(); dispose(); });
contentPanel.setLayout(new BorderLayout(0, 0));
// btnNewButton.setIcon(new
// ImageIcon(ClassLoader.getSystemResource("/com/sun/javafx/webkit/prism/resources/mediaPlayDisabled.png")));
btnNewButton.setIconTextGap(5);
btnNewButton.setFont(new Font("Times New Roman", Font.PLAIN, 15));
contentPanel.add(btnNewButton, BorderLayout.SOUTH);
JLabel lblDeinPunktestand = new JLabel("Dein Punktestand: " + String.valueOf(score));
lblDeinPunktestand.setFont(new Font("Times New Roman", Font.PLAIN, 25));
contentPanel.add(lblDeinPunktestand, BorderLayout.NORTH);
//TODO: the display ofthe result image could work, but not guaranteed
// Image resultImage = Toolkit.getDefaultToolkit()
// .getImage(this.getClass()
// .getResource((score < goodOrBadResult) ? "/Snake/src/main/resources/Try_Again.jpg" : "/Snake/src/main/resources/1211548-200.png"));
// resultImage.flush();
setVisible(true);
} }
/** /**
@ -81,10 +73,4 @@ public class Endscreen extends JDialog {
* @since Snake 1.0 * @since Snake 1.0
*/ */
public int getScore() { return score; } public int getScore() { return score; }
/**
* @param score the new highscore
* @since Snake 1.0
*/
public void setScore(int score) { this.score = score; }
} }