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
36 changed files with 31 additions and 427 deletions
Showing only changes of commit 74924f769f - Show all commits

5
.classpath Normal file → Executable file
View File

@ -2,6 +2,7 @@
<classpath> <classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes> <attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
@ -12,10 +13,6 @@
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="src" path="src/main/resources"/> <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"> <classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes> <attributes>
<attribute name="test" value="true"/> <attribute name="test" value="true"/>

0
.gitignore vendored Normal file → Executable file
View File

0
.project Normal file → Executable file
View File

0
.settings/org.eclipse.jdt.core.prefs Normal file → Executable file
View File

0
.settings/org.eclipse.jdt.ui.prefs Normal file → Executable file
View File

0
.settings/org.eclipse.m2e.core.prefs Normal file → Executable file
View File

0
LICENSE Normal file → Executable file
View File

0
README.md Normal file → Executable file
View File

View 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
View File

@ -1 +0,0 @@
/bin/

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,11 +0,0 @@
package dev.lh.snake;
import java.awt.Graphics;
public interface Updateable {
void tick();
void render(Graphics g);
}

Binary file not shown.

Binary file not shown.

0
pom.xml Normal file → Executable file
View File

View File

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

0
src/main/dev/lh/FoodFactory.java Normal file → Executable file
View File

View File

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

0
src/main/dev/lh/Main.java Normal file → Executable file
View File

20
src/main/dev/lh/Snake.java Normal file → Executable file
View File

@ -52,10 +52,11 @@ public class Snake implements Updateable {
Down; Down;
} }
private static FoodFactory foodFactory = FoodFactory.getInstance();
private static Endscreen endscreen;
private Direction Richtung; private Direction Richtung;
private int length; private int length;
private List<Point> tiles = new ArrayList<>(); private List<Point> tiles = new ArrayList<>();
private static FoodFactory foodFactory = FoodFactory.getInstance();
private final int snakeSize = 10; private final int snakeSize = 10;
/** /**
@ -67,17 +68,15 @@ public class Snake implements Updateable {
public Snake(int length) { public Snake(int length) {
this.length = length; this.length = length;
Richtung = Direction.Right; Richtung = Direction.Right;
// adding the initial tiles of the snake
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 - snakeSize * i, 240));
}
}// End Constructor
@Override @Override
public void nextFrame() { public void nextFrame() {
int velX = 0, velY = 0; int velX = 0, velY = 0;
switch (Richtung) { switch (Richtung) {
case Up: case Up:
velY = -snakeSize; velY = -snakeSize;
break; break;
@ -90,7 +89,7 @@ public class Snake implements Updateable {
case Right: case Right:
velX = snakeSize; velX = snakeSize;
break; break;
}// switch }
Point next = (Point) tiles.get(0).clone(), cur; Point next = (Point) tiles.get(0).clone(), cur;
tiles.get(0).x += velX; tiles.get(0).x += velX;
tiles.get(0).y += velY; tiles.get(0).y += velY;
@ -101,8 +100,9 @@ public class Snake implements Updateable {
next = cur; next = cur;
} }
// case if the snake is outside of the screen or touches itself // case if snake is outside of the screen or touches itself
if (!Main.getGame().getBounds().contains(tiles.get(0)) || checkSelfCollision()) gameOver(); if (checkSelfCollision()) gameOver();
// if (!Main.getGame().getBounds().contains(tiles.get(0))) gameOver();
// case if snake eats food // 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))) {
@ -117,7 +117,7 @@ public class Snake implements Updateable {
* @since Snake 1.1 * @since Snake 1.1
*/ */
private void gameOver() { private void gameOver() {
Endscreen endscreen = new Endscreen(length); endscreen = new Endscreen(length);
endscreen.setVisible(true); endscreen.setVisible(true);
Main.getGame().close(); Main.getGame().close();
} }

View File

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

0
src/main/dev/lh/Updateable.java Normal file → Executable file
View File

43
src/main/dev/lh/ui/Endscreen.java Normal file → Executable file
View File

@ -1,13 +1,9 @@
package dev.lh.ui; package dev.lh.ui;
import java.awt.BorderLayout; import java.awt.*;
import java.awt.Font;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import javax.swing.JButton; import javax.swing.*;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import dev.lh.Main; import dev.lh.Main;
@ -24,8 +20,9 @@ public class Endscreen extends JDialog {
private static final long serialVersionUID = -4457484397259161063L; private static final long serialVersionUID = -4457484397259161063L;
private final JPanel contentPanel = new JPanel(); private final JPanel contentPanel = new JPanel();
private static int score = 0; private int score = 0;
private static final int goodOrBadResult = 200;
/** /**
* Create the dialog. * Create the dialog.
@ -33,7 +30,7 @@ public class Endscreen extends JDialog {
* @param score the highscore to set * @param score the highscore to set
*/ */
public Endscreen(int score) { public Endscreen(int score) {
Endscreen.score = score; this.score = score;
try { try {
// readInHighscoresPoints(); // readInHighscoresPoints();
// readInHighscoresPlayers(); // readInHighscoresPlayers();
@ -55,7 +52,6 @@ public class Endscreen extends JDialog {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
contentPanel.setLayout(null);
// JScrollPane scrollPane = new JScrollPane(); // JScrollPane scrollPane = new JScrollPane();
// scrollPane.setBounds(10, 412, 349, 238); // scrollPane.setBounds(10, 412, 349, 238);
// contentPanel.add(scrollPane); // contentPanel.add(scrollPane);
@ -78,16 +74,13 @@ public class Endscreen extends JDialog {
JButton btnNewButton = new JButton("Play again"); JButton btnNewButton = new JButton("Play again");
btnNewButton.setMnemonic(KeyEvent.VK_ENTER); btnNewButton.setMnemonic(KeyEvent.VK_ENTER);
btnNewButton.addActionListener(e -> { Main.startGame(); dispose(); });
btnNewButton.addActionListener(e -> { Main.startGame(); setVisible(false); dispose(); }); contentPanel.setLayout(new BorderLayout(0, 0));
// BLOß NICHT RAUSWERFEN btnNewButton.setIcon(new ImageIcon(ClassLoader.getSystemResource("/com/sun/javafx/webkit/prism/resources/mediaPlayDisabled.png")));
btnNewButton.setIconTextGap(5); 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"))); // 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); contentPanel.add(btnNewButton, BorderLayout.SOUTH);
contentPanel.add(btnNewButton);
// JButton btnClose = new JButton("Close game"); // JButton btnClose = new JButton("Close game");
// //
@ -139,14 +132,17 @@ public class Endscreen extends JDialog {
JLabel lblDeinPunktestand = new JLabel("Dein Punktestand: " + String.valueOf(score)); JLabel lblDeinPunktestand = new JLabel("Dein Punktestand: " + String.valueOf(score));
lblDeinPunktestand.setFont(new Font("Times New Roman", Font.PLAIN, 25)); lblDeinPunktestand.setFont(new Font("Times New Roman", Font.PLAIN, 25));
lblDeinPunktestand.setBounds(10, 45, 291, 50); contentPanel.add(lblDeinPunktestand, BorderLayout.NORTH);
contentPanel.add(lblDeinPunktestand);
// JLabel lblYourName = new JLabel("Your Name:"); // JLabel lblYourName = new JLabel("Your Name:");
// lblYourName.setFont(new Font("Times New Roman", Font.PLAIN, 15)); // lblYourName.setFont(new Font("Times New Roman", Font.PLAIN, 15));
// lblYourName.setBounds(10, 355, 82, 29); // lblYourName.setBounds(10, 355, 82, 29);
// contentPanel.add(lblYourName); // contentPanel.add(lblYourName);
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(""); // 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));
@ -161,10 +157,9 @@ public class Endscreen extends JDialog {
// chckbxNewCheckBox.setBounds(300, 200, 250, 210); // chckbxNewCheckBox.setBounds(300, 200, 250, 210);
// lblDasIstEin.setText("Das kannst du aber noch verbessern!"); // lblDasIstEin.setText("Das kannst du aber noch verbessern!");
// lblDasIstEin.setBounds(10, 100, 240, 50); // lblDasIstEin.setBounds(10, 100, 240, 50);
// contentPanel.add(lblDasIstEin);
// } // }
// contentPanel.add(chckbxNewCheckBox); // contentPanel.add(chckbxNewCheckBox, BorderLayout.CENTER);
// contentPanel.add(lblDasIstEin); // contentPanel.add(lblDasIstEin, BorderLayout.EAST);
setVisible(true); setVisible(true);
} }
@ -172,13 +167,13 @@ public class Endscreen extends JDialog {
* @return the highscore of the current game * @return the highscore of the current game
* @since Snake 1.0 * @since Snake 1.0
*/ */
public static int getScore() { return score; } public int getScore() { return score; }
/** /**
* @param score the new highscore * @param score the new highscore
* @since Snake 1.0 * @since Snake 1.0
*/ */
public static void setScore(int score) { Endscreen.score = score; } public void setScore(int score) { this.score = score; }
/* /*
* public static void readInHighscoresPoints() { try { // FileReader reads text * public static void readInHighscoresPoints() { try { // FileReader reads text
* files in the default encoding. FileReader fileReader = new * files in the default encoding. FileReader fileReader = new

2
src/main/dev/lh/ui/GameWindow.java Normal file → Executable file
View File

@ -44,7 +44,7 @@ public class GameWindow extends JFrame {
setMinimumSize(size); setMinimumSize(size);
setPreferredSize(size); setPreferredSize(size);
setMaximumSize(size); setMaximumSize(size);
setExtendedState(JFrame.MAXIMIZED_BOTH); setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
setResizable(false); setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE); setDefaultCloseOperation(EXIT_ON_CLOSE);

0
src/main/dev/lh/ui/StartScreen.java Normal file → Executable file
View File

0
src/main/resources/1211548-200.png Normal file → Executable file
View 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
View File

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB