我就是不知道该去修改哪块 现在能变长 就是不能转弯 我觉得是键盘监听那儿修改 你觉得呢。每个节点是对象 NEW的时候已经赋了跟头结点一样的方向 [其他解释] 我估计知道你错误的原因了。 你把你new一个新对象的那段代码以及创建新对象的那个构造器发上来。 我估计你付给这个新对象的(跟头结点一样的方向 )是一个定值,或者是一个非动态更新的引用。 [其他解释] //往蛇的头部添加一个新的节点 public void addToHead(){ Node n = null; switch(head.dir){ case LEFT: n = new Node(head.row,head.col-1,head.dir); break; case RIGHT: n = new Node(head.row,head.col+1,head.dir); break; case UP: n = new Node(head.row-1,head.col,head.dir); break; case DOWN: n = new Node(head.row+1,head.col,head.dir); break; } n.next = head; head.pre = n; head = n;
}
//构造方法
public class Node {
int width = Yard.BLOCK_SIZE; int hight = Yard.BLOCK_SIZE; int row, col;// 出现的位置
Node pre, next = null; Direction dir = Direction.LEFT;
private Node(int row, int col, Direction dir) { this.row = row; this.col = col; this.dir = dir; } public int getRow() { return row; }
public void setRow(int row) { this.row = row; }
public int getCol() { return col; }
public void setCol(int col) { this.col = col; }
private void draw(Graphics g) { Color c = g.getColor(); g.setColor(Color.BLUE); g.fillRect(Yard.BLOCK_SIZE * col, Yard.BLOCK_SIZE * row, width,hight); g.setColor(c); }