Cleaned up project

This commit is contained in:
delvh
2020-03-31 18:13:19 +02:00
parent fb1bec2cce
commit 74924f769f
36 changed files with 31 additions and 427 deletions

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;
}
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;
/**
@ -67,17 +68,15 @@ public class Snake implements Updateable {
public Snake(int length) {
this.length = length;
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;
@ -90,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;
@ -101,8 +100,9 @@ public class Snake implements Updateable {
next = cur;
}
// 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 is outside of the screen or touches itself
if (checkSelfCollision()) gameOver();
// 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))) {
@ -117,7 +117,7 @@ public class Snake implements Updateable {
* @since Snake 1.1
*/
private void gameOver() {
Endscreen endscreen = new Endscreen(length);
endscreen = new Endscreen(length);
endscreen.setVisible(true);
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;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import dev.lh.Main;
@ -24,8 +20,9 @@ public class Endscreen extends JDialog {
private static final long serialVersionUID = -4457484397259161063L;
private final JPanel contentPanel = new JPanel();
private static int score = 0;
private final JPanel contentPanel = new JPanel();
private int score = 0;
private static final int goodOrBadResult = 200;
/**
* Create the dialog.
@ -33,7 +30,7 @@ public class Endscreen extends JDialog {
* @param score the highscore to set
*/
public Endscreen(int score) {
Endscreen.score = score;
this.score = score;
try {
// readInHighscoresPoints();
// readInHighscoresPlayers();
@ -55,7 +52,6 @@ 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);
@ -78,16 +74,13 @@ public class Endscreen extends JDialog {
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);
contentPanel.add(btnNewButton, BorderLayout.SOUTH);
// JButton btnClose = new JButton("Close game");
//
@ -139,14 +132,17 @@ public class Endscreen extends JDialog {
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);
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));
@ -161,10 +157,9 @@ public class Endscreen extends JDialog {
// 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);
// contentPanel.add(chckbxNewCheckBox, BorderLayout.CENTER);
// contentPanel.add(lblDasIstEin, BorderLayout.EAST);
setVisible(true);
}
@ -172,13 +167,13 @@ 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 void setScore(int score) { this.score = score; }
/*
* public static void readInHighscoresPoints() { try { // FileReader reads text
* 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);
setPreferredSize(size);
setMaximumSize(size);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
setResizable(false);
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