📄 snake.java
字号:
package cn.edu.usc.snake.entities;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import cn.edu.usc.snake.listener.SnakeListener;
import cn.edu.usc.snake.util.ConstValue;
import cn.edu.usc.snake.util.Direction;
public class Snake {
private LinkedList<Point> body;
private Direction oldDirection, newDirection;
private Set<SnakeListener> listeners = new HashSet<SnakeListener>();
private Point oldTail;
private boolean alive;
private int eatFoodCount;
public Snake() {
initSnake();
}
/**
* 初始化蛇
*/
public void initSnake() {
int x = 1;
int y = 0;
alive = true;
eatFoodCount = 0;
body = new LinkedList<Point>();
for(int i = 0; i < 3; i ++) {
body.addFirst(new Point(x, y ++));
}
this.oldDirection = this.newDirection = Direction.D;
}
/**
* 移动(走一步)
* 增加蛇头,去掉蛇尾
*/
public void move() {
if(!this.checkInvalidDirection(this.oldDirection, this.newDirection)) {
this.oldDirection = this.newDirection;
}
//去掉蛇尾
oldTail = body.removeLast();
int x = body.getFirst().x;
int y = body.getFirst().y;
switch(oldDirection) {
case L:
x --;
if(x < 0) {
x = ConstValue.PANEL_WIDTH;
}
break;
case U:
y --;
if(y < 0) {
y = ConstValue.PANEL_HEIGHT;
}
break;
case R:
x ++;
if(x >= ConstValue.PANEL_WIDTH) {
x = 0;
}
break;
case D:
y ++;
if(y >= ConstValue.PANEL_HEIGHT) {
y = 0;
}
break;
}
Point snakeHead = new Point(x,y);
//增加蛇头
body.addFirst(snakeHead);
}
/**
* 判断2个方向是否相反
* @param oldDire
* @param newDire
* @return
*/
private boolean checkInvalidDirection(Direction oldDire,Direction newDire) {
boolean result = false;
switch (oldDire) {
case L:
if (newDire == Direction.R) {
result = true;
}
break;
case U:
if (newDire == Direction.D) {
result = true;
}
break;
case R:
if (newDire == Direction.L) {
result = true;
}
break;
case D:
if (newDire == Direction.U) {
result = true;
}
break;
}
return result;
}
/**
* 改变方向
*/
public void changeDirection(Direction dire) {
if(!this.checkInvalidDirection(this.newDirection, dire)) {
this.newDirection = dire;
}
}
/**
* 吃食物(身体变长)
*/
public void eatFood() {
body.addLast(oldTail);
this.eatFoodCount ++;
}
/**
* 蛇是否迟到了自己的身体
*/
public boolean isEatBody() {
for(int i = 1; i < body.size(); i ++) {
if(body.get(i).equals(this.getHead())) {
return true;
}
}
return false;
}
/**
* 显示
* @param g
*/
public void drawMe(Graphics g) {
Color c = g.getColor();
for(Point p : body) {
if(p.equals(getHead())) {
g.setColor(Color.GREEN);
} else {
g.setColor(Color.LIGHT_GRAY);
}
g.fillRoundRect(p.x * ConstValue.CELL_WIDTH,
p.y * ConstValue.CELL_HEIGHT,
ConstValue.CELL_WIDTH,
ConstValue.CELL_HEIGHT,
ConstValue.CELL_RECT_SIZE,
ConstValue.CELL_RECT_SIZE);
}
g.setColor(c);
}
/**
* 返回蛇头坐标
* @return
*/
public Point getHead() {
return body.getFirst();
}
/**
* 检查点p是否为蛇体的一部分
* @param p
* @return
*/
public boolean isInSnake(Point p) {
for(Point s : body) {
if(s.equals(p)) {
return true;
}
}
return false;
}
/**
* 蛇移动线程启动
*/
public void start() {
new Thread(new SnakeDriver()).start();
}
/**
* 注册监听器
* @param l
*/
public void addSnakeListener(SnakeListener l) {
if(null != l) {
this.listeners.add(l);
}
}
/**
* 蛇移动线程
* @author HP
*
*/
private class SnakeDriver implements Runnable {
@Override
public void run() {
while(alive) {
move();
for(SnakeListener l : listeners) {
l.snakeMove(Snake.this);
}
try {
Thread.sleep(ConstValue.SNAKE_SPEED);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void die() {
this.alive = false;
}
public boolean isAlive() {
return alive;
}
public int getEatFoodCount() {
return eatFoodCount;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -