Provisorically fixed bug resulting in an empty Endscreen
Still, the screen neither shows the Labels or the images
This commit is contained in:
parent
30011a0788
commit
ab584c187d
101
src/main/dev/lh/Snake.java
Executable file → Normal file
101
src/main/dev/lh/Snake.java
Executable file → Normal 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
34
src/main/dev/lh/ui/Endscreen.java
Executable file → Normal file
34
src/main/dev/lh/ui/Endscreen.java
Executable file → Normal 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,17 +48,10 @@ public class Endscreen extends JDialog {
|
|||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
JButton btnNewButton = new JButton("Play again");
|
JButton btnNewButton = new JButton("Play again");
|
||||||
btnNewButton.setMnemonic(KeyEvent.VK_ENTER);
|
btnNewButton.setMnemonic(KeyEvent.VK_ENTER);
|
||||||
btnNewButton.addActionListener(e -> { Main.startGame(); dispose(); });
|
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));
|
btnNewButton.setFont(new Font("Times New Roman", Font.PLAIN, 15));
|
||||||
contentPanel.add(btnNewButton, BorderLayout.SOUTH);
|
contentPanel.add(btnNewButton, BorderLayout.SOUTH);
|
||||||
|
|
||||||
@ -67,13 +59,13 @@ public class Endscreen extends JDialog {
|
|||||||
lblDeinPunktestand.setFont(new Font("Times New Roman", Font.PLAIN, 25));
|
lblDeinPunktestand.setFont(new Font("Times New Roman", Font.PLAIN, 25));
|
||||||
contentPanel.add(lblDeinPunktestand, BorderLayout.NORTH);
|
contentPanel.add(lblDeinPunktestand, BorderLayout.NORTH);
|
||||||
|
|
||||||
//TODO: the display ofthe result image could work, but not guaranteed
|
Image resultImage = Toolkit.getDefaultToolkit()
|
||||||
// Image resultImage = Toolkit.getDefaultToolkit()
|
.getImage(this.getClass().getResource((score < goodOrBadResult) ? "/Try_Again.jpg" : "/1211548-200.png"));
|
||||||
// .getImage(this.getClass()
|
resultImage.flush();
|
||||||
// .getResource((score < goodOrBadResult) ? "/Snake/src/main/resources/Try_Again.jpg" : "/Snake/src/main/resources/1211548-200.png"));
|
|
||||||
// resultImage.flush();
|
|
||||||
|
|
||||||
setVisible(true);
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -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; }
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user