From 073d9fe1886434ff6db6cce9664580f7535e2c91 Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 9 Nov 2019 23:11:16 +0100 Subject: [PATCH] Finally found source code Unfortunately, due to major complications while trying to upload it, the source code will have lost most of its usability --- Snake/.classpath | 6 ++ Snake/.gitignore | 1 + Snake/.project | 17 ++++ Snake/.settings/org.eclipse.jdt.core.prefs | 11 +++ Snake/src/dev/lh/snake/Food.java | 21 +++++ Snake/src/dev/lh/snake/GameWindow.java | 98 ++++++++++++++++++++++ Snake/src/dev/lh/snake/Handler.java | 15 ++++ Snake/src/dev/lh/snake/Main.java | 13 +++ Snake/src/dev/lh/snake/Snake.java | 76 +++++++++++++++++ Snake/src/dev/lh/snake/Spawner.java | 19 +++++ Snake/src/dev/lh/snake/Updateable.java | 11 +++ 11 files changed, 288 insertions(+) create mode 100644 Snake/.classpath create mode 100644 Snake/.gitignore create mode 100644 Snake/.project create mode 100644 Snake/.settings/org.eclipse.jdt.core.prefs create mode 100644 Snake/src/dev/lh/snake/Food.java create mode 100644 Snake/src/dev/lh/snake/GameWindow.java create mode 100644 Snake/src/dev/lh/snake/Handler.java create mode 100644 Snake/src/dev/lh/snake/Main.java create mode 100644 Snake/src/dev/lh/snake/Snake.java create mode 100644 Snake/src/dev/lh/snake/Spawner.java create mode 100644 Snake/src/dev/lh/snake/Updateable.java diff --git a/Snake/.classpath b/Snake/.classpath new file mode 100644 index 0000000..e461bea --- /dev/null +++ b/Snake/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/Snake/.gitignore b/Snake/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/Snake/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/Snake/.project b/Snake/.project new file mode 100644 index 0000000..919eb8f --- /dev/null +++ b/Snake/.project @@ -0,0 +1,17 @@ + + + Snake + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/Snake/.settings/org.eclipse.jdt.core.prefs b/Snake/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..bb35fa0 --- /dev/null +++ b/Snake/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +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 diff --git a/Snake/src/dev/lh/snake/Food.java b/Snake/src/dev/lh/snake/Food.java new file mode 100644 index 0000000..c380bf4 --- /dev/null +++ b/Snake/src/dev/lh/snake/Food.java @@ -0,0 +1,21 @@ +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); + } + + +} diff --git a/Snake/src/dev/lh/snake/GameWindow.java b/Snake/src/dev/lh/snake/GameWindow.java new file mode 100644 index 0000000..0fb25fa --- /dev/null +++ b/Snake/src/dev/lh/snake/GameWindow.java @@ -0,0 +1,98 @@ +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 \ No newline at end of file diff --git a/Snake/src/dev/lh/snake/Handler.java b/Snake/src/dev/lh/snake/Handler.java new file mode 100644 index 0000000..db7b371 --- /dev/null +++ b/Snake/src/dev/lh/snake/Handler.java @@ -0,0 +1,15 @@ +package dev.lh.snake; + +import java.util.ArrayList; +import java.util.List; + +public class Handler { + + List targets; + + public Handler() { + targets = new ArrayList<>(); + targets.add(new Snake(3)); + } + +} diff --git a/Snake/src/dev/lh/snake/Main.java b/Snake/src/dev/lh/snake/Main.java new file mode 100644 index 0000000..7a6b3cd --- /dev/null +++ b/Snake/src/dev/lh/snake/Main.java @@ -0,0 +1,13 @@ +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); + } + +} diff --git a/Snake/src/dev/lh/snake/Snake.java b/Snake/src/dev/lh/snake/Snake.java new file mode 100644 index 0000000..6c00f4e --- /dev/null +++ b/Snake/src/dev/lh/snake/Snake.java @@ -0,0 +1,76 @@ +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