Y += Node.H;
break;
}
body.add(0, new Node(X, Y));
// ---------------去掉蛇尾
body.remove(body.size() - 1);
}
}
}
// ---------组成蛇身的单位,食物
class Node {
public static final int W = 20;
public static final int H = 20;
int x;
int y;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
}
// ------画板
class SnakePanel extends JPanel {
Snake snake;
public SnakePanel(Snake snake) {
this.snake = snake;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Node node = null;
for (int i = 0; i < snake.body.size(); i++) {// ---黄绿间隔画蛇身 if (i % 2 == 0)
g.setColor(Color.green);
else
g.setColor(Color.yellow);
node = snake.body.get(i);
g.fillRect(node.x, node.y, node.H,
*******************试用*********************
}
node = snake.food; node.W);//