package com.qdmmy6.snake;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.*;
import java.util.concurrent.*;
public class Snake implements Runnable {
private int length; //长度
private final int size; //正方形的边长
private long speed = 100; //蛇的运行速度
public static final int NEW = 10;
public static final int DIE = 20;
public static final int START = 30;
public static final int STOP = 40;
private int status = NEW; //蛇的状态
private LinkedList<Rectangle2D> body = new LinkedList<Rectangle2D>(); //蛇体
private int direction; //方向
private SnakeMap map; //地图
public Snake(SnakeMap map) {
this(10, 10, 100, KeyEvent.VK_DOWN,
new Point2D.Double(100, 100), map);
}
public Snake(int length, int size, long speed, int direction, Point2D point, SnakeMap map) {
this.length = length;
this.size = size;
this.speed = speed;
this.direction = direction;
this.map = map;
init(point);
}
// 排列矩形
private void init(Point2D point) {
for(int i = 0; i < length; i++) {
Rectangle2D rect = new Rectangle2D.Double(point.getX(),
point.getY(), size, size);
body.addLast(rect);
int x = 0, y = 0;
if(direction == KeyEvent.VK_UP) {
x = 0;
y = size;
} else if(direction == KeyEvent.VK_DOWN) {
x = 0;
y = -size;
} else if(direction == KeyEvent.VK_LEFT) {
x = -size;
y = 0;
} else if(direction == KeyEvent.VK_RIGHT) {
x = size;
y = 0;
}
point = new Point2D.Double(point.getX() + x, point.getY() + y);
}
}
// 返回头的左上角坐标
public Point2D getHead() {
double x = body.getFirst().getX();
double y = body.getFirst().getY();
return new Point2D.Double(x, y);
}
// 返回蛇体
public List<Rectangle2D> getBody() {
return this.body;
}
public void execute() {
if(this.status == NEW) {
start();
} else if(status == START) {
suspend();
} else if(status == STOP) {
resume();
}
}
// 启动任务
public void start() {
ExecutorService exec = Executors.newCachedThreadPool();
status = START;
exec.execute(this);
exec.shutdown();
}
// 停止运行
public void suspend() {
if(status == START) {
this.status = STOP;
}
}
// 恢复运行
public void resume() {
synchronized(this) {
if(status == STOP) {
this.status = START;
this.notifyAll();
}
}
}
// 杀死蛇
public void die() {
status = DIE;
}
public void run() {
try {
while(status != DIE) {
move(this.direction);
Thread.sleep(this.speed);
synchronized(this) {
if(status == STOP) {
this.wait();
}
}
}
} catch(Exception e) {}
}
// 移动方法
public void move(int direction) {
if(this.status == START) {
Rectangle2D rect = getRect(direction);
//System.out.println(rect + " [" + direction + "]");
if(rect != null) {
this.direction = direction;
body.removeLast();
body.addFirst(rect);
map.repaint();
}
}
}
private Rectangle2D getRect(int d) {
if((this.direction - d) % 2 == 0 && this.direction != d) {
return null;
}
Point2D point = getRect(getHead(), d);
return new Rectangle2D.Double(point.getX(), point.getY(), size, size);
}
private Point2D getRect(Point2D point, int d) {
int x = 0, y = 0;
if(d == KeyEvent.VK_UP) {
x = 0;
y = -size;
} else if(d == KeyEvent.VK_DOWN) {
x = 0;
y = size;
} else if(d == KeyEvent.VK_LEFT) {
x = -size;
y = 0;
} else if(d == KeyEvent.VK_RIGHT) {
x = size;
y = 0;
}
return new Point2D.Double(point.getX() + x, point.getY() + y);
}
public static void main(String[] args) {
//上 38 下 40 右 39 左 37
System.out.println(KeyEvent.VK_LEFT);
}
} |
|