📄 snakemodel.java
字号:
import java.util.*;
public class SnakeModel {
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;
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;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -