Recovered most code of Snake, state: around v0.8.5 of before v1.0
only thing still not working: collision checking of foods (game crashes when touching food) code is mostly equal to previous version, minor improvements had to be made to ensure functionality
This commit is contained in:
parent
073d9fe188
commit
5e879e5a3d
27
.classpath
Normal file
27
.classpath
Normal file
@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry including="**/*.java" kind="src" output="target/classes" path="src/main">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</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"/>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target/
|
17
.project
Normal file
17
.project
Normal file
@ -0,0 +1,17 @@
|
||||
<?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>
|
8
.settings/org.eclipse.jdt.core.prefs
Normal file
8
.settings/org.eclipse.jdt.core.prefs
Normal file
@ -0,0 +1,8 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
||||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
|
||||
org.eclipse.jdt.core.compiler.release=disabled
|
||||
org.eclipse.jdt.core.compiler.source=1.8
|
4
.settings/org.eclipse.m2e.core.prefs
Normal file
4
.settings/org.eclipse.m2e.core.prefs
Normal file
@ -0,0 +1,4 @@
|
||||
activeProfiles=
|
||||
eclipse.preferences.version=1
|
||||
resolveWorkspaceProjects=true
|
||||
version=1
|
@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<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>
|
||||
|
BIN
Snake/jgoodies-forms-1.8.0.jar
Normal file
BIN
Snake/jgoodies-forms-1.8.0.jar
Normal file
Binary file not shown.
BIN
Snake/miglayout15-swing.jar
Normal file
BIN
Snake/miglayout15-swing.jar
Normal file
Binary file not shown.
33
pom.xml
Normal file
33
pom.xml
Normal file
@ -0,0 +1,33 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>Snake</groupId>
|
||||
<artifactId>Snake</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<build>
|
||||
<sourceDirectory>Snake/src</sourceDirectory>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>Snake/src</directory>
|
||||
<excludes>
|
||||
<exclude>**/*.java</exclude>
|
||||
</excludes>
|
||||
</resource>
|
||||
<resource>
|
||||
<directory>Snake/src/dev/lh/snake</directory>
|
||||
<excludes>
|
||||
<exclude>**/*.java</exclude>
|
||||
</excludes>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
21
src/main/dev/lh/Food.java
Normal file
21
src/main/dev/lh/Food.java
Normal file
@ -0,0 +1,21 @@
|
||||
package dev.lh;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
114
src/main/dev/lh/FoodFactory.java
Normal file
114
src/main/dev/lh/FoodFactory.java
Normal file
@ -0,0 +1,114 @@
|
||||
package dev.lh;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Point;
|
||||
import java.awt.Rectangle;
|
||||
|
||||
public class FoodFactory {
|
||||
|
||||
public static enum Food{
|
||||
white, yellow, orange, red, blue
|
||||
}
|
||||
|
||||
private static FoodFactory foodFactory = new FoodFactory();
|
||||
|
||||
private long timeOfNextFood;
|
||||
|
||||
|
||||
Point pFood = null;
|
||||
|
||||
|
||||
|
||||
private Food nextFood = Food.white;
|
||||
|
||||
public int rectangleSize = 6;
|
||||
|
||||
private FoodFactory() {}
|
||||
|
||||
public static FoodFactory getInstance() { return foodFactory; }
|
||||
|
||||
public Food generateFood() {
|
||||
int nextFoodIs = (int) Math.floor(Math.random()*5);
|
||||
switch (nextFoodIs) {
|
||||
|
||||
case 0: nextFood =Food.white; break;
|
||||
case 1: nextFood =Food.yellow; break;
|
||||
case 2: nextFood =Food.orange; break;
|
||||
case 3: nextFood =Food.red; break;
|
||||
case 4: nextFood =Food.blue; break;
|
||||
default: nextFood=generateFood();
|
||||
}
|
||||
rectangleSize= nextFood.ordinal()+2;
|
||||
setTimeToNextFoodMillis();
|
||||
return nextFood;
|
||||
}
|
||||
|
||||
public void setTimeToNextFoodMillis() {
|
||||
timeOfNextFood=System.currentTimeMillis()+(int) Math.round(Math.random() * 15000+1000);;
|
||||
}
|
||||
|
||||
public Food getNextFood() {
|
||||
return nextFood;
|
||||
}
|
||||
|
||||
public void setNext(Food nextFood) {
|
||||
this.nextFood = nextFood;
|
||||
}
|
||||
|
||||
public long getTimeOfNextFood() {
|
||||
return timeOfNextFood;
|
||||
}
|
||||
|
||||
public Point generateFoodLocation(int width, int height) {
|
||||
pFood=new Point((int) Math.round(Math.random() * width), (int) Math.round(Math.random() * height));
|
||||
if(pFood.x<50||pFood.x>width-50||pFood.y<50||pFood.y>height-50) {
|
||||
pFood.x = (pFood.x<50)?50:(pFood.x>width-50)?width-50:pFood.x;
|
||||
pFood.y = (pFood.y<50)?50:(pFood.y>height-50)?height-50:pFood.y;
|
||||
}
|
||||
return pFood;
|
||||
}
|
||||
|
||||
public int getRectangleSize() {
|
||||
return rectangleSize;
|
||||
}
|
||||
|
||||
public Point getFoodLocation() {
|
||||
return pFood;
|
||||
}
|
||||
|
||||
public void colorOfFood(Graphics g) {
|
||||
switch(nextFood) {
|
||||
case white: g.setColor(Color.white); break;
|
||||
case yellow:g.setColor(Color.yellow); break;
|
||||
case orange:g.setColor(Color.orange); break;
|
||||
case red: g.setColor(Color.red); break;
|
||||
case blue: g.setColor(Color.blue); break;
|
||||
}//switch
|
||||
}
|
||||
|
||||
public void paintFood(Graphics g) {
|
||||
colorOfFood(g);
|
||||
g.fillRect(pFood.x, pFood.y, 5*rectangleSize, 5*rectangleSize);
|
||||
}
|
||||
|
||||
public boolean checkCollision(Rectangle snakeHead) {
|
||||
int s = rectangleSize*5;
|
||||
Rectangle food = new Rectangle(pFood, new Dimension(s, s));
|
||||
return food.intersects(snakeHead);
|
||||
}
|
||||
|
||||
public int getAdditionalLength() {
|
||||
int snakeAdditionalLength = 0;
|
||||
switch(nextFood) {
|
||||
case white: snakeAdditionalLength =40;break;
|
||||
case yellow:snakeAdditionalLength =15;break;
|
||||
case orange:snakeAdditionalLength =6;break;
|
||||
case red: snakeAdditionalLength =2;break;
|
||||
case blue: snakeAdditionalLength =1;break;
|
||||
}//switch
|
||||
return snakeAdditionalLength;
|
||||
}
|
||||
|
||||
}
|
15
src/main/dev/lh/Handler.java
Normal file
15
src/main/dev/lh/Handler.java
Normal file
@ -0,0 +1,15 @@
|
||||
package dev.lh;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Handler {
|
||||
|
||||
List<Updateable> targets;
|
||||
|
||||
public Handler() {
|
||||
targets = new ArrayList<>();
|
||||
targets.add(new Snake(3));
|
||||
}
|
||||
|
||||
}
|
22
src/main/dev/lh/Main.java
Normal file
22
src/main/dev/lh/Main.java
Normal file
@ -0,0 +1,22 @@
|
||||
package dev.lh;
|
||||
|
||||
import dev.lh.ui.GameWindow;
|
||||
|
||||
public class Main {
|
||||
|
||||
private static GameWindow game;
|
||||
public static void main(String[] args) {
|
||||
//if wanted, the StartScreen can be added here
|
||||
startGame();
|
||||
}
|
||||
|
||||
public static void startGame() {
|
||||
game = new GameWindow("Snake");
|
||||
game.setVisible(true);
|
||||
}
|
||||
|
||||
public static GameWindow getGame() {
|
||||
return game;
|
||||
}
|
||||
|
||||
}
|
94
src/main/dev/lh/Snake.java
Normal file
94
src/main/dev/lh/Snake.java
Normal file
@ -0,0 +1,94 @@
|
||||
package dev.lh;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Point;
|
||||
import java.awt.Rectangle;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import dev.lh.ui.GameWindow;
|
||||
|
||||
public class Snake implements Updateable {
|
||||
|
||||
public static enum Direction{
|
||||
Left, Right, Up, Down;
|
||||
}
|
||||
private Direction Richtung;
|
||||
private int length;
|
||||
private List<Point> tiles = new ArrayList<>();
|
||||
private static FoodFactory foodFactory = FoodFactory.getInstance();
|
||||
private final int snakeSize = 10;
|
||||
|
||||
public Snake(int length) {
|
||||
this.length = length;
|
||||
Richtung = Direction.Left;
|
||||
|
||||
for(int i = 0; i<length;i++) {
|
||||
tiles.add(new Point(320-50*i, 240));
|
||||
}
|
||||
|
||||
}//End Constructor
|
||||
@Override
|
||||
public void tick() {
|
||||
int velX = 0, velY = 0;
|
||||
switch(Richtung) {
|
||||
|
||||
case Up:
|
||||
velY=-snakeSize;
|
||||
break;
|
||||
case Down:
|
||||
velY=snakeSize;
|
||||
break;
|
||||
case Left:
|
||||
velX=-snakeSize;
|
||||
break;
|
||||
case Right:
|
||||
velX=snakeSize;
|
||||
break;
|
||||
}//switch
|
||||
Point next = (Point) tiles.get(0).clone(), cur;
|
||||
tiles.get(0).x+=velX;
|
||||
tiles.get(0).y+=velY;
|
||||
|
||||
|
||||
for(int i = 1;i<length;i++) {
|
||||
cur = tiles.get(i);
|
||||
tiles.set(i,(Point) next.clone());
|
||||
next = cur;
|
||||
}//for
|
||||
// if(tiles.get(0).x<=0||tiles.get(0).x>=)
|
||||
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
|
||||
@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
|
||||
|
||||
public Direction getRichtung() {
|
||||
return Richtung;
|
||||
}
|
||||
|
||||
public void setRichtung(Direction richtung) {
|
||||
Richtung = richtung;
|
||||
}
|
||||
|
||||
public void addLength(int additional) {
|
||||
for(int i=0;i<additional;i++) {
|
||||
tiles.add(null);
|
||||
}
|
||||
length+=additional;
|
||||
}
|
||||
}
|
19
src/main/dev/lh/Spawner.java
Normal file
19
src/main/dev/lh/Spawner.java
Normal file
@ -0,0 +1,19 @@
|
||||
package dev.lh;
|
||||
|
||||
import java.awt.Graphics;
|
||||
|
||||
public class Spawner implements Updateable{
|
||||
|
||||
@Override
|
||||
public void tick() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(Graphics g) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
11
src/main/dev/lh/Updateable.java
Normal file
11
src/main/dev/lh/Updateable.java
Normal file
@ -0,0 +1,11 @@
|
||||
package dev.lh;
|
||||
|
||||
import java.awt.Graphics;
|
||||
|
||||
public interface Updateable {
|
||||
|
||||
void tick();
|
||||
|
||||
void render(Graphics g);
|
||||
|
||||
}
|
282
src/main/dev/lh/ui/Endscreen.java
Normal file
282
src/main/dev/lh/ui/Endscreen.java
Normal file
@ -0,0 +1,282 @@
|
||||
package dev.lh.ui;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Font;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
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.border.EmptyBorder;
|
||||
|
||||
import dev.lh.Main;
|
||||
|
||||
public class Endscreen extends JDialog {
|
||||
|
||||
|
||||
private static final long serialVersionUID = -4457484397259161063L;
|
||||
|
||||
private final JPanel contentPanel = new JPanel();
|
||||
public static int currentIndex = 0;
|
||||
public static boolean alreadySaved = false;
|
||||
public static int score = 0;
|
||||
final int goodOrBadResult = 250;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Beginn der Hauptmethode/Erstellung des JDialogs
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
Endscreen dialog = new Endscreen(score);
|
||||
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
||||
dialog.setVisible(true);
|
||||
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
|
||||
@Override
|
||||
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
|
||||
Thread.getAllStackTraces().forEach((thread, stackTraceElement) -> thread.interrupt());
|
||||
System.exit(0);
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the dialog.
|
||||
*/
|
||||
|
||||
public Endscreen(int score1) {
|
||||
setScore(score1);
|
||||
try {
|
||||
// readInHighscoresPoints();
|
||||
// readInHighscoresPlayers();
|
||||
|
||||
setTitle("Endscreen");
|
||||
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
||||
setBounds(100, 100, 700, 700);
|
||||
getContentPane().setLayout(new BorderLayout());
|
||||
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
getContentPane().add(contentPanel, BorderLayout.CENTER);
|
||||
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(new ActionListener() {// Beginn Listener new game
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Main.startGame();
|
||||
setVisible(false);
|
||||
dispose();
|
||||
}
|
||||
});
|
||||
//BLOß NICHT RAUSWERFEN
|
||||
btnNewButton.setIconTextGap(5);
|
||||
btnNewButton.setIcon(new ImageIcon(
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
// 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);
|
||||
|
||||
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(Endscreen.class.getResource("/dev/lh/snake/1211548-200.png")));
|
||||
chckbxNewCheckBox.setBounds(300, 200, 200, 200);
|
||||
lblDasIstEin.setBounds(10, 100, 212, 50);
|
||||
} else {
|
||||
chckbxNewCheckBox.setIcon(new ImageIcon(Endscreen.class.getResource("/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);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static int getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
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); }
|
||||
*
|
||||
* } } }
|
||||
*/
|
||||
}
|
97
src/main/dev/lh/ui/GameWindow.java
Normal file
97
src/main/dev/lh/ui/GameWindow.java
Normal file
@ -0,0 +1,97 @@
|
||||
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.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.Timer;
|
||||
|
||||
import dev.lh.FoodFactory;
|
||||
import dev.lh.Snake;
|
||||
import dev.lh.Snake.Direction;
|
||||
|
||||
public class GameWindow extends JFrame {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private Snake s = new Snake(7);
|
||||
private FoodFactory foodFactory = FoodFactory.getInstance();
|
||||
|
||||
public GameWindow(String title) {
|
||||
super(title);
|
||||
newFood();
|
||||
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
|
||||
setBounds(new Rectangle(size));
|
||||
setLocationRelativeTo(null);
|
||||
setMinimumSize(size);
|
||||
setPreferredSize(size);
|
||||
setMaximumSize(size);
|
||||
setExtendedState(JFrame.MAXIMIZED_BOTH);
|
||||
setResizable(false);
|
||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
|
||||
|
||||
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, super.getWidth(), super.getHeight());
|
||||
s.render(g);
|
||||
foodFactory.paintFood(g);
|
||||
}
|
||||
});
|
||||
|
||||
addKeyListener(new KeyAdapter() {
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
super.keyPressed(e);
|
||||
switch (e.getKeyCode()) {
|
||||
case KeyEvent.VK_W:
|
||||
case KeyEvent.VK_UP:
|
||||
s.setRichtung(Direction.Up);
|
||||
break;
|
||||
case KeyEvent.VK_A:
|
||||
case KeyEvent.VK_LEFT:
|
||||
s.setRichtung(Direction.Left);
|
||||
break;
|
||||
case KeyEvent.VK_S:
|
||||
case KeyEvent.VK_DOWN:
|
||||
s.setRichtung(Direction.Down);
|
||||
break;
|
||||
case KeyEvent.VK_D:
|
||||
case KeyEvent.VK_RIGHT:
|
||||
s.setRichtung(Direction.Right);
|
||||
break;
|
||||
}// switch
|
||||
}// keypressed
|
||||
});// keylistener
|
||||
|
||||
Timer timer = new Timer(50, (evt) -> {
|
||||
s.tick();
|
||||
if(System.currentTimeMillis()>=foodFactory.getTimeOfNextFood()) newFood();
|
||||
repaint();
|
||||
});
|
||||
timer.start();
|
||||
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
public void newFood() {
|
||||
foodFactory.generateFood();
|
||||
foodFactory.generateFoodLocation(super.getWidth(), super.getHeight());
|
||||
}
|
||||
|
||||
public void close() {
|
||||
dispose();
|
||||
}
|
||||
}
|
111
src/main/dev/lh/ui/StartScreen.java
Normal file
111
src/main/dev/lh/ui/StartScreen.java
Normal file
@ -0,0 +1,111 @@
|
||||
package dev.lh.ui;
|
||||
|
||||
import java.awt.EventQueue;
|
||||
import java.awt.Font;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
|
||||
import dev.lh.Main;
|
||||
|
||||
public class StartScreen extends JFrame {
|
||||
|
||||
private static final long serialVersionUID = 6055940532003735543L;
|
||||
private JPanel contentPane;
|
||||
public static int currentIndex = 0;
|
||||
public static List<String[]> combination = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Launch the application.
|
||||
*/
|
||||
public static void close() {
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
EventQueue.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
StartScreen frame = new StartScreen();
|
||||
frame.setVisible(true);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 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.
|
||||
*/
|
||||
public StartScreen() {
|
||||
try {
|
||||
// readInHighscores();
|
||||
setTitle("Snake - Startscreen");
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setBounds(500, 200, 550, 550);
|
||||
contentPane = new JPanel();
|
||||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
setContentPane(contentPane);
|
||||
|
||||
JButton buPlay = new JButton("Start Game");
|
||||
buPlay.setBounds(158,197, 190, 131);
|
||||
buPlay.setIcon(new ImageIcon(
|
||||
StartScreen.class.getResource("/com/sun/javafx/webkit/prism/resources/mediaPlayDisabled.png")));
|
||||
buPlay.setMnemonic(KeyEvent.VK_ENTER);
|
||||
buPlay.setFont(new Font("Times New Roman", Font.PLAIN, 16));
|
||||
|
||||
buPlay.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent a) {
|
||||
|
||||
Main.startGame();
|
||||
setVisible(false);
|
||||
dispose();
|
||||
System.gc();
|
||||
}
|
||||
});
|
||||
contentPane.setLayout(null);
|
||||
|
||||
} catch (Exception e) {
|
||||
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
BIN
src/main/resources/1211548-200.png
Normal file
BIN
src/main/resources/1211548-200.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 5.4 KiB |
BIN
src/main/resources/Try_Again.jpg
Normal file
BIN
src/main/resources/Try_Again.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 11 KiB |
Loading…
Reference in New Issue
Block a user