贪吃蛇的实现(代码 4)

dalie9 2006-05-10 02:49:04
package gluttonous_snake;

import java.awt.*;
import java.util.*;

class Snake extends LinkedList {
private static final long serialVersionUID = 1438756131832994052L;
public int snakeDirection = 2;
public int snakeReDirection = 4;

public Snake() {
this.add(new Point(3, 3));
this.add(new Point(4, 3));
this.add(new Point(5, 3));
this.add(new Point(6, 3));
this.add(new Point(7, 3));
this.add(new Point(8, 3));
this.add(new Point(9, 3));
this.add(new Point(10, 3));
}

public void changeDirection(Point temp, int direction) {
this.snakeDirection = direction;
switch (direction) {
case 1:// up
this.snakeReDirection = 3;
this.add(new Point(temp.x, temp.y - 1));
break;
case 2:// right
this.snakeReDirection = 4;
this.add(new Point(temp.x + 1, temp.y));
break;
case 3:// down
this.snakeReDirection = 1;
this.add(new Point(temp.x, temp.y + 1));
break;
case 4:// left
this.snakeReDirection = 2;
this.add(new Point(temp.x - 1, temp.y));
break;
}
}
//判断食物是否被吃掉了
public boolean checkBeanIn(Point bean) {
Point temp = (Point) this.getLast();
if (temp.equals(bean)) {
return true;
}
return false;
}

public void removeTail() {
this.remove(0);
}

public void drawSnake(Graphics g, int singleWidthX, int singleHeightY,
int cooPos) {
g.setColor(ColorGroup.COLOR_SNAKE);
Iterator snakeSq = this.iterator();
while (snakeSq.hasNext()) {
Point tempPoint = (Point) snakeSq.next();
this.drawSnakePiece(g, tempPoint.x, tempPoint.y, singleWidthX,
singleHeightY, cooPos);
}
}

public void drawSnakePiece(Graphics g, int temp1, int temp2,
int singleWidthX, int singleHeightY, int cooPos) {
g.fillRoundRect(singleWidthX * temp1 + 1, singleHeightY * temp2 + 1,
singleWidthX - 2, singleHeightY - 2, cooPos, cooPos);
}

public void clearEndSnakePiece(Graphics g, int temp1, int temp2,
int singleWidthX, int singleHeightY, int cooPos) {
g.setColor(ColorGroup.COLOR_BACK);
g.fillRoundRect(singleWidthX * temp1 + 1, singleHeightY * temp2 + 1,
singleWidthX - 2, singleHeightY - 2, cooPos, cooPos);
}
}
...全文
57 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧