- import java.util.Arrays;
- import java.util.LinkedList;
- import java.util.Random;
- import javax.swing.JOptionPane;
- public class SnakeModel implements Runnable
- {
- Snake gs;
- boolean[][] matrix;//定义矩阵
- LinkedList nodeArray = new LinkedList();//list类的方法,用于动态添加数组元素,没有同步方法
- Node food;//食物
- int maxX;
- int maxY;
- int direction = 2;//起始方向为向上
- boolean running = false;
- int timeInterval = 300;//时间间隔
- double speedChangeRate = 0.5;
- boolean paused = false;
- int score = 0;
- int countMove = 0;
- 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(Snake gs, int maxX, int maxY)//构造方法
- {
- this.gs = gs;
- this.maxX = maxX;
- this.maxY = maxY;
- // 初始化矩阵
- matrix = new boolean[maxX][];
- for (int i = 0; i < maxX; ++i)
- {
- matrix[i] = new boolean[maxY];
- Arrays.fill(matrix[i], false);//LinkedList的方法fill()用于填充
- }
- // 初始化蛇
- int initArrayLength = maxX > 20 ? 10 : maxX / 2;//初始蛇的长度
- for (int i = 0; i < initArrayLength; ++i)
- {
- int x = maxX / 2 + i;
- int y = maxY / 2;//初始蛇的位置
- nodeArray.addLast(new Node(x, y));//LinkedList的优势在于在中间位置插入和删除操作,速度是最快的
- matrix[x][y] = true;
- }
- food = createFood();
- matrix[food.x][food.y] = true;
- }
- public void changeDirection(int newDirection)
- {
- if (direction % 2 != newDirection % 2)
- {
- direction = newDirection;
- }
- }
- public boolean moveOn()
- {
- Node n = (Node)nodeArray.getFirst();
- 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))
- { //蛇不能碰到外壁
- 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;
- }
- else
- return false;
- }
- 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);//优先级高的线程可用run()方法中的sleep方法休眠一段时间
- } //线程在休眠时被打断,会抛出异常,必须使用try-catch语句
- catch (Exception e)
- {
- break;
- }
- if (!paused)
- {
- if (moveOn())
- {
- gs.repaint();
- }
- else
- {
- int n = JOptionPane.showConfirmDialog(
- null,
- "不好意思啊,你输了,点确定再来一局吧!",
- "游戏结束",
- JOptionPane.YES_NO_OPTION);
- if (n == JOptionPane.YES_OPTION)
- {
- running = false;
- gs.begin();
- }
- break;
- }
- }
- }
- running = false;
- }
- private Node createFood()
- {
- int x = 0;
- int y = 0;
- do
- {
- Random r = new Random();
- 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;
- }
- }