Collision checking for outer bounds works apparentlly solely on Windows, commented out for now (#1)
* Cleaned up project * Commented display of result-image out * added TODO in outer-bounds checking in Snake
This commit is contained in:
parent
46223d60ca
commit
8ff179a27d
5
.classpath
Normal file → Executable file
5
.classpath
Normal file → Executable file
@ -2,6 +2,7 @@
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
@ -12,10 +13,6 @@
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="src/main/resources"/>
|
||||
<classpathentry kind="lib" path="Snake_jar.jar"/>
|
||||
<classpathentry kind="lib" path="Snake_runnableJAR.jar"/>
|
||||
<classpathentry kind="lib" path="Snake/jgoodies-forms-1.8.0.jar"/>
|
||||
<classpathentry kind="lib" path="Snake/miglayout15-swing.jar"/>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||
<attributes>
|
||||
<attribute name="test" value="true"/>
|
||||
|
0
.gitignore
vendored
Normal file → Executable file
0
.gitignore
vendored
Normal file → Executable file
0
.settings/org.eclipse.jdt.core.prefs
Normal file → Executable file
0
.settings/org.eclipse.jdt.core.prefs
Normal file → Executable file
0
.settings/org.eclipse.jdt.ui.prefs
Normal file → Executable file
0
.settings/org.eclipse.jdt.ui.prefs
Normal file → Executable file
0
.settings/org.eclipse.m2e.core.prefs
Normal file → Executable file
0
.settings/org.eclipse.m2e.core.prefs
Normal file → Executable file
@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
1
Snake/.gitignore
vendored
1
Snake/.gitignore
vendored
@ -1 +0,0 @@
|
||||
/bin/
|
@ -1,17 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Snake</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
@ -1,11 +0,0 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.8
|
Binary file not shown.
Binary file not shown.
@ -1,21 +0,0 @@
|
||||
package dev.lh.snake;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Point;
|
||||
|
||||
public class Food {
|
||||
|
||||
private Point position;
|
||||
|
||||
public Food(int x, int y) {
|
||||
position = new Point(x,y);
|
||||
}
|
||||
|
||||
public void render(Graphics g) {
|
||||
g.setColor(Color.yellow);
|
||||
g.fillRect(position.x, position.y, 16, 16);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,98 +0,0 @@
|
||||
package dev.lh.snake;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.Timer;
|
||||
|
||||
import dev.lh.snake.Snake;
|
||||
|
||||
public class GameWindow extends JFrame {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
Toolkit tk = Toolkit.getDefaultToolkit();
|
||||
int xSize = ((int) tk.getScreenSize().getWidth());
|
||||
int ySize = ((int) tk.getScreenSize().getHeight());
|
||||
|
||||
public GameWindow(int width, int height, String title) {
|
||||
super(title);
|
||||
setLocationRelativeTo(null);
|
||||
Dimension size = new Dimension(width, height);
|
||||
setPreferredSize(size);
|
||||
setMinimumSize(size);
|
||||
setMaximumSize(size);
|
||||
setResizable(false);
|
||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
|
||||
Snake s = new Snake(3);
|
||||
|
||||
add(new JPanel() {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.setColor(Color.black);
|
||||
g.fillRect(0, 0, width, height);
|
||||
s.render(g);
|
||||
}
|
||||
});
|
||||
|
||||
addKeyListener(new KeyAdapter() {
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
super.keyPressed(e);
|
||||
switch (e.getKeyCode()) {
|
||||
case KeyEvent.VK_W:
|
||||
s.setRichtung(Direction.Up);
|
||||
break;
|
||||
case KeyEvent.VK_A:
|
||||
s.setRichtung(Direction.Left);
|
||||
break;
|
||||
case KeyEvent.VK_S:
|
||||
s.setRichtung(Direction.Down);
|
||||
break;
|
||||
case KeyEvent.VK_D:
|
||||
s.setRichtung(Direction.Right);
|
||||
break;
|
||||
case KeyEvent.VK_UP:
|
||||
s.setRichtung(Direction.Up);
|
||||
break;
|
||||
case KeyEvent.VK_LEFT:
|
||||
s.setRichtung(Direction.Left);
|
||||
break;
|
||||
case KeyEvent.VK_DOWN:
|
||||
s.setRichtung(Direction.Down);
|
||||
break;
|
||||
case KeyEvent.VK_RIGHT:
|
||||
s.setRichtung(Direction.Right);
|
||||
break;
|
||||
|
||||
}// switch
|
||||
}// keypressed
|
||||
});// keylistener
|
||||
|
||||
Timer timer = new Timer (200, (evt)->{
|
||||
s.tick();
|
||||
repaint();
|
||||
});
|
||||
timer.start();
|
||||
|
||||
setVisible(true);
|
||||
|
||||
}
|
||||
}// Konstruktor
|
@ -1,15 +0,0 @@
|
||||
package dev.lh.snake;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Handler {
|
||||
|
||||
List<Updateable> targets;
|
||||
|
||||
public Handler() {
|
||||
targets = new ArrayList<>();
|
||||
targets.add(new Snake(3));
|
||||
}
|
||||
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
package dev.lh.snake;
|
||||
|
||||
import dev.lh.snake.GameWindow;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
GameWindow game = new GameWindow(640, 480, "Snake");
|
||||
game.setVisible(true);
|
||||
}
|
||||
|
||||
}
|
@ -1,76 +0,0 @@
|
||||
package dev.lh.snake;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Point;
|
||||
|
||||
public class Snake implements Updateable {
|
||||
|
||||
static enum Direction{
|
||||
Left, Right, Up, Down;
|
||||
}
|
||||
private Direction Richtung;
|
||||
private int length;
|
||||
private Point[] tiles;
|
||||
|
||||
public Snake(int length) {
|
||||
this.length = length;
|
||||
tiles =new Point[length];
|
||||
Richtung = Direction.Left;
|
||||
|
||||
for(int i = 0; i<length;i++) {
|
||||
tiles[i] = new Point(320-50*i, 240);
|
||||
}
|
||||
|
||||
}//End Constructor
|
||||
@Override
|
||||
public void tick() {
|
||||
int velX = 0, velY = 0;
|
||||
switch(Richtung) {
|
||||
|
||||
case Up:
|
||||
velY=-50;
|
||||
break;
|
||||
case Down:
|
||||
velY=50;
|
||||
break;
|
||||
case Left:
|
||||
velX=-50;
|
||||
break;
|
||||
case Right:
|
||||
velX=50;
|
||||
break;
|
||||
}//switch
|
||||
Point next = (Point) tiles[0].clone(), cur;
|
||||
tiles[0].x+=velX;
|
||||
tiles[0].y+=velY;
|
||||
|
||||
|
||||
for(int i = 1;i<length;i++) {
|
||||
cur = tiles[i];
|
||||
tiles[i]=(Point) next.clone();
|
||||
next = cur;
|
||||
|
||||
}//for
|
||||
|
||||
}//End tick
|
||||
@Override
|
||||
public void render(Graphics g) {
|
||||
g.setColor(Color.green);
|
||||
|
||||
for (int i = 0; i<length;i++) {
|
||||
g.drawRect(tiles[i].x, tiles[i].y, 50, 50);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}//End render
|
||||
|
||||
public Direction getRichtung() {
|
||||
return Richtung;
|
||||
}
|
||||
|
||||
public void setRichtung(Direction richtung) {
|
||||
Richtung = richtung;
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
package dev.lh.snake;
|
||||
|
||||
import java.awt.Graphics;
|
||||
|
||||
public class Spawner implements Updateable{
|
||||
|
||||
@Override
|
||||
public void tick() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(Graphics g) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package dev.lh.snake;
|
||||
|
||||
import java.awt.Graphics;
|
||||
|
||||
public interface Updateable {
|
||||
|
||||
void tick();
|
||||
|
||||
void render(Graphics g);
|
||||
|
||||
}
|
BIN
Snake_jar.jar
BIN
Snake_jar.jar
Binary file not shown.
Binary file not shown.
@ -1,45 +0,0 @@
|
||||
package dev.lh;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Point;
|
||||
|
||||
/**
|
||||
* Project: <strong>Snake</strong><br>
|
||||
* File: <strong>Food.java</strong><br>
|
||||
* Created: <strong>11 Mar 2020</strong><br>
|
||||
*
|
||||
* @author Leon Hofmeister
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
public class Food {
|
||||
|
||||
private Point position;
|
||||
|
||||
/**
|
||||
* Constructs a new food object.
|
||||
*
|
||||
* @param x the x coordinate of the new food
|
||||
* @param y the y coordinate of the new food
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
public Food(int x, int y) { position = new Point(x, y); }
|
||||
|
||||
/**
|
||||
* Constructs a new food object.
|
||||
*
|
||||
* @param position the position of the food
|
||||
* @since Snake 1.1
|
||||
*/
|
||||
public Food(Point position) { this.position = position; }
|
||||
|
||||
/**
|
||||
* @param g the {@link Graphics} object used to draw the food
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
public void render(Graphics g) {
|
||||
g.setColor(Color.yellow);
|
||||
g.fillRect(position.x, position.y, 16, 16);
|
||||
}
|
||||
|
||||
}
|
9
src/main/dev/lh/FoodFactory.java
Normal file → Executable file
9
src/main/dev/lh/FoodFactory.java
Normal file → Executable file
@ -1,10 +1,6 @@
|
||||
package dev.lh;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Point;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.*;
|
||||
|
||||
import dev.lh.ui.GameWindow;
|
||||
|
||||
@ -227,8 +223,7 @@ public class FoodFactory {
|
||||
case blue:
|
||||
snakeAdditionalLength = 1;
|
||||
break;
|
||||
}// switch
|
||||
}
|
||||
return snakeAdditionalLength;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,30 +0,0 @@
|
||||
package dev.lh;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The handler handles incoming events in Snake.<br>
|
||||
* <br>
|
||||
* Project: <strong>Snake</strong><br>
|
||||
* File: <strong>Handler.java</strong><br>
|
||||
* Created: <strong>11 Mar 2020</strong><br>
|
||||
*
|
||||
* @author Leon Hofmeister
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
public class Handler {
|
||||
|
||||
List<Updateable> targets;
|
||||
|
||||
/**
|
||||
* Constructs a new {@link Handler}.
|
||||
*
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
public Handler() {
|
||||
targets = new ArrayList<>();
|
||||
targets.add(new Snake(3));
|
||||
}
|
||||
|
||||
}
|
1
src/main/dev/lh/Main.java
Normal file → Executable file
1
src/main/dev/lh/Main.java
Normal file → Executable file
@ -38,5 +38,4 @@ public class Main {
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
public static GameWindow getGame() { return game; }
|
||||
|
||||
}
|
||||
|
55
src/main/dev/lh/Snake.java
Normal file → Executable file
55
src/main/dev/lh/Snake.java
Normal file → Executable 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;
|
||||
|
||||
/**
|
||||
@ -51,10 +52,11 @@ public class Snake implements Updateable {
|
||||
Down;
|
||||
}
|
||||
|
||||
private static FoodFactory foodFactory = FoodFactory.getInstance();
|
||||
private static Endscreen endscreen;
|
||||
private Direction Richtung;
|
||||
private int length;
|
||||
private List<Point> tiles = new ArrayList<>();
|
||||
private static FoodFactory foodFactory = FoodFactory.getInstance();
|
||||
private final int snakeSize = 10;
|
||||
|
||||
/**
|
||||
@ -65,18 +67,16 @@ public class Snake implements Updateable {
|
||||
*/
|
||||
public Snake(int length) {
|
||||
this.length = length;
|
||||
Richtung = Direction.Left;
|
||||
|
||||
Richtung = Direction.Right;
|
||||
// adding the initial tiles of the snake
|
||||
for (int i = 0; i < length; i++)
|
||||
tiles.add(new Point(320 - 50 * i, 240));
|
||||
|
||||
}// End Constructor
|
||||
tiles.add(new Point(320 - snakeSize * i, 240));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void nextFrame() {
|
||||
int velX = 0, velY = 0;
|
||||
switch (Richtung) {
|
||||
|
||||
case Up:
|
||||
velY = -snakeSize;
|
||||
break;
|
||||
@ -89,7 +89,7 @@ public class Snake implements Updateable {
|
||||
case Right:
|
||||
velX = snakeSize;
|
||||
break;
|
||||
}// switch
|
||||
}
|
||||
Point next = (Point) tiles.get(0).clone(), cur;
|
||||
tiles.get(0).x += velX;
|
||||
tiles.get(0).y += velY;
|
||||
@ -98,24 +98,51 @@ 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 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();
|
||||
|
||||
// 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 = 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
|
||||
|
@ -1,25 +0,0 @@
|
||||
package dev.lh;
|
||||
|
||||
import java.awt.Graphics;
|
||||
|
||||
/**
|
||||
* Project: <strong>Snake</strong><br>
|
||||
* File: <strong>Spawner.java</strong><br>
|
||||
* Created: <strong>11 Mar 2020</strong><br>
|
||||
*
|
||||
* @author Leon Hofmeister
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
public class Spawner implements Updateable {
|
||||
|
||||
@Override
|
||||
public void nextFrame() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(Graphics g) {
|
||||
|
||||
}
|
||||
|
||||
}
|
1
src/main/dev/lh/Updateable.java
Normal file → Executable file
1
src/main/dev/lh/Updateable.java
Normal file → Executable file
@ -29,5 +29,4 @@ public interface Updateable {
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
void render(Graphics g);
|
||||
|
||||
}
|
||||
|
213
src/main/dev/lh/ui/Endscreen.java
Normal file → Executable file
213
src/main/dev/lh/ui/Endscreen.java
Normal file → Executable file
@ -1,15 +1,9 @@
|
||||
package dev.lh.ui;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Font;
|
||||
import java.awt.*;
|
||||
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;
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
|
||||
import dev.lh.Main;
|
||||
@ -26,17 +20,17 @@ public class Endscreen extends JDialog {
|
||||
|
||||
private static final long serialVersionUID = -4457484397259161063L;
|
||||
|
||||
private final JPanel contentPanel = new JPanel();
|
||||
private static int score = 0;
|
||||
private final int goodOrBadResult = 250;
|
||||
private final JPanel contentPanel = new JPanel();
|
||||
private int score = 0;
|
||||
private static final int goodOrBadResult = 200;
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
this.score = score;
|
||||
try {
|
||||
// readInHighscoresPoints();
|
||||
// readInHighscoresPlayers();
|
||||
@ -58,113 +52,27 @@ public class Endscreen extends JDialog {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
contentPanel.setLayout(null);
|
||||
// JScrollPane scrollPane = new JScrollPane();
|
||||
// scrollPane.setBounds(10, 412, 349, 238);
|
||||
// contentPanel.add(scrollPane);
|
||||
// String[][] combis = new String[highscorePoints.length][2];
|
||||
// for (int i = 0; i < highscorePoints.length; i++) {
|
||||
// combis[i][0] = highscorePlayers[i];
|
||||
// combis[i][1] = String.valueOf(highscorePoints[i]);
|
||||
// }
|
||||
// table = new JTable(combis, tableTitle);
|
||||
// table.setRowSelectionAllowed(false);
|
||||
// table.setFillsViewportHeight(true);
|
||||
// table.setFont(new Font("Times New Roman", Font.PLAIN, 15));
|
||||
// scrollPane.setViewportView(table);
|
||||
|
||||
// JLabel lblNewLabel = new JLabel("Highscores");
|
||||
// lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD | Font.ITALIC,
|
||||
// 18));
|
||||
// lblNewLabel.setBounds(65, 292, 98, 41);
|
||||
// contentPanel.add(lblNewLabel);
|
||||
|
||||
JButton btnNewButton = new JButton("Play again");
|
||||
btnNewButton.setMnemonic(KeyEvent.VK_ENTER);
|
||||
|
||||
btnNewButton.addActionListener(e -> { Main.startGame(); setVisible(false); dispose(); });
|
||||
// BLOß NICHT RAUSWERFEN
|
||||
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.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);
|
||||
contentPanel.add(btnNewButton);
|
||||
|
||||
// JButton btnClose = new JButton("Close game");
|
||||
//
|
||||
// btnClose.addActionListener(new ActionListener() {// Beginn Listener new game
|
||||
// public void actionPerformed(ActionEvent e) {
|
||||
// System.exit(0);
|
||||
// }
|
||||
// });
|
||||
//
|
||||
// btnClose.setFont(new Font("Times New Roman", Font.PLAIN, 15));
|
||||
// btnClose.setBounds(400, 500, 200, 100);
|
||||
// contentPanel.add(btnClose);
|
||||
//
|
||||
// tfName = new JTextField();
|
||||
// tfName.setFont(new Font("Times New Roman", Font.PLAIN, 15));
|
||||
// tfName.setBounds(102, 344, 257, 40);
|
||||
// contentPanel.add(tfName);
|
||||
// tfName.setColumns(10);
|
||||
//
|
||||
// JTextArea txtrBitteErstNamen = new JTextArea();
|
||||
// txtrBitteErstNamen.setVisible(false);
|
||||
// txtrBitteErstNamen.setBackground(UIManager.getColor("ScrollBar.foreground"));
|
||||
// txtrBitteErstNamen.setText("Bitte erst Namen \r\neingeben und\r\ndann
|
||||
// Speichern!!!!");
|
||||
// txtrBitteErstNamen.setBounds(468, 412, 155, 92);
|
||||
// contentPanel.add(txtrBitteErstNamen);
|
||||
// txtrBitteErstNamen.setVisible(false);
|
||||
//
|
||||
// JButton btnSaveHighscore = new JButton("Save Highscore");
|
||||
// btnSaveHighscore.setMnemonic(KeyEvent.VK_ENTER);
|
||||
// btnSaveHighscore.setIconTextGap(5);
|
||||
// btnSaveHighscore.setIcon(
|
||||
// new
|
||||
// ImageIcon(Endscreen.class.getResource("/javax/swing/plaf/metal/icons/ocean/floppy.gif")));
|
||||
// btnSaveHighscore.setFont(new Font("Times New Roman", Font.PLAIN, 15));
|
||||
//
|
||||
// btnSaveHighscore.addActionListener(new ActionListener() {// Beginn Listener
|
||||
// Save Highscore
|
||||
// public void actionPerformed(ActionEvent e) {
|
||||
// relocate(score1);
|
||||
// writeFiles();
|
||||
// table.updateUI();
|
||||
// }
|
||||
//
|
||||
// });
|
||||
//
|
||||
// btnSaveHighscore.setBounds(468, 344, 155, 50);
|
||||
// contentPanel.add(btnSaveHighscore);
|
||||
contentPanel.add(btnNewButton, BorderLayout.SOUTH);
|
||||
|
||||
JLabel lblDeinPunktestand = new JLabel("Dein Punktestand: " + String.valueOf(score));
|
||||
lblDeinPunktestand.setFont(new Font("Times New Roman", Font.PLAIN, 25));
|
||||
lblDeinPunktestand.setBounds(10, 45, 291, 50);
|
||||
contentPanel.add(lblDeinPunktestand);
|
||||
contentPanel.add(lblDeinPunktestand, BorderLayout.NORTH);
|
||||
|
||||
// JLabel lblYourName = new JLabel("Your Name:");
|
||||
// lblYourName.setFont(new Font("Times New Roman", Font.PLAIN, 15));
|
||||
// lblYourName.setBounds(10, 355, 82, 29);
|
||||
// contentPanel.add(lblYourName);
|
||||
//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();
|
||||
|
||||
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);
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
@ -172,88 +80,11 @@ public class Endscreen extends JDialog {
|
||||
* @return the highscore of the current game
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
public static int getScore() { return score; }
|
||||
public int getScore() { return score; }
|
||||
|
||||
/**
|
||||
* @param score the new highscore
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
public static void setScore(int score) { Endscreen.score = score; }
|
||||
/*
|
||||
* public static void readInHighscoresPoints() { try { // FileReader reads text
|
||||
* files in the default encoding. FileReader fileReader = new
|
||||
* FileReader(fileNamePoints);
|
||||
* // Always wrap FileReader in BufferedReader. BufferedReader bufferedReader =
|
||||
* new BufferedReader(fileReader);
|
||||
* currentIndex = Integer.parseInt(bufferedReader.readLine()); for (int i = 0; i
|
||||
* < currentIndex; i++) { highscorePoints[i]=
|
||||
* Integer.parseInt(bufferedReader.readLine()); } // Always close files.
|
||||
* bufferedReader.close(); fileReader.close(); } catch (FileNotFoundException
|
||||
* ex) { System.out.println("Error 404:File '" + fileNamePoints +
|
||||
* "' not found");
|
||||
* } catch (IOException ex) { System.out.println("Error reading file '" +
|
||||
* fileNamePoints + "'"); ex.printStackTrace(); } } private void
|
||||
* readInHighscoresPlayers(){ try { // FileReader reads text files in the
|
||||
* default encoding. FileReader fileReader = new FileReader(fileNamePlayers);
|
||||
* // Always wrap FileReader in BufferedReader. BufferedReader bufferedReader =
|
||||
* new BufferedReader(fileReader);
|
||||
* for (int i = 0; i < currentIndex; i++) { highscorePlayers[i]=
|
||||
* bufferedReader.readLine(); } // Always close files. bufferedReader.close();
|
||||
* fileReader.close(); } catch (FileNotFoundException ex) {
|
||||
* System.out.println("Error 404:File '" + fileNamePlayers + "' not found");
|
||||
* } catch (IOException ex) { System.out.println("Error reading file '" +
|
||||
* fileNamePlayers + "'"); ex.printStackTrace(); } } /* private void
|
||||
* writeFiles() { File dateiPoints = new File("." + File.separator +
|
||||
* fileNamePoints); FileWriter fwpoints = null; BufferedWriter bwpoints = null;
|
||||
* try { fwpoints = new FileWriter(dateiPoints); bwpoints = new
|
||||
* BufferedWriter(fwpoints); bwpoints.write(highscorePoints.length); for (int
|
||||
* i=0;i<highscorePoints.length;i++) { bwpoints.write(highscorePoints[i]); } }
|
||||
* catch (Exception e1) { e1.printStackTrace(); } finally { try {
|
||||
* bwpoints.close(); fwpoints.close(); alreadySaved = true; } catch (IOException
|
||||
* e2) { e2.printStackTrace(); } }
|
||||
* File dateiPlayers = new File("." + File.separator + fileNamePlayers);
|
||||
* FileWriter fwplayers = null; BufferedWriter bwplayers = null; try { fwplayers
|
||||
* = new FileWriter(dateiPlayers); bwplayers = new BufferedWriter(fwplayers);
|
||||
* for (int i=0;i<highscorePlayers.length;i++) {
|
||||
* bwplayers.write(highscorePlayers[i]); } } catch (Exception e1) {
|
||||
* e1.printStackTrace(); } finally { try { bwplayers.close(); fwplayers.close();
|
||||
* alreadySaved = true; } catch (IOException e2) { e2.printStackTrace(); } }
|
||||
* }
|
||||
* /** Launch the application.
|
||||
*/
|
||||
/*
|
||||
* public void relocate(int newScore) {
|
||||
* String newPlayer = new String(tfName.getText()); if (newPlayer.equals("")) {
|
||||
* txtrBitteErstNamen.setVisible(true); return; } else { sortFiles(newScore,
|
||||
* newPlayer); } }
|
||||
* private void sortFiles(int newScore, String newPlayer) { if
|
||||
* (highscorePoints.length==highscorePlayers.length&&
|
||||
* highscorePoints.length<=30) { for(int i=0;i<highscorePoints.length;i++) {
|
||||
* if(highscorePoints[i]<newScore) { int tmp=highscorePoints[i];
|
||||
* highscorePoints[i]=newScore; for(int k=i+1;k<highscorePoints.length;k++) {
|
||||
* int tmp2=highscorePoints[k]; highscorePoints[k]=tmp; tmp=tmp2; } String
|
||||
* temp=highscorePlayers[i]; highscorePlayers[i]=newPlayer; for(int
|
||||
* k=i+1;k<highscorePlayers.length;k++) { String temp2=highscorePlayers[k];
|
||||
* highscorePlayers[k]=temp; temp=temp2; } return; } } } else
|
||||
* if(highscorePoints.length==highscorePlayers.length&&highscorePoints.length>=
|
||||
* 30){ for (int i=30;i<highscorePoints.length;i++) { highscorePoints[i]=null;
|
||||
* highscorePlayers[i]=null; } for(int i=0;i<highscorePoints.length;i++) {
|
||||
* if(highscorePoints[i]<newScore) { int tmp=highscorePoints[i];
|
||||
* highscorePoints[i]=newScore; for(int k=i+1;k<highscorePoints.length;k++) {
|
||||
* int tmp2=highscorePoints[k]; highscorePoints[k]=tmp; tmp=tmp2; } String
|
||||
* temp=highscorePlayers[i]; highscorePlayers[i]=newPlayer; for(int
|
||||
* k=i+1;k<highscorePlayers.length;k++) { String temp2=highscorePlayers[k];
|
||||
* highscorePlayers[k]=temp; temp=temp2; } return; } } } }
|
||||
*/
|
||||
/*
|
||||
* private void relocate(int toCompare, boolean is30) { if(is30) { for(int i=0;
|
||||
* i<30;i++) { if(temp[i]<toCompare) { int tmp=temp[i]; temp[i]=toCompare;
|
||||
* for(int k=i+1;k<temp.length;k++) { int tmp2=temp[k]; temp[k]=tmp; tmp=tmp2; }
|
||||
* arrange(temp); return; } else { temp[30]=toCompare; arrange(temp); } } } else
|
||||
* { for(int i=0; i<temp.length;i++) { if(temp[i]<toCompare) { int tmp=temp[i];
|
||||
* temp[i]=toCompare; for(int k=i+1;k<temp.length;k++) { int tmp2=temp[k];
|
||||
* temp[k]=tmp; tmp=tmp2; } arrange(temp); return; } else { temp[30]=toCompare;
|
||||
* arrange(temp); }
|
||||
* } } }
|
||||
*/
|
||||
}
|
||||
public void setScore(int score) { this.score = score; }
|
||||
}
|
||||
|
14
src/main/dev/lh/ui/GameWindow.java
Normal file → Executable file
14
src/main/dev/lh/ui/GameWindow.java
Normal file → Executable file
@ -1,10 +1,6 @@
|
||||
package dev.lh.ui;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
|
||||
@ -44,7 +40,7 @@ public class GameWindow extends JFrame {
|
||||
setMinimumSize(size);
|
||||
setPreferredSize(size);
|
||||
setMaximumSize(size);
|
||||
setExtendedState(JFrame.MAXIMIZED_BOTH);
|
||||
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
|
||||
setResizable(false);
|
||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
|
||||
@ -89,7 +85,7 @@ public class GameWindow extends JFrame {
|
||||
});
|
||||
|
||||
Timer timer = new Timer(50,
|
||||
(evt) -> { s.nextFrame(); if (System.currentTimeMillis() >= foodFactory.getTimeOfNextFood()) newFood(); repaint(); });
|
||||
evt -> { s.nextFrame(); if (System.currentTimeMillis() >= foodFactory.getTimeOfNextFood()) newFood(); repaint(); });
|
||||
timer.start();
|
||||
|
||||
setVisible(true);
|
||||
@ -102,7 +98,7 @@ public class GameWindow extends JFrame {
|
||||
*/
|
||||
public void newFood() {
|
||||
foodFactory.generateFood();
|
||||
foodFactory.generateFoodLocation(super.getWidth(), super.getHeight());
|
||||
foodFactory.generateFoodLocation(getWidth(), getHeight());
|
||||
repaint();
|
||||
}
|
||||
|
||||
@ -112,4 +108,4 @@ public class GameWindow extends JFrame {
|
||||
* @since Snake 1.1
|
||||
*/
|
||||
public void close() { dispose(); }
|
||||
}
|
||||
}
|
||||
|
34
src/main/dev/lh/ui/StartScreen.java
Normal file → Executable file
34
src/main/dev/lh/ui/StartScreen.java
Normal file → Executable file
@ -4,10 +4,7 @@ import java.awt.EventQueue;
|
||||
import java.awt.Font;
|
||||
import java.awt.event.KeyEvent;
|
||||
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
|
||||
import dev.lh.Main;
|
||||
@ -47,35 +44,6 @@ public class StartScreen extends JFrame {
|
||||
});
|
||||
}
|
||||
|
||||
// public static void readInHighscores() {
|
||||
// try {
|
||||
// combination.clear();
|
||||
// // FileReader reads text files in the default encoding.
|
||||
// FileReader fileReader = new FileReader(fileName);
|
||||
//
|
||||
// // Always wrap FileReader in BufferedReader.
|
||||
// BufferedReader bufferedReader = new BufferedReader(fileReader);
|
||||
//
|
||||
// currentIndex = Integer.parseInt(bufferedReader.readLine());
|
||||
// for (int i = 0; i < currentIndex; i++) {
|
||||
// String[] spielerScore = new String[2];
|
||||
// spielerScore[0] = bufferedReader.readLine();
|
||||
// spielerScore[1] = bufferedReader.readLine();
|
||||
// combination.add(spielerScore);
|
||||
// }
|
||||
// // Always close files.
|
||||
// bufferedReader.close();
|
||||
// fileReader.close();
|
||||
// } catch (FileNotFoundException ex) {
|
||||
// System.out.println("Error 404:File '" + fileName + "' not found");
|
||||
//
|
||||
// } catch (IOException ex) {
|
||||
// System.out.println("Error reading file '" + fileName + "'");
|
||||
// ex.printStackTrace();
|
||||
//
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* Create the frame.
|
||||
*/
|
||||
|
0
src/main/resources/1211548-200.png
Normal file → Executable file
0
src/main/resources/1211548-200.png
Normal file → Executable file
Before Width: | Height: | Size: 5.4 KiB After Width: | Height: | Size: 5.4 KiB |
0
src/main/resources/Try_Again.jpg
Normal file → Executable file
0
src/main/resources/Try_Again.jpg
Normal file → Executable file
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 11 KiB |
Loading…
Reference in New Issue
Block a user