贪吃蛇snake Java实现(二)

package cn.tcc.snake.antition;

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.tcc.snake.listener.SnakeListener;
import cn.tcc.snake.util.Global;

public class Sanke {
    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 LinkedList<Point> body = new LinkedList<Point>();
    private Set<SnakeListener> Listeners = new HashSet<SnakeListener>(); // 监听器
    private Point oldTail;
    private boolean life;
    public void die(){
        life = false;
    }
    public Sanke(){
        init();
        
    }
    public void init(){
    int x = Global.WIDTH / 2;
    int y = Global.HEIGHT /2;
    for(int i=0;i<10;i++){
        body.addLast(new Point(x--,y));
        
    }
    newDirection=oldDirection = RIGHT;
    life = true;
    }
public void Move(){
    System.out.println("Snake 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 changDirection(int direction){
    System.out.println("Snake changDirection");
    
    newDirection = direction;
    
}

public void eatFood(){
    body.addLast(oldTail);
    System.out.println("Snake eatFood");
}
public boolean isEatBody(){
    System.out.println("Snake EatBody");
    for(int i=1;i<body.size();i++){
        if(body.get(i).equals(this.getHead())){
            return true;
        }
    }
    return false;
}
public void drawMe(Graphics g){
    g.setColor(Color.BLUE);
    for(Point p: body){
    g.fill3DRect(p.x*Global.CELL_SIZE, p.y*Global.CELL_SIZE,Global.CELL_SIZE,Global.CELL_SIZE, true);
    }
    
    System.out.println("snake drawme");
}
public Point getHead(){
    return body.getFirst();
}
public class SnakeDriver implements Runnable{

    @Override
    public void run() {
        
        while(life){
            Move();
            for(SnakeListener l: Listeners){
            l.SnakeMoved(Sanke.this);
            
            }
            try {
                Thread.sleep(300);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
}
public void start(){
    new Thread(new SnakeDriver()).start();
}
//添加监听器
public void addSnakeListener(SnakeListener l){
    if(l != null){
        this.Listeners.add(l);
    }
}
}

原文地址:https://www.cnblogs.com/chenligeng/p/7994877.html