Collision checking for outer bounds works apparentlly solely on Windows, commented out for now #1

Merged
delvh merged 5 commits from f/collision_checking into develop 2020-04-03 10:43:25 +02:00
3 changed files with 57 additions and 31 deletions
Showing only changes of commit fb1bec2cce - Show all commits

View File

@ -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

View File

@ -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);
}

View File

@ -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();
}