Finally found source code
Unfortunately, due to major complications while trying to upload it, the source code will have lost most of its usability
This commit is contained in:
parent
6d162adde1
commit
073d9fe188
6
Snake/.classpath
Normal file
6
Snake/.classpath
Normal file
@ -0,0 +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 kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
1
Snake/.gitignore
vendored
Normal file
1
Snake/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/bin/
|
17
Snake/.project
Normal file
17
Snake/.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>
|
11
Snake/.settings/org.eclipse.jdt.core.prefs
Normal file
11
Snake/.settings/org.eclipse.jdt.core.prefs
Normal file
@ -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
|
21
Snake/src/dev/lh/snake/Food.java
Normal file
21
Snake/src/dev/lh/snake/Food.java
Normal file
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
98
Snake/src/dev/lh/snake/GameWindow.java
Normal file
98
Snake/src/dev/lh/snake/GameWindow.java
Normal file
@ -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
|
15
Snake/src/dev/lh/snake/Handler.java
Normal file
15
Snake/src/dev/lh/snake/Handler.java
Normal file
@ -0,0 +1,15 @@
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
13
Snake/src/dev/lh/snake/Main.java
Normal file
13
Snake/src/dev/lh/snake/Main.java
Normal file
@ -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);
|
||||
}
|
||||
|
||||
}
|
76
Snake/src/dev/lh/snake/Snake.java
Normal file
76
Snake/src/dev/lh/snake/Snake.java
Normal file
@ -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<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;
|
||||
}
|
||||
}
|
19
Snake/src/dev/lh/snake/Spawner.java
Normal file
19
Snake/src/dev/lh/snake/Spawner.java
Normal file
@ -0,0 +1,19 @@
|
||||
package dev.lh.snake;
|
||||
|
||||
import java.awt.Graphics;
|
||||
|
||||
public class Spawner implements Updateable{
|
||||
|
||||
@Override
|
||||
public void tick() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(Graphics g) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
11
Snake/src/dev/lh/snake/Updateable.java
Normal file
11
Snake/src/dev/lh/snake/Updateable.java
Normal file
@ -0,0 +1,11 @@
|
||||
package dev.lh.snake;
|
||||
|
||||
import java.awt.Graphics;
|
||||
|
||||
public interface Updateable {
|
||||
|
||||
void tick();
|
||||
|
||||
void render(Graphics g);
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user