//这个是节点,因为在游戏中,蛇是有节点组成,我这里用坐标代替了它的接点位置
public class Point {
public int x;
public int y;
public Point(){
}
public Point(int x,int y){
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
import java.util.LinkedList;
import java.util.Iterator;
public class Snake {
private LinkedList<Point> points;
//通过计算,向上时,X的坐标要-10.其余的类推
public static final int SNAKE_UP=-10;
public static final int SNAKE_DOWN=10;
public static final int SNAKE_LEFT=-1;
public static final int SNAKE_RIGHT=1;
public Snake(){
}
public Snake(LinkedList<Point> points){
this.points=points;
}
public void setPoint(LinkedList<Point> points){
this.points=points;
}
public LinkedList<Point> getPoint(){
return points;
}
//行走方法
public void go(int dir){
Point head=points.getFirst();//获取第一个点
int x=head.getX()+dir/10;
int y=head.getY()+dir%10;
Point dest=new Point(x,y);//获取目标坐标
points.addFirst(dest);
points.removeLast();
}
public boolean contains(int x,int y){
Iterator<Point> p=this.points.iterator();
while(p.hasNext()){
Point point=p.next();
if(point.getX()==x&&point.getY()==y){
return true;
}
}return false;
}
}
//游戏的画板
public class GamePanel {
private Snake snake;
public GamePanel(){
}
public GamePanel(Snake snake){
this.snake=snake;
}
public Snake getSnake() {
return snake;
}
public void setSnake(Snake snake) {
this.snake = snake;
}