import java.util.*;
import javax.swing.*;
class SnakeModel implements Runnable{
GreedSnake gs;
// 用来显示游戏画面中的蛇与食物,有的地方为true
boolean[][] matrix;
// List 接口的链接列表实现。实现所有可选的列表操作,并且允许所有元素(包括 null)。除了实现 List 接口外,LinkedList 类还为在列表的开头及结尾 get、remove 和 insert 元素提供了统一的命名方法。这些操作允许将链接列表用作堆栈、队列或双端队列 (deque)。
LinkedList nodeArray = new LinkedList();
// Node类用来指示在坐标轴上x,y坐标的类
Node food;
// 移动范围,超出此范围则游戏失败
int maxX;
int maxY;
// 设定贪吃蛇初始移动方向
int direction = 2;
boolean running = false;
int timeInterval = 200;
double speedChangeRate = 0.75;
boolean paused = false;
int score = 0;
int countMove = 0;
// UP and DOWN should be even
// RIGHT and LEFT should be odd
public static final int UP = 2;
public static final int DOWN = 4;
public static final int LEFT = 1;
public static final int RIGHT = 3;
// 构造函数
public SnakeModel(GreedSnake gs, int maxX, int maxY){
this.gs = gs;
this.maxX = maxX;
this.maxY = maxY;
// initial matirx
matrix = new boolean[maxX][];
for(int i=0; i<maxX; ++i){
matrix[i] = new boolean[maxY];
// 将指定的 boolean 值分配给指定 boolean 型数组的每个元素。
Arrays.fill(matrix[i],false);
}
// initial the snake
// 指定初始时的蛇的形态
int initArrayLength = maxX > 20 ? 10 : maxX/2;
for(int i = 0; i < initArrayLength; ++i){
// 初始时蛇的头在位置(maxX/2,maxY/2),蛇身为水平
int x = maxX/2+i;
int y = maxY/2;
// 将给定元素追加到此列表的结尾。(与 add 方法功能相同;包括它只是出于一致性考虑。)
nodeArray.addLast(new Node(x, y));
matrix[x][y] = true;
}
food = createFood();
// 标记并显示食物
matrix[food.x][food.y] = true;
}
public void changeDirection(int newDirection){
// 注意,如果前一步移动的是左右键,则下一次改变方向时只能使用上下键,上一次移动的是上下键,则下一次改变方向时只能使用左右键,因为蛇不能回头,
if (direction % 2 != newDirection % 2){
direction = newDirection;
}
}
// 如果返回false,则游戏失败
public boolean moveOn(){
// 返回此列表的第一个元素
Node n = (Node)nodeArray.getFirst();
// 这里的x,y指的是蛇头部的坐标
int x = n.x;
int y = n.y;
switch(direction){
case UP:
y--;
break;
case DOWN:
y++;
break;
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
}
if ((0 <= x && x < maxX) && (0 <= y && y < maxY)){
// 如果碰到了matrix为真的点
if (matrix[x][y]){
if(x == food.x && y == food.y){
nodeArray.addFirst(food);
// 计分
int scoreGet = (10000 - 200 * countMove) / timeInterval;
score += scoreGet > 0? scoreGet : 10;
countMove = 0;
// 出现新食物
food = createFood();
matrix[food.x][food.y] = true;
return true;
}
// 如果碰到了matrix为真的点,但又不是食物的点,这就意味着碰到了自己,则游戏失败
else
return false;
}
// 如果没有碰到matrix为真的点
else{
nodeArray.addFirst(new Node(x,y));
matrix[x][y] = true;
// 移除并返回此列表的最后一个元素。
n = (Node)nodeArray.removeLast();
matrix[n.x][n.y] = false;
countMove++;
return true;
}
}
return false;
}
public void run(){
running = true;
while (running){
try{
// 在指定的毫秒数内让当前正在执行的线程休眠(暂停执行)。该线程不丢失任何监视器的所属权。
// 加速和减速效果主要由这一语句来体现
Thread.sleep(timeInterval);
}
catch(Exception e){
break;
}
// 如果不是游戏暂停暂停
if(!paused){
if (moveOn()){
// 且游戏没有失败,则重绘刷新游戏画面
gs.repaint();
}
// 如果游戏失败,弹出对话框
else{
// JOptionPane 有助于方便地弹出要求用户提供值或向其发出通知的标准对话框。
//
// 调出对话框,它显示使用由 messageType 参数确定的默认图标的 message。
// 参数:
// parentComponent - 确定在其中显示对话框的 Frame;如果为 null 或者 parentComponent 不具有 Frame,则使用默认的 Frame
// message - 要显示的 Object
// title - 对话框的标题字符串
// messageType - 要显示的消息类型:ERROR_MESSAGE、INFORMATION_MESSAGE、WARNING_MESSAGE、QUESTION_MESSAGE 或 PLAIN_MESSAGE
JOptionPane.showMessageDialog(
null,
"you failed",
"Game Over",
JOptionPane.INFORMATION_MESSAGE);
// 跳出while循环,否则会一直弹出you fail对话框
break;
}
}
}
running = false;
}
private Node createFood(){
int x = 0;
int y = 0;
do{
// 此类的实例用于生成伪随机数流。
Random r = new Random();
// 返回一个伪随机数,它是从此随机数生成器的序列中取出的、在 0(包括)和指定值(不包括)之间均匀分布的 int值。nextInt 的常规协定是伪随机地生成并返回指定范围中的一个 int 值。所有 n 个可能 int 值的生成概率(大致)相同。
x = r.nextInt(maxX);
y = r.nextInt(maxY);
// 只能在当前为空的空格生成食物,即目前有蛇的身体和食物的地方不能生成食物
}while(matrix[x][y]);
return new Node(x,y);
}
public void speedUp(){
timeInterval *= speedChangeRate;
}
public void speedDown(){
timeInterval /= speedChangeRate;
}
public void changePauseState(){
paused = !paused;
}
public String toString(){
String result = "";
for(int i=0; i<nodeArray.size(); ++i){
Node n = (Node)nodeArray.get(i);
result += "[" + n.x + "," + n.y + "]";
}
return result;
}
}
class Node{
int x;
int y;
Node(int x, int y){
this.x = x;
this.y = y;
}
}
评论0
最新资源