package cn.itcast.snake.controller;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import cn.itcast.snake.entities.Food;
import cn.itcast.snake.entities.GamePanel;
import cn.itcast.snake.entities.Ground;
import cn.itcast.snake.entities.Snake;
import cn.itcast.snake.listener.SnakeListener;
public class Controller extends KeyAdapter implements SnakeListener {
private Snake snake;
private Food food;
private Ground ground;
private GamePanel gamePanel;
public Controller(Snake snake, Food food, Ground ground, GamePanel gamePanel) {
super();
this.snake = snake;
this.food = food;
this.ground = ground;
this.gamePanel = gamePanel;
}
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
snake.changeDirection(Snake.UP);
break;
case KeyEvent.VK_DOWN:
snake.changeDirection(Snake.DOWN);
break;
case KeyEvent.VK_LEFT:
snake.changeDirection(Snake.LEFT);
break;
case KeyEvent.VK_RIGHT:
snake.changeDirection(Snake.RIGHT);
break;
}
}
public void snakeMoved(Snake snake) {
// TODO 自動產生方法存根
if (food.isSnakeEatFood(snake)) {
snake.eatFood();
food.newFood(ground.getPoint());
}
if (ground.isSnakeEatRock(snake)) {
snake.die();
}
if (snake.isEatBody()) {
snake.die();
}
gamePanel.display(snake, food, ground);
}
public void newGame() {
snake.start();
food.newFood(ground.getPoint());
}
}
package cn.itcast.snake.entities;
import java.awt.Graphics;
import java.awt.Point;
import cn.itcast.snake.util.Global;
public class Food extends Point {
public void newFood(Point p) {
this.setLocation(p);
}
public boolean isSnakeEatFood(Snake snake) {
System.out.println("isSnakeEatFood");
return this.equals(snake.getHead());
}
public void drawMe(Graphics g) {
System.out.println("FooddrawMe");
g.fill3DRect(x * Global.CELL_SIZE, y * Global.CELL_SIZE,
Global.CELL_SIZE, Global.CELL_SIZE, true);
}
}
package cn.itcast.snake.entities;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import cn.itcast.snake.util.Global;
public class GamePanel extends JPanel {
private Snake snake;
private Ground ground;
private Food food;
public void display(Snake snake, Food food, Ground ground) {
System.out.println("GamePanel display");
this.snake = snake;
this.food = food;
this.ground = ground;
this.repaint();
}
@Override
protected void paintComponent(Graphics g) {
g.setColor(Color.gray);
g.fillRect(0, 0, Global.WIDTH * Global.CELL_SIZE, Global.HEIGHT
* Global.CELL_SIZE);
if (ground != null && snake != null && food != null) {
this.snake.drawMe(g);
this.food.drawMe(g);
this.ground.drawMe(g);
}
}
}
、
package cn.itcast.snake.entities;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Random;
import cn.itcast.snake.util.Global;
public class Ground {
private int[][] rocks = new int[Global.WIDTH][Global.HEIGHT];
public Ground() {
for (int x = 0; x < Global.WIDTH; x++) {
rocks[x][0] = 1;
rocks[x][Global.HEIGHT - 1] = 1;
}
}
public boolean isSnakeEatRock(Snake snake) {
System.out.println("Ground isSnakeEatRock");
for (int x = 0; x < Global.WIDTH; x++) {
for (int y = 0; y < Global.HEIGHT; y++) {
if (rocks[x][y] == 1
&& (x == snake.getHead().x && y == snake.getHead().y))
return true;
}
}
return false;
}
public Point getPoint() {
Random random = new Random();
int x = 0, y = 0;
do {
x = random.nextInt(Global.WIDTH);
y = random.nextInt(Global.HEIGHT);
} while (rocks[x][y] == 1);
return new Point(x, y);
}
public void drawMe(Graphics g) {
System.out.println("Ground drawMe");
g.setColor(Color.DARK_GRAY);
for (int x = 0; x < Global.WIDTH; x++) {
for (int y = 0; y < Global.HEIGHT; y++) {
if (rocks[x][y] == 1) {
g.fill3DRect(x * Global.CELL_SIZE, y * Global.CELL_SIZE,
Global.CELL_SIZE, Global.CELL_SIZE, true);
}
}
}
}
}
package cn.itcast.snake.entities;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import cn.itcast.snake.listener.SnakeListener;
import cn.itcast.snake.util.Global;
public class Snake {
public static final int UP = -1;
public static final int DOWN = 1;
public static final int LEFT = 2;
public static final int RIGHT = -2;
private int oldDirection, newDirection;
private Point oldTail;
private LinkedList<Point> body = new LinkedList<Point>();
private Set<SnakeListener> listeners = new HashSet<SnakeListener>();
private boolean life;
public Snake() {
init();
}
public void init() {
int x = Global.WIDTH / 2;
int y = Global.HEIGHT / 2;
for (int i = 0; i < 3; i++) {
body.addLast(new Point(x--, y));
}
oldDirection = newDirection = RIGHT;
life = true;
}
public void die() {
life = false;
}
public void move() {
System.out.println("move");
if (!(oldDirection + newDirection == 0)) {
oldDirection = newDirection;
}
oldTail = body.removeLast();
int x = body.getFirst().x;
int y = body.getFirst().y;
switch (oldDirection) {
case UP:
y--;
if (y < 0) {
y = Global.HEIGHT - 1;
}
break;
case DOWN:
y++;
if (y >= Global.HEIGHT) {
y = 0;
}
break;
case LEFT:
x--;
if (x < 0) {
x = Global.WIDTH - 1;
}
break;
case RIGHT:
x++;
if (x > Global.WIDTH) {
x = 0;
}
break;
}
Point newHead = new Point(x, y);
body.addFirst(newHead);
}
public void changeDirection(int direction) {
System.out.println("changeDirection");
newDirection = direction;
}
public void eatFood() {
System.out.println("");
body.addLast(oldTail);
}
public boolean isEatBody() {
System.out.println("");
for (int i = 1; i < body.size(); i++) {
if (body.get(i).equals(this.getHead())) {
return true;
}
}
return false;
}
public void drawMe(Graphics g) {
System.out.println("");
g.setColor(new Color(0xcfcfcf));
for (Point p : body) {
g.fill3DRect(p.x * Global.CELL_SIZE, p.y * Global.CELL_SIZE,
Global.CELL_SIZE, Global.CELL_SIZE, true);
}
}
public Point getHead() {
return body.getFirst();
}
private class SnakeDriver implements Runnable {
public void run() {
while (life) {
move();
for (SnakeListener l : listeners) {
l.snakeMoved(Snake.this);
}
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void start() {
new Thread(new SnakeDriver()).start();
}
public void addSnakeListener(SnakeListener l) {
if (l != null) {
this.listeners.add(l);
}
}
}
package cn.itcast.snake.game;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import cn.itcast.snake.controller.Controller;
import cn.itcast.snake.entities.Food;
import cn.itcast.snake.entities.GamePanel;
import cn.itcast.snake.entities.Ground;
import cn.itcast.snake.entities.Snake;
import cn.itcast.snake.util.Global;
public class Game {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自動產生方法存根
Snake snake = new Snake();
Food food = new Food();
Ground ground = new Ground();
GamePanel gamePanel = new GamePanel();
Controller controller = new Controller(snake, food, ground, gamePanel);
javax.swing.JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gamePanel.setSize(Global.WIDTH * Global.CELL_SIZE, Global.HEIGHT
* Global.CELL_SIZE);
frame.setSize(Global.WIDTH * Global.CELL_SIZE + 10, Global.HEIGHT
* Global.CELL_SIZE + 35);
frame.add(gamePanel, BorderLayout.CENTER);
gamePanel.addKeyListener(controller);
snake.addSnakeListener(controller);
frame.addKeyListener(controller);
frame.setVisible(true);
controller.newGame();
}
}
package cn.itcast.snake.listener;
import cn.itcast.snake.entities.Snake;
public interface SnakeListener {
void snakeMoved(Snake snake);
}
package cn.itcast.snake.util;
public class Global {
public static final int CELL_SIZE = 20;
public static final int WIDTH = 15;
public static final int HEIGHT = 15;
}