那假设你的坦克类是这样的class Tank{
public static final int UP=1;
public static final int DOWN=3;
public static final int LEFT=0;
public static final int RIGHT=2;
boolean stop[]=new boolean[4];//这四个控制允许坦克移动的四个方向
int x=0,y=0;
}
接下来是移动public boolean move(int dire,int [][]map){//参数是移动的方向与地图数据
if(stop[dire])//如果这个方向是禁止移动的直接返回
return fasle;
int off_x=dire%2!=0?0:dire-1;
int off_y=dire%2==0?0:dire-2;
this.x+=off_x;
this.y+=off_y;
if(this.x%60==0||this.y%60==0){//移动之后的坦克位与方块的边界
int x=this.x/60;
int y=this.y/60;
if(x-1>=0&&map[x-1][y]!=1){//对坦克左边的方块进行判断
stop[LEFT]=true//设置左方向可移动
}else{
stop[LEFT]=false;//设置左方向不可移动
}
.......依次类推
}
return true;
}