public class Snake extends JPanel implements KeyListener, Runnable {
// Deklarieren der Attribute Field_Size,field,snake, direction,finished und
// paused
private int FIELD_SIZE = 20;
private LinkedList<Point> snake = new LinkedList<Point>();
private States[][] field;
private int direction = KeyEvent.VK_UP;
private boolean paused = false;
private boolean finished = false;
/**
* enum States enthaelt die 4 moeglichen Zustaende fuer ein Feld auf dem
* Spielfeld.
*/
private enum States {
EMPTY, SNAKE, APPLE, BLOCK;
}
public static void main(String[] args) {
new Snake();
}
/**
* Methode init() setzt alle Attribute der Klasse auf default-Werte, legt
* das Spielfeld an, platziert die Schlange darauf und verteilt 3 Aepfel.
*/
private void init() {
int highscore = 1;
int applesInGame = 2;
int applesEaten = 2;
int level = 1;
int points = 2;
// Zuweisung 2deminsionales array vom typ States
field = new States[FIELD_SIZE][FIELD_SIZE];
for (int a = 0; a < FIELD_SIZE; a++) {
for (int b = 0; b < FIELD_SIZE; b++) {
field[a][b] = States.EMPTY;
}
}
field[1][1] = States.SNAKE;
field[1][2] = States.SNAKE;
field[1][3] = States.SNAKE;
snake.addFirst(new Point(1, 1));
snake.addFirst(new Point(1, 2));
snake.addFirst(new Point(1, 3));
}
/**
* Methode reset() setzt das Spiel auf den Anfang zurück
*/
private void reset() {
}
/**
* Konstruktor für die Snake-Klasse
*/
public Snake() {
init();
paused = false;
JFrame frame = new JFrame("MySnake");
frame.setSize(200 + FIELD_SIZE * 20, 100 + FIELD_SIZE * 20);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addKeyListener(this);
frame.add(this);
frame.setVisible(true);
new Thread(this).start();
}
/**
* Notwendig zum Ablauf des Spiels, nicht veraendern!
*/
// @Override
public void run() {
long time = System.currentTimeMillis();
while (true) {
while (!paused && !finished) {
long time_elapsed = System.currentTimeMillis() - time;
try {
long sleep = Math.max(100 - time_elapsed, 60);
Thread.sleep(sleep);
} catch (InterruptedException e) {
}
time = System.currentTimeMillis();
nextTurn();
}
while (paused || finished) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* Verteilt zufaellig ein Element vom angegeben State auf dem Spielfeld.
*
* @param type
*/
public void addRandomised(States type) {
}
/**
* In der Methode nextTurn() werden alle Anweisungen ausgefuehrt, die fuer
* den naechsten Schritt im Spiel notwendig sind.
*/
public void nextTurn() {
// Lesen des Schlangen kopfes
private Snake head=null;
}
/**
* Zeichnet die aktuelle Spielsituation auf den Bildschirm.
*/
@Override
public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i <= (field.length) - 1; i++) {
for (int j = 0; j <= (field.length) - 1; j++) {
if (field[i][j] == States.EMPTY) {
g.setColor(Color.white);
g.fillRect(i * 20, j * 20, (i + 1) * 20, (j + 1) * 20);
}
if (field[i][j] == States.SNAKE) {
g.setColor(Color.BLACK);
g.fillOval(i * 70, j * 80, 20, 20);
g.fillOval(i * 70, j * 80, 20, 20);
g.fillOval(i * 20, j * 20, 20, 20);
}
if (field[i][j] == States.APPLE) {
g.setColor(Color.RED);
g.fillOval(50, 50, 20, 20);
}
if (field[i][j] == States.BLOCK) {
g.setColor(Color.GREEN);
g.fillRect(30, 30, 20, 20);
g.fillRect(90, 90, 20, 20);
g.fillRect(30, 90, 20, 20);
g.fillRect(60, 10, 20, 20);
}
}
}
g.drawRect(0, 0, 20 * (field.length), 20 * (field.length));
}
/**
* keyPressed() wird aufgerufen, wenn eine Taste gedrueckt wurde
*/
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP: {
if (direction != KeyEvent.VK_DOWN) {
direction = KeyEvent.VK_UP;
}
}
case KeyEvent.VK_DOWN: {
if (direction != KeyEvent.VK_UP) {
direction = KeyEvent.VK_DOWN;
}
}
case KeyEvent.VK_RIGHT: {
if (direction != KeyEvent.VK_LEFT) {
direction = KeyEvent.VK_RIGHT;
}
}
case KeyEvent.VK_LEFT: {
if (direction != KeyEvent.VK_RIGHT) {
direction = KeyEvent.VK_LEFT;
}
}
}
}