📄 snake.java
字号:
package com.nilaiya.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.Set;
import com.nilaiya.snake.listener.SnakeListener;
import com.nilaiya.snake.util.Global;
/**
* @author yellow
*
*/
public class Snake {
//判断蛇是否死掉
boolean lift;
//蛇的最后一个节点
private Point oldTail;
//表示方向的常量
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>();
/**
* 构造器
*
*/
public Snake() {
init();
}
/**
* 蛇死掉的方法
*
*/
public void die() {
lift = false;
System.out.println("蛇死掉了!");
}
/**
* 得到蛇头的坐标
* @return Point
* @return the body.getFirst()
*/
public Point getHead() {
return body.getFirst();
}
/**
* 初始化蛇
*
*/
public void init() {
int x = Global.WIDTH / 2;
int y = Global.HEIGHT / 2;
for (int i = 0; i < 3; i++) {
body.addLast(new Point(x--, y));//蛇头在右边(最中间)
}
//蛇头默认移动方向向右
oldDirection = newDirection = RIGHT;
lift = true;//蛇一开始是活的
}
/**
* 添加监听器的方法
* @param l
*/
public void addSnakeListener(SnakeListener l) {
if (l != null) {
this.listeners.add(l);
}
}
/**
* 蛇的移动方法
*
*/
public void move() {
System.out.println("蛇移动了!");
if (!(oldDirection + newDirection == 0)) {
oldDirection = newDirection;
}
//1.去掉尾部
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);
//2.加头
body.addFirst(newHead);
}
/**
* 蛇改变方向的方法
* @param direction
*/
public void changeDirection(int direction) {
System.out.println("蛇改变方向了!");
newDirection = direction;
}
/**
* 蛇吃食物的方法
*
*/
public void eatFood() {
System.out.println("蛇吃食物了!");
body.addLast(oldTail);
}
/**
* 判断蛇是否吃到了自己的身体
* @return boolean
*/
public boolean isEatBody() {
System.out.println("蛇吃到自己的身体了!");
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) {
System.out.println("显示蛇的方法");
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);
}
}
/**
* 内部类
* 蛇一开始要不停的移动
* @author yellow
*
*/
private class SnakeMove implements Runnable {
public void run() {
while (lift) {
move();
//循环所有的遍历
for (SnakeListener l : listeners) {
l.snakeMoved(Snake.this);
}
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("线程延迟出错!");
}
}
}
}
/**
* 启动线程的方法
*
*/
public void start() {
new Thread(new SnakeMove()).start();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -