From ab584c187d21c6155a4bae95d4cc7039dbb14bfe Mon Sep 17 00:00:00 2001 From: delvh Date: Thu, 18 Jun 2020 13:08:57 +0200 Subject: [PATCH] Provisorically fixed bug resulting in an empty Endscreen Still, the screen neither shows the Labels or the images --- src/main/dev/lh/Snake.java | 101 ++++++++++++++++-------------- src/main/dev/lh/ui/Endscreen.java | 52 ++++++--------- 2 files changed, 74 insertions(+), 79 deletions(-) mode change 100755 => 100644 src/main/dev/lh/Snake.java mode change 100755 => 100644 src/main/dev/lh/ui/Endscreen.java diff --git a/src/main/dev/lh/Snake.java b/src/main/dev/lh/Snake.java old mode 100755 new mode 100644 index a09479f..1dc0ea7 --- a/src/main/dev/lh/Snake.java +++ b/src/main/dev/lh/Snake.java @@ -73,6 +73,49 @@ public class Snake implements Updateable { 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 public void nextFrame() { 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 - if (checkSelfCollision()) gameOver(); - // 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(); + if (checkSelfCollision()) { + 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 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 public void render(Graphics g) { 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); } - /** - * @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 * @since Snake 1.0 */ 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; - } } diff --git a/src/main/dev/lh/ui/Endscreen.java b/src/main/dev/lh/ui/Endscreen.java old mode 100755 new mode 100644 index 2900378..e489274 --- a/src/main/dev/lh/ui/Endscreen.java +++ b/src/main/dev/lh/ui/Endscreen.java @@ -20,9 +20,9 @@ public class Endscreen extends JDialog { private static final long serialVersionUID = -4457484397259161063L; - private final JPanel contentPanel = new JPanel(); - private int score = 0; private static final int goodOrBadResult = 200; + private final JPanel contentPanel = new JPanel(); + private final int score; /** * Create the dialog. @@ -32,14 +32,13 @@ public class Endscreen extends JDialog { public Endscreen(int score) { this.score = score; try { - // readInHighscoresPoints(); - // readInHighscoresPlayers(); setTitle("Endscreen"); - setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); setBounds(100, 100, 700, 700); getContentPane().setLayout(new BorderLayout()); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); + contentPanel.setLayout(new BorderLayout(0, 0)); getContentPane().add(contentPanel, BorderLayout.CENTER); addWindowListener(new java.awt.event.WindowAdapter() { @@ -49,31 +48,24 @@ public class Endscreen extends JDialog { 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) { 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 */ public int getScore() { return score; } - - /** - * @param score the new highscore - * @since Snake 1.0 - */ - public void setScore(int score) { this.score = score; } }