current working status (not working)

This commit is contained in:
delvh 2020-03-11 22:45:31 +01:00
parent 46223d60ca
commit fb1bec2cce
3 changed files with 57 additions and 31 deletions

View File

@ -7,6 +7,7 @@ import java.awt.Rectangle;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import dev.lh.ui.Endscreen;
import dev.lh.ui.GameWindow; import dev.lh.ui.GameWindow;
/** /**
@ -65,7 +66,7 @@ public class Snake implements Updateable {
*/ */
public Snake(int length) { public Snake(int length) {
this.length = length; this.length = length;
Richtung = Direction.Left; Richtung = Direction.Right;
for (int i = 0; i < length; i++) for (int i = 0; i < length; i++)
tiles.add(new Point(320 - 50 * i, 240)); tiles.add(new Point(320 - 50 * i, 240));
@ -98,24 +99,49 @@ public class Snake implements Updateable {
cur = tiles.get(i); cur = tiles.get(i);
tiles.set(i, (Point) next.clone()); tiles.set(i, (Point) next.clone());
next = cur; 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))) { if (foodFactory.checkCollision(new Rectangle(tiles.get(0).x, tiles.get(0).y, snakeSize, snakeSize))) {
addLength(foodFactory.getAdditionalLength()); addLength(foodFactory.getAdditionalLength());
GameWindow game = Main.getGame(); GameWindow game = Main.getGame();
game.newFood(); 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 @Override
public void render(Graphics g) { public void render(Graphics g) {
g.setColor(Color.green); g.setColor(Color.green);
for (int i = 0; i < length; i++) for (int i = 0; i < length; i++)
g.fillRect(tiles.get(i).x, tiles.get(i).y, snakeSize, snakeSize); g.fillRect(tiles.get(i).x, tiles.get(i).y, snakeSize, snakeSize);
}
}// End render
/** /**
* @return the current {@link Direction} of the snake * @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.Font;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog; import javax.swing.JDialog;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
@ -28,15 +26,14 @@ public class Endscreen extends JDialog {
private final JPanel contentPanel = new JPanel(); private final JPanel contentPanel = new JPanel();
private static int score = 0; private static int score = 0;
private final int goodOrBadResult = 250;
/** /**
* Create the dialog. * Create the dialog.
* *
* @param score1 the highscore to set * @param score the highscore to set
*/ */
public Endscreen(int score1) { public Endscreen(int score) {
setScore(score1); Endscreen.score = score;
try { try {
// readInHighscoresPoints(); // readInHighscoresPoints();
// readInHighscoresPlayers(); // readInHighscoresPlayers();
@ -85,7 +82,8 @@ public class Endscreen extends JDialog {
btnNewButton.addActionListener(e -> { Main.startGame(); setVisible(false); dispose(); }); btnNewButton.addActionListener(e -> { Main.startGame(); setVisible(false); dispose(); });
// BLOß NICHT RAUSWERFEN // BLOß NICHT RAUSWERFEN
btnNewButton.setIconTextGap(5); 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"))); // Endscreen.class.getResource("/com/sun/javafx/webkit/prism/resources/mediaPlayDisabled.png")));
btnNewButton.setFont(new Font("Times New Roman", Font.PLAIN, 15)); btnNewButton.setFont(new Font("Times New Roman", Font.PLAIN, 15));
btnNewButton.setBounds(85, 512, 243, 100); btnNewButton.setBounds(85, 512, 243, 100);
@ -149,22 +147,24 @@ public class Endscreen extends JDialog {
// lblYourName.setBounds(10, 355, 82, 29); // lblYourName.setBounds(10, 355, 82, 29);
// contentPanel.add(lblYourName); // contentPanel.add(lblYourName);
JCheckBox chckbxNewCheckBox = new JCheckBox(""); // JCheckBox chckbxNewCheckBox = new JCheckBox("");
JLabel lblDasIstEin = new JLabel("Das ist ein hervorragender Wert!"); // JLabel lblDasIstEin = new JLabel("Das ist ein hervorragender Wert!");
lblDasIstEin.setFont(new Font("Times New Roman", Font.PLAIN, 15)); // lblDasIstEin.setFont(new Font("Times New Roman", Font.PLAIN, 15));
if (score1 >= goodOrBadResult) { // if (score >= goodOrBadResult) {
chckbxNewCheckBox.setIcon(new ImageIcon(ClassLoader.getSystemResource("/dev/lh/snake/1211548-200.png"))); // chckbxNewCheckBox.setIcon(new
chckbxNewCheckBox.setBounds(300, 200, 200, 200); // ImageIcon(ClassLoader.getSystemResource("/dev/lh/snake/1211548-200.png")));
lblDasIstEin.setBounds(10, 100, 212, 50); // chckbxNewCheckBox.setBounds(300, 200, 200, 200);
} else { // lblDasIstEin.setBounds(10, 100, 212, 50);
chckbxNewCheckBox.setIcon(new ImageIcon(ClassLoader.getSystemResource("/dev/lh/snake/Try_Again.jpg"))); // } else {
chckbxNewCheckBox.setBounds(300, 200, 250, 210); // chckbxNewCheckBox.setIcon(new
lblDasIstEin.setText("Das kannst du aber noch verbessern!"); // ImageIcon(ClassLoader.getSystemResource("/dev/lh/snake/Try_Again.jpg")));
lblDasIstEin.setBounds(10, 100, 240, 50); // chckbxNewCheckBox.setBounds(300, 200, 250, 210);
contentPanel.add(lblDasIstEin); // lblDasIstEin.setText("Das kannst du aber noch verbessern!");
} // lblDasIstEin.setBounds(10, 100, 240, 50);
contentPanel.add(chckbxNewCheckBox); // contentPanel.add(lblDasIstEin);
contentPanel.add(lblDasIstEin); // }
// contentPanel.add(chckbxNewCheckBox);
// contentPanel.add(lblDasIstEin);
setVisible(true); setVisible(true);
} }

View File

@ -102,7 +102,7 @@ public class GameWindow extends JFrame {
*/ */
public void newFood() { public void newFood() {
foodFactory.generateFood(); foodFactory.generateFood();
foodFactory.generateFoodLocation(super.getWidth(), super.getHeight()); foodFactory.generateFoodLocation(getWidth(), getHeight());
repaint(); repaint();
} }