From fb1bec2ccef793b96ed3e734a0039dbe709423f5 Mon Sep 17 00:00:00 2001 From: delvh Date: Wed, 11 Mar 2020 22:45:31 +0100 Subject: [PATCH] current working status (not working) --- src/main/dev/lh/Snake.java | 40 +++++++++++++++++++++----- src/main/dev/lh/ui/Endscreen.java | 46 +++++++++++++++--------------- src/main/dev/lh/ui/GameWindow.java | 2 +- 3 files changed, 57 insertions(+), 31 deletions(-) diff --git a/src/main/dev/lh/Snake.java b/src/main/dev/lh/Snake.java index 2e0a2c1..88134dc 100644 --- a/src/main/dev/lh/Snake.java +++ b/src/main/dev/lh/Snake.java @@ -7,6 +7,7 @@ import java.awt.Rectangle; import java.util.ArrayList; import java.util.List; +import dev.lh.ui.Endscreen; import dev.lh.ui.GameWindow; /** @@ -65,7 +66,7 @@ public class Snake implements Updateable { */ public Snake(int length) { this.length = length; - Richtung = Direction.Left; + Richtung = Direction.Right; for (int i = 0; i < length; i++) tiles.add(new Point(320 - 50 * i, 240)); @@ -98,24 +99,49 @@ public class Snake implements Updateable { cur = tiles.get(i); tiles.set(i, (Point) next.clone()); next = cur; - } // for - // if(tiles.get(0).x<=0||tiles.get(0).x>=) + } + + // case if the snake is outside of the screen or touches itself + if (!Main.getGame().getBounds().contains(tiles.get(0)) || checkSelfCollision()) gameOver(); + + // case if snake eats food if (foodFactory.checkCollision(new Rectangle(tiles.get(0).x, tiles.get(0).y, snakeSize, snakeSize))) { addLength(foodFactory.getAdditionalLength()); GameWindow game = Main.getGame(); game.newFood(); } + } - }// End tick + /** + * + * @since Snake 1.1 + */ + private void gameOver() { + Endscreen 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); - for (int i = 0; i < length; i++) g.fillRect(tiles.get(i).x, tiles.get(i).y, snakeSize, snakeSize); - - }// End render + } /** * @return the current {@link Direction} of the snake diff --git a/src/main/dev/lh/ui/Endscreen.java b/src/main/dev/lh/ui/Endscreen.java index c6cf133..92da2d3 100644 --- a/src/main/dev/lh/ui/Endscreen.java +++ b/src/main/dev/lh/ui/Endscreen.java @@ -4,9 +4,7 @@ import java.awt.BorderLayout; import java.awt.Font; import java.awt.event.KeyEvent; -import javax.swing.ImageIcon; import javax.swing.JButton; -import javax.swing.JCheckBox; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JPanel; @@ -28,15 +26,14 @@ public class Endscreen extends JDialog { private final JPanel contentPanel = new JPanel(); private static int score = 0; - private final int goodOrBadResult = 250; /** * Create the dialog. * - * @param score1 the highscore to set + * @param score the highscore to set */ - public Endscreen(int score1) { - setScore(score1); + public Endscreen(int score) { + Endscreen.score = score; try { // readInHighscoresPoints(); // readInHighscoresPlayers(); @@ -85,7 +82,8 @@ public class Endscreen extends JDialog { btnNewButton.addActionListener(e -> { Main.startGame(); setVisible(false); dispose(); }); // BLOß NICHT RAUSWERFEN btnNewButton.setIconTextGap(5); - btnNewButton.setIcon(new ImageIcon(ClassLoader.getSystemResource("/com/sun/javafx/webkit/prism/resources/mediaPlayDisabled.png"))); + // btnNewButton.setIcon(new + // ImageIcon(ClassLoader.getSystemResource("/com/sun/javafx/webkit/prism/resources/mediaPlayDisabled.png"))); // Endscreen.class.getResource("/com/sun/javafx/webkit/prism/resources/mediaPlayDisabled.png"))); btnNewButton.setFont(new Font("Times New Roman", Font.PLAIN, 15)); btnNewButton.setBounds(85, 512, 243, 100); @@ -149,22 +147,24 @@ public class Endscreen extends JDialog { // lblYourName.setBounds(10, 355, 82, 29); // contentPanel.add(lblYourName); - JCheckBox chckbxNewCheckBox = new JCheckBox(""); - JLabel lblDasIstEin = new JLabel("Das ist ein hervorragender Wert!"); - lblDasIstEin.setFont(new Font("Times New Roman", Font.PLAIN, 15)); - if (score1 >= goodOrBadResult) { - chckbxNewCheckBox.setIcon(new ImageIcon(ClassLoader.getSystemResource("/dev/lh/snake/1211548-200.png"))); - chckbxNewCheckBox.setBounds(300, 200, 200, 200); - lblDasIstEin.setBounds(10, 100, 212, 50); - } else { - chckbxNewCheckBox.setIcon(new ImageIcon(ClassLoader.getSystemResource("/dev/lh/snake/Try_Again.jpg"))); - chckbxNewCheckBox.setBounds(300, 200, 250, 210); - lblDasIstEin.setText("Das kannst du aber noch verbessern!"); - lblDasIstEin.setBounds(10, 100, 240, 50); - contentPanel.add(lblDasIstEin); - } - contentPanel.add(chckbxNewCheckBox); - contentPanel.add(lblDasIstEin); + // JCheckBox chckbxNewCheckBox = new JCheckBox(""); + // JLabel lblDasIstEin = new JLabel("Das ist ein hervorragender Wert!"); + // lblDasIstEin.setFont(new Font("Times New Roman", Font.PLAIN, 15)); + // if (score >= goodOrBadResult) { + // chckbxNewCheckBox.setIcon(new + // ImageIcon(ClassLoader.getSystemResource("/dev/lh/snake/1211548-200.png"))); + // chckbxNewCheckBox.setBounds(300, 200, 200, 200); + // lblDasIstEin.setBounds(10, 100, 212, 50); + // } else { + // chckbxNewCheckBox.setIcon(new + // ImageIcon(ClassLoader.getSystemResource("/dev/lh/snake/Try_Again.jpg"))); + // chckbxNewCheckBox.setBounds(300, 200, 250, 210); + // lblDasIstEin.setText("Das kannst du aber noch verbessern!"); + // lblDasIstEin.setBounds(10, 100, 240, 50); + // contentPanel.add(lblDasIstEin); + // } + // contentPanel.add(chckbxNewCheckBox); + // contentPanel.add(lblDasIstEin); setVisible(true); } diff --git a/src/main/dev/lh/ui/GameWindow.java b/src/main/dev/lh/ui/GameWindow.java index ba6d481..087aa47 100644 --- a/src/main/dev/lh/ui/GameWindow.java +++ b/src/main/dev/lh/ui/GameWindow.java @@ -102,7 +102,7 @@ public class GameWindow extends JFrame { */ public void newFood() { foodFactory.generateFood(); - foodFactory.generateFoodLocation(super.getWidth(), super.getHeight()); + foodFactory.generateFoodLocation(getWidth(), getHeight()); repaint(); }