Permalink
Browse files

merge snake branch

  • Loading branch information...
Jeky committed Dec 16, 2017
2 parents d10aa30 + fa2147b commit fc79d6609c7dd78fb6c909b2e713c343c8e8127e
@@ -0,0 +1,55 @@
package org.jam.core.game;

import java.awt.event.KeyEvent;

public enum Direction {

WEST(KeyEvent.VK_LEFT, -1, 0),
EAST(KeyEvent.VK_RIGHT, 1, 0),
NORTH(KeyEvent.VK_UP, 0, -1),
SOUTH(KeyEvent.VK_DOWN, 0, 1);

static {
WEST.left = SOUTH;
EAST.left = NORTH;
NORTH.left = WEST;
SOUTH.left = EAST;

WEST.right = NORTH;
EAST.right = SOUTH;
NORTH.right = EAST;
SOUTH.right = WEST;
}

private final int keyCode;
private final int horizontal;
private final int vertical;
private Direction left;
private Direction right;

Direction(int keyCode, int horizontal, int vertical) {
this.keyCode = keyCode;
this.horizontal = horizontal;
this.vertical = vertical;
}

public int getKeyCode() {
return keyCode;
}

public int getHorizontal() {
return horizontal;
}

public int getVertical() {
return vertical;
}

public Direction turnLeft() {
return left;
}

public Direction turnRight() {
return right;
}
}
@@ -1,5 +1,6 @@
package org.jam.core.game;

import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
@@ -24,20 +25,30 @@
private List<Sprite> sprites;

public Game() {
this(DEFAULT_FPS);
this(800, 600);
}

public Game(int fps) {
this(800, 600, fps);
}

public Game(int width, int height) {
this(width, height, DEFAULT_FPS);
}

public Game(int width, int height, int fps) {
animations = new LinkedList<>();
sprites = new LinkedList<>();
timeDelta = (long) 1000 / fps;
canvas = new GameCanvas(animations);
canvas.setPreferredSize(new Dimension(width, height));

start = false;

gameWindow = new JFrame();
gameWindow.add(canvas);
gameWindow.pack();
gameWindow.setResizable(false);
gameWindow.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -10,7 +10,7 @@
<version>0.1-SNAPSHOT</version>
<modules>
<module>core</module>
<module>demo</module>
<module>snake</module>
</modules>

<build>
File renamed without changes.
@@ -5,8 +5,8 @@
public class Main {

public static void main(String[] args) {
Game game = new Game();
game.addSprite(new Me());
Game game = new Game(Snake.SIZE * 40, Snake.SIZE * 40, 60);
game.addSprite(new Snake(10, 10));
game.start();
}
}
@@ -0,0 +1,122 @@
package org.jam.demo;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;

import org.jam.core.game.Direction;
import org.jam.core.game.animations.Sprite;

public class Snake extends Sprite {

public static final int SIZE = 24;
private static final Color HEAD_COLOR = Color.RED;
private static final Color BODY_BORDER_COLOR = Color.CYAN;
private static final Color BODY_COLOR = Color.BLUE;
private static final int FRAME_COUNT = 10;
private static final int INIT_LENGTH = 4;
private Direction direction;
private int frameCount;
private Deque<Point> body;

public Snake(int x, int y) {
this.x = x;
this.y = y;
width = SIZE;
height = SIZE;
direction = Direction.NORTH;
frameCount = 0;
body = new LinkedList<>();
for (int i = 0; i < INIT_LENGTH; i++) {
body.add(new Point(x, y));
}

// register keys
registerKeyPressedEvent(Direction.NORTH.getKeyCode(), Snake::toNorth);
registerKeyPressedEvent(Direction.SOUTH.getKeyCode(), Snake::toSouth);
registerKeyPressedEvent(Direction.EAST.getKeyCode(), Snake::toEast);
registerKeyPressedEvent(Direction.WEST.getKeyCode(), Snake::toWest);
}

@Override
public void update(Dimension canvasDimension) {
frameCount++;

if (frameCount == FRAME_COUNT / 2) {
int predictX = x + direction.getHorizontal();
int predictY = y + direction.getVertical();
int width = (int) (canvasDimension.getWidth() / SIZE);
int height = (int) (canvasDimension.getHeight() / SIZE);

if (hitBody(predictX, predictY) || hitWall(predictX, predictY, width, height)) {
System.out.println("Game Over");
System.exit(-1);
}
} else if (frameCount == FRAME_COUNT) {
frameCount = 0;

x += direction.getHorizontal();
y += direction.getVertical();
body.removeLast();
body.addFirst(new Point(x, y));
}
}


@Override
public void draw(Graphics2D g2d) {
// draw head
g2d.setColor(HEAD_COLOR);
Iterator<Point> iter = body.iterator();
drawSnakeNode(g2d, iter.next());
// draw body
iter.forEachRemaining(p -> {
g2d.setColor(BODY_COLOR);
drawSnakeNode(g2d, p);
});
}

private boolean hitBody(int x, int y) {
return body.stream()
.filter(p -> p.getX() == x && p.getY() == y)
.count() > 0;
}

private boolean hitWall(int x, int y, int width, int height) {
return x < 0 || y < 0 || x > width - 1 || y > height - 1;
}

private void toNorth() {
if (direction != Direction.NORTH && direction != Direction.SOUTH) {
direction = Direction.NORTH;
}
}

private void toSouth() {
if (direction != Direction.NORTH && direction != Direction.SOUTH) {
direction = Direction.SOUTH;
}
}

private void toEast() {
if (direction != Direction.EAST && direction != Direction.WEST) {
direction = Direction.EAST;
}
}

private void toWest() {
if (direction != Direction.EAST && direction != Direction.WEST) {
direction = Direction.WEST;
}
}

private void drawSnakeNode(Graphics2D g2d, Point p) {
g2d.fillRect(p.x * SIZE, p.y * SIZE, SIZE, SIZE);
g2d.setColor(BODY_BORDER_COLOR);
g2d.drawRect(p.x * SIZE, p.y * SIZE, SIZE, SIZE);
}
}

0 comments on commit fc79d66

Please sign in to comment.