*作者:C.Jason
*要點分析:
*1)數據結構:matrix[][]用來存儲地圖上面的信息,如果什么也沒有設置為false,
* 如果有食物或蛇,設置為true;nodeArray,一個LinkedList,用來保存蛇的每
* 一節;food用來保存食物的位置;而Node類是保存每個位置的信息。
*2)重要函數:
* changeDirection(int newDirection) ,用來改變蛇前進的方向,而且只是
* 保存頭部的前進方向,因為其他的前進方向已經用位置來指明了。 其中newDirection
* 必須和原來的direction不是相反方向,所以相反方向的值用了同樣的奇偶性。在測試
* 的時候使用了direction%2!=newDirection%2 進行判斷。
* moveOn(),用來更新蛇的位置,對于當前方向,把頭部位置進行相應改變。如果越界,
* 結束;否則,檢測是否遇到食物(加頭部)或身體(結束);如果什么都沒有,加上頭部,
* 去掉尾部。由于用了LinkedList數據結構,省去了相當多的麻煩。
*************************************************************************/
import java.util.*;
import javax.swing.*;
//----------------------------------------------------------------------
//Node:結點類
//----------------------------------------------------------------------
class Node
{
int x;
int y;
Node(int x,int y)
{
this.x=x;
this.y=y;
}
}
//----------------------------------------------------------------------
//SnakeModel:貪吃蛇模型
//----------------------------------------------------------------------
class SnakeModel implements Runnable
{
GreedSnake gs;
boolean[][] matrix;// 界面數據保存在數組里
LinkedList nodeArray=new LinkedList();
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和DOWN是偶數,RIGHT和LEFT是奇數
public static final int UP=2;
public static final int DOWN=4;
public static final int LEFT=1;
public static final int RIGHT=3;
//----------------------------------------------------------------------
//GreedModel():初始化界面
//----------------------------------------------------------------------
public SnakeModel(GreedSnake 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);// 沒有蛇和食物的地區置false
}
//初始化貪吃蛇
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));
matrix[x][y]=true;// 蛇身處置true
}
food=createFood();
matrix[food.x][food.y]=true;// 食物處置true
}
//----------------------------------------------------------------------
//changeDirection():改變運動方向
//----------------------------------------------------------------------
public void changeDirection(int newDirection)
{
if(direction%2!=newDirection%2)// 避免沖突
{
direction=newDirection;
}
}
//----------------------------------------------------------------------
//moveOn():貪吃蛇運動函數
//----------------------------------------------------------------------
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;//越界(撞到墻壁)
}
//----------------------------------------------------------------------
//run():貪吃蛇運動線程
//----------------------------------------------------------------------
public void run()
{
running=true;
while(running)
{
try
{
Thread.sleep(timeInterval);
}catch(Exception e)
{
break;
}
if(!paused)
{
if(moveOn())// 未結束
{
gs.repaint();
}
else//游戲結束
{
JOptionPane.showMessageDialog(null,"GAME OVER",
"Game Over",JOptionPane.INFORMATION_MESSAGE);
break;
}
}
}
running=false;
}
//----------------------------------------------------------------------
//createFood():生成食物及放置地點
//----------------------------------------------------------------------
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);
}
//----------------------------------------------------------------------
//speedUp():加快蛇運動速度
//----------------------------------------------------------------------
public void speedUp()
{
timeInterval*=speedChangeRate;
}
//----------------------------------------------------------------------
//speedDown():放慢蛇運動速度
//----------------------------------------------------------------------
public void speedDown()
{
timeInterval/=speedChangeRate;
}
//----------------------------------------------------------------------
//changePauseState(): 改變游戲狀態(暫;蚶^續)
//----------------------------------------------------------------------
public void changePauseState()
{
paused=!paused;
}
}
延伸閱讀
文章來源于領測軟件測試網 http://www.kjueaiud.com/