?? snakemodel.java.bak
字號:
import java.util.*;
public class SnakeModel {
public snakeUse getSnakeHead(){
return(snakeUse)(snake.getLast());
}
public snakeUse getSnakeTail(){
return (snakeUse)(snake.getFirst());
}
public snakeUse getRuningDiriction(){
return runingDiriction;
}
public LinkedList getSnake(){
return snake;
}
public LinkedList getOthers(){
return others;
}
public int getScore(){
return gameScore;
}
public boolean getReadyAddScore(){
return readyAddScore;
}
private void setSnakeHead(snakeUse snakeHead){
this.snakeHead=snakeHead;
}
public snakeUse getTempBlock(){
return tempBlock;
}
private void setTempBlock(){
tempBlock=(snakeUse)(others.get((int)(Math.random()*others.size())));
}
private void moveTo(Object a,LinkedList from,LinkedList to){
from.remove(a);
to.add(a);
}
public void initializeV(){
others.clear();
snake.clear();
gameScore=0;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
others.add(jps[i][j]);
}
}
//初始化蛇的形狀([5][5]、[5][6]、[5][7])
for(int i=5;i<8;i++){
moveTo(jps[5][i],others,snake);
}
snakeHead=new snakeUse(5,7);
//設置隨機塊
tempBlock=new snakeUse(0,0);
setTempBlock();
//初始化運動方向
runingDiriction=new snakeUse(1,0);
}
public SnakeModel(int row1,int col1){
row=row1;
col=col1;
jps=new snakeUse[row][col];
snake=new LinkedList();
others=new LinkedList();
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
jps[i][j]=new snakeUse(i,j);
}
}
initializeV();
}
//如果move成功返回true,否則(結束),返回false
//參數direction是snakeUse類型,
//direction 的值:(0,1)表示向右(0,-1)表示向左
//(1,0)表示向下(-1,0)表示向上
public boolean move(snakeUse direction){
//判斷給的方向是不是跟運行方向相反
if (direction.reverse(runingDiriction)){
snakeHead.setX(snakeHead.getX()+runingDiriction.getX());
snakeHead.setY(snakeHead.getY()+runingDiriction.getY());
}else{
snakeHead.setX(snakeHead.getX()+direction.getX());
snakeHead.setY(snakeHead.getY()+direction.getY());
}
//如果蛇吃了隨機塊
try{
if ((snakeHead.getX()==tempBlock.getX())
&&(snakeHead.getY()==tempBlock.getY()))
{
moveTo(jps[snakeHead.getX()][snakeHead.getY()],others,snake);
setTempBlock();
gameScore+=1;
readyAddScore=true;
}else{
readyAddScore=false;
//是否出界
if((snakeHead.getX()<row)&&(snakeHead.getY()<col)){
//如果不出界,判斷是否與自身相交
if(snake.contains(jps[snakeHead.getX()][snakeHead.getY()])){
//如果相交,游戲結束
return false;
}else{
//如果不相交,就把snakeHead加到snake里面,并且把尾巴移出
moveTo(jps[snakeHead.getX()][snakeHead.getY()],others,snake);
moveTo(snake.getFirst(),snake,others);
}
}else{
//出界!游戲結束
return false;
}
}
}catch(ArrayIndexOutOfBoundsException e){
return false;
}
//更新運行方向
if (!(direction.reverse(runingDiriction)
||direction.equals(runingDiriction))){
runingDiriction.setX(direction.getX());
runingDiriction.setY(direction.getY());
}
return true;
}
private int row,col;
private snakeUse snakeHead;
private snakeUse[][] jps;
private LinkedList snake;
private LinkedList others;
private snakeUse tempBlock;
private snakeUse runingDiriction;
private int gameScore=0;
private boolean readyAddScore=false;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -