Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
3fe0bb72da | ||
|
f7fbc8b3e4 | ||
|
daf55abd93 |
@ -2,7 +2,7 @@
|
|||||||
My advanced Hello World program is a version of the old game Snake.
|
My advanced Hello World program is a version of the old game Snake.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
- normal playing experience of Snake
|
- normal playing experience of Snake but hardware-accelerated
|
||||||
- 5 different fruits that can be eaten to enlarge the Snake
|
- 5 different fruits that can be eaten to enlarge the Snake
|
||||||
- theoretically Start- and Endscreen, currently mostly unused
|
- theoretically Start- and Endscreen, currently mostly unused
|
||||||
- theoretically visual display whether a result is good or bad, commented out for now as it does not appear to work as expected
|
- theoretically visual display whether a result is good or bad, commented out for now as it does not appear to work as expected
|
||||||
@ -23,4 +23,4 @@ My advanced Hello World program is a version of the old game Snake.
|
|||||||
- as the name suggests, defines the visual appearance before and after games
|
- as the name suggests, defines the visual appearance before and after games
|
||||||
|
|
||||||
## JAR of the final version
|
## JAR of the final version
|
||||||
If you follow <a href="https://github.com/delvh/Snake/releases">**this link**</a> and click on release "Snake - almost finished", you can download the runnable JAR of how the completed project should look like. Unfortunately due to poor Version-Control-skills, the original code got lost and had to be rewritten, however it should resemble the executed code closely
|
If you follow <a href="https://github.com/delvh/Snake/releases">**this link**</a> and click on release "Snake - Rattlesnake version", you can download the runnable JAR of how the project currently looks like.
|
||||||
|
@ -28,7 +28,7 @@ public class Snake implements Updateable {
|
|||||||
* @author Leon Hofmeister
|
* @author Leon Hofmeister
|
||||||
* @since Snake 1.0
|
* @since Snake 1.0
|
||||||
*/
|
*/
|
||||||
public static enum Direction {
|
public enum Direction {
|
||||||
/**
|
/**
|
||||||
* Use if the snake should head left.
|
* Use if the snake should head left.
|
||||||
*/
|
*/
|
||||||
@ -50,10 +50,10 @@ public class Snake implements Updateable {
|
|||||||
DOWN;
|
DOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Endscreen endscreen;
|
private static Endscreen endscreen;
|
||||||
private Direction direction = Direction.RIGHT;
|
private Direction direction = Direction.RIGHT;
|
||||||
private int length;
|
private int length;
|
||||||
private List<Rectangle> tiles = new ArrayList<>();
|
private final List<Rectangle> tiles = new ArrayList<>();
|
||||||
|
|
||||||
private static final int TILE_SIZE = 10;
|
private static final int TILE_SIZE = 10;
|
||||||
|
|
||||||
@ -78,7 +78,7 @@ public class Snake implements Updateable {
|
|||||||
* @since Snake 1.0
|
* @since Snake 1.0
|
||||||
*/
|
*/
|
||||||
public void addLength(int additional) {
|
public void addLength(int additional) {
|
||||||
Rectangle last = tiles.get(tiles.size() - 1);
|
final Rectangle last = tiles.get(tiles.size() - 1);
|
||||||
for (int i = 0; i < additional; i++)
|
for (int i = 0; i < additional; i++)
|
||||||
tiles.add(last);
|
tiles.add(last);
|
||||||
length += additional;
|
length += additional;
|
||||||
@ -96,9 +96,9 @@ public class Snake implements Updateable {
|
|||||||
* @since Snake 1.1
|
* @since Snake 1.1
|
||||||
*/
|
*/
|
||||||
private void gameOver() {
|
private void gameOver() {
|
||||||
|
Main.getGame().close();
|
||||||
endscreen = new Endscreen(length);
|
endscreen = new Endscreen(length);
|
||||||
endscreen.setVisible(true);
|
endscreen.setVisible(true);
|
||||||
Main.getGame().close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package dev.lh.ui;
|
package dev.lh.ui;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.Font;
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
@ -20,9 +21,9 @@ public class Endscreen extends JDialog {
|
|||||||
|
|
||||||
private static final long serialVersionUID = -4457484397259161063L;
|
private static final long serialVersionUID = -4457484397259161063L;
|
||||||
|
|
||||||
private static final int goodOrBadResult = 200;
|
// private static final int goodOrBadResult = 200;
|
||||||
private final JPanel contentPanel = new JPanel();
|
private final JPanel contentPanel = new JPanel();
|
||||||
private final int score;
|
private final int score;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the dialog.
|
* Create the dialog.
|
||||||
@ -49,9 +50,10 @@ public class Endscreen extends JDialog {
|
|||||||
lblDeinPunktestand.setFont(new Font("Times New Roman", Font.PLAIN, 25));
|
lblDeinPunktestand.setFont(new Font("Times New Roman", Font.PLAIN, 25));
|
||||||
contentPanel.add(lblDeinPunktestand, BorderLayout.NORTH);
|
contentPanel.add(lblDeinPunktestand, BorderLayout.NORTH);
|
||||||
|
|
||||||
final Image resultImage = Toolkit.getDefaultToolkit()
|
// final Image resultImage = Toolkit.getDefaultToolkit()
|
||||||
.getImage(this.getClass().getResource(score < goodOrBadResult ? "/Try_Again.jpg" : "/1211548-200.png"));
|
// .getImage(this.getClass().getResource(score < goodOrBadResult ?
|
||||||
resultImage.flush();
|
// "/Try_Again.jpg" : "/1211548-200.png"));
|
||||||
|
// resultImage.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -4,9 +4,7 @@ import java.awt.EventQueue;
|
|||||||
import java.awt.Font;
|
import java.awt.Font;
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
|
|
||||||
import javax.swing.JButton;
|
import javax.swing.*;
|
||||||
import javax.swing.JFrame;
|
|
||||||
import javax.swing.JPanel;
|
|
||||||
import javax.swing.border.EmptyBorder;
|
import javax.swing.border.EmptyBorder;
|
||||||
|
|
||||||
import dev.lh.Main;
|
import dev.lh.Main;
|
||||||
@ -26,9 +24,7 @@ public class StartScreen extends JFrame {
|
|||||||
/**
|
/**
|
||||||
* Closes the application.
|
* Closes the application.
|
||||||
*/
|
*/
|
||||||
public static void close() {
|
public static void close() { System.exit(0); }
|
||||||
System.exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Launches Snake.
|
* Launches Snake.
|
||||||
@ -36,25 +32,22 @@ public class StartScreen extends JFrame {
|
|||||||
* @param args the program arguments
|
* @param args the program arguments
|
||||||
* @since Snake 1.0
|
* @since Snake 1.0
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) { EventQueue.invokeLater(StartScreen::new); }
|
||||||
EventQueue.invokeLater(StartScreen::new);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the frame.
|
* Create the frame.
|
||||||
*/
|
*/
|
||||||
public StartScreen() {
|
public StartScreen() {
|
||||||
setTitle("Snake - Startscreen");
|
setTitle("Snake - Startscreen");
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||||
setBounds(500, 200, 550, 550);
|
setBounds(500, 200, 550, 550);
|
||||||
|
|
||||||
JPanel contentPane = new JPanel();
|
final JPanel contentPane = new JPanel();
|
||||||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||||
setContentPane(contentPane);
|
setContentPane(contentPane);
|
||||||
|
|
||||||
JButton buPlay = new JButton("Start Game");
|
final JButton buPlay = new JButton("Start Game");
|
||||||
buPlay.setBounds(158, 197, 190, 131);
|
buPlay.setBounds(158, 197, 190, 131);
|
||||||
buPlay.setText("Play Again");
|
|
||||||
buPlay.setMnemonic(KeyEvent.VK_ENTER);
|
buPlay.setMnemonic(KeyEvent.VK_ENTER);
|
||||||
buPlay.setFont(new Font("Times New Roman", Font.PLAIN, 16));
|
buPlay.setFont(new Font("Times New Roman", Font.PLAIN, 16));
|
||||||
buPlay.addActionListener(a -> {
|
buPlay.addActionListener(a -> {
|
||||||
|
Loading…
Reference in New Issue
Block a user