📄 gamecanvas.java
字号:
this.loc[1]=y-1;
break;
case 13: moveArea[0]=x;
moveArea[1]=y;
set2(2);
break;
case 14: moveArea[0]=x-1;
moveArea[1]=y;
set2(2);
this.loc[0]=x-1;
this.loc[1]=y;
break;
case 0:
case 15: moveArea[0]=x;
moveArea[1]=y;
set2(4);
break;
default: break;
} repaint();
}
private boolean isMoved() //判断是否能够移动
{
switch(orient){
case up:
case down:
if(selectArea[2]==2){
if(draw.current[moveArea[1]][moveArea[0]]!=0||draw.current[moveArea[1]][moveArea[0]+1]!=0)
return false;
}
else {
if(draw.current[moveArea[1]][moveArea[0]]!=0)
return false;
}
if(moveArea[0]!=selectArea[0])
return false;
break;
case left:
case right:
if(selectArea[3]==2)
{
if(draw.current[moveArea[1]][moveArea[0]]!=0||draw.current[moveArea[1]+1][moveArea[0]]!=0)
return false;
}
else {
if(draw.current[moveArea[1]][moveArea[0]]!=0)
return false;
}
if(moveArea[1]!=selectArea[1])
return false;
break;
}
if(moveArea[1]-selectArea[1]>selectArea[3]||moveArea[1]-selectArea[1]<-selectArea[3])
return false;
if(moveArea[0]-selectArea[0]>selectArea[2]||moveArea[0]-selectArea[0]<-selectArea[2])
return false;
return true;
}
protected void keyPressed(int keyCode) //按键设置
{
switch(getGameAction(keyCode)){
case Canvas.UP:
if(!selected){
if(loc[1]-1>=0){
loc[1]--;
selectMethod(loc[0],loc[1]);
}
}
else {
if(loc[1]-1>=0){
loc[1]--;
moveMethod(loc[0],loc[1]);
orient=1;
}
} break;
case Canvas.DOWN:
if(!selected){
if(loc[1]+selectArea[3]<HEIGHT){
if(selectArea[3]==2)
loc[1]+=2;
else loc[1]++;
selectMethod(loc[0],loc[1]);
}
}
else {
if(loc[1]+1<HEIGHT){
if(moveArea[3]==2)
loc[1]+=2;
else loc[1]++;
moveMethod(loc[0],loc[1]);
orient=2;
}
} break;
case Canvas.LEFT:
if(!selected){
if(loc[0]-1>=0){
loc[0]--;
selectMethod(loc[0],loc[1]);
}
}
else {
if(loc[0]-1>=0){
loc[0]--;
moveMethod(loc[0],loc[1]);
orient=3;
}
} break;
case Canvas.RIGHT:
if(!selected){
if(loc[0]+selectArea[2]<WIDTH){
if(selectArea[2]==2)
loc[0]+=2;
else loc[0]++;
selectMethod(loc[0],loc[1]);
}
}
else {
if(loc[0]+1<WIDTH){
if(moveArea[2]==2)
loc[0]+=2;
else loc[0]++;
moveMethod(loc[0],loc[1]);
orient=4;
}
} break;
case Canvas.FIRE:
if(selected){
if(isMoved())
{
Move();
selected=false;
orient=0;
if(win())
repaint();
selectMethod(loc[0],loc[1]);
} else{ selected=false;
orient=0;
selectMethod(loc[0],loc[1]);
}
}
else{
if(draw.current[loc[1]][loc[0]]==0){}
else
selected=true;
moveMethod(loc[0],loc[1]);
} break;
}
}
private void Move() //控制逻辑,移动方法?
{
switch(orient){
case up:
if(selectArea[2]==2&&selectArea[3]==2)
{
draw.current[moveArea[1]][moveArea[0]]=draw.current[selectArea[1]][selectArea[0]];
draw.current[moveArea[1]][moveArea[0]+1]=draw.current[selectArea[1]][selectArea[0]+1];
draw.current[selectArea[1]][selectArea[0]]=draw.current[selectArea[1]+1][selectArea[0]];
draw.current[selectArea[1]][selectArea[0]+1]=draw.current[selectArea[1]+1][selectArea[0]+1];
draw.current[selectArea[1]+1][selectArea[0]]=draw.current[selectArea[1]+1][selectArea[0]+1]=0;
}
else if(selectArea[2]==2&&selectArea[3]==1)
{
draw.current[moveArea[1]][moveArea[0]]=draw.current[selectArea[1]][selectArea[0]];
draw.current[moveArea[1]][moveArea[0]+1]=draw.current[selectArea[1]][selectArea[0]+1];
draw.current[selectArea[1]][selectArea[0]]=draw.current[selectArea[1]][selectArea[0]+1]=0;
}
else if(selectArea[2]==1&&selectArea[3]==2)
{
if(moveArea[3]==2)
draw.current[moveArea[1]+1][moveArea[0]]=draw.current[selectArea[1]][selectArea[0]];
else
draw.current[moveArea[1]][moveArea[0]]=draw.current[selectArea[1]][selectArea[0]];
draw.current[selectArea[1]][selectArea[0]]=draw.current[selectArea[1]+1][selectArea[0]];
draw.current[selectArea[1]+1][selectArea[0]]=0;
}
else
{
draw.current[moveArea[1]][moveArea[0]]=draw.current[selectArea[1]][selectArea[0]];
draw.current[selectArea[1]][selectArea[0]]=0;
} steps++;
break;
case down:
if(selectArea[2]==2&&selectArea[3]==2)
{
draw.current[moveArea[1]][moveArea[0]]=draw.current[selectArea[1]+1][selectArea[0]];
draw.current[moveArea[1]][moveArea[0]+1]=draw.current[selectArea[1]+1][selectArea[0]+1];
draw.current[selectArea[1]+1][selectArea[0]]=draw.current[selectArea[1]][selectArea[0]];
draw.current[selectArea[1]+1][selectArea[0]+1]=draw.current[selectArea[1]][selectArea[0]+1];
draw.current[selectArea[1]][selectArea[0]]=draw.current[selectArea[1]][selectArea[0]+1]=0;
}
else if(selectArea[2]==2&&selectArea[3]==1)
{
draw.current[moveArea[1]][moveArea[0]]=draw.current[selectArea[1]][selectArea[0]];
draw.current[moveArea[1]][moveArea[0]+1]=draw.current[selectArea[1]][selectArea[0]+1];
draw.current[selectArea[1]][selectArea[0]]=draw.current[selectArea[1]][selectArea[0]+1]=0;
}
else if(selectArea[2]==1&&selectArea[3]==2)
{
draw.current[moveArea[1]][moveArea[0]]=draw.current[selectArea[1]+1][selectArea[0]];
draw.current[selectArea[1]+1][selectArea[0]]=draw.current[selectArea[1]][selectArea[0]];
draw.current[selectArea[1]][selectArea[0]]=0;
}
else
{
draw.current[moveArea[1]][moveArea[0]]=draw.current[selectArea[1]][selectArea[0]];
draw.current[selectArea[1]][selectArea[0]]=0;
} steps++;
break;
case left:
if(selectArea[2]==2&&selectArea[3]==2)
{
draw.current[moveArea[1]][moveArea[0]]=draw.current[selectArea[1]][selectArea[0]];
draw.current[moveArea[1]+1][moveArea[0]]=draw.current[selectArea[1]+1][selectArea[0]];
draw.current[selectArea[1]][selectArea[0]]=draw.current[selectArea[1]][selectArea[0]+1];
draw.current[selectArea[1]+1][selectArea[0]]=draw.current[selectArea[1]+1][selectArea[0]+1];
draw.current[selectArea[1]][selectArea[0]+1]=draw.current[selectArea[1]+1][selectArea[0]+1]=0;
}
else if(selectArea[2]==2&&selectArea[3]==1)
{
if(moveArea[2]==2)
draw.current[moveArea[1]][moveArea[0]+1]=draw.current[selectArea[1]][selectArea[0]];
else
draw.current[moveArea[1]][moveArea[0]]=draw.current[selectArea[1]][selectArea[0]];
draw.current[selectArea[1]][selectArea[0]]=draw.current[selectArea[1]][selectArea[0]+1];
draw.current[selectArea[1]][selectArea[0]+1]=0;
}
else if(selectArea[2]==1&&selectArea[3]==2)
{
draw.current[moveArea[1]][moveArea[0]]=draw.current[selectArea[1]][selectArea[0]];
draw.current[moveArea[1]+1][moveArea[0]]=draw.current[selectArea[1]+1][selectArea[0]];
draw.current[selectArea[1]][selectArea[0]]=draw.current[selectArea[1]+1][selectArea[0]]=0;
}
else
{
draw.current[moveArea[1]][moveArea[0]]=draw.current[selectArea[1]][selectArea[0]];
draw.current[selectArea[1]][selectArea[0]]=0;
} steps++;
break;
case right:
if(selectArea[2]==2&&selectArea[3]==2)
{
draw.current[moveArea[1]][moveArea[0]]=draw.current[selectArea[1]][selectArea[0]+1];
draw.current[moveArea[1]+1][moveArea[0]]=draw.current[selectArea[1]+1][selectArea[0]+1];
draw.current[selectArea[1]][selectArea[0]+1]=draw.current[selectArea[1]][selectArea[0]];
draw.current[selectArea[1]+1][selectArea[0]+1]=draw.current[selectArea[1]+1][selectArea[0]];
draw.current[selectArea[1]][selectArea[0]]=draw.current[selectArea[1]+1][selectArea[0]]=0;
}
else if(selectArea[2]==2&&selectArea[3]==1)
{
draw.current[moveArea[1]][moveArea[0]]=draw.current[selectArea[1]][selectArea[0]+1];
draw.current[selectArea[1]][selectArea[0]+1]=draw.current[selectArea[1]][selectArea[0]];
draw.current[selectArea[1]][selectArea[0]]=0;
}
else if(selectArea[2]==1&&selectArea[3]==2)
{
draw.current[moveArea[1]][moveArea[0]]=draw.current[selectArea[1]][selectArea[0]];
draw.current[moveArea[1]+1][moveArea[0]]=draw.current[selectArea[1]+1][selectArea[0]];
draw.current[selectArea[1]][selectArea[0]]=draw.current[selectArea[1]+1][selectArea[0]]=0;
}
else
{
draw.current[moveArea[1]][moveArea[0]]=draw.current[selectArea[1]][selectArea[0]];
draw.current[selectArea[1]][selectArea[0]]=0;
}
steps++;
break;
default: break;
}
}
private boolean win() //判断是否成功
{
if(draw.current[4][1]==3&&draw.current[4][2]==4)
return true;
else return false;
}
}//class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -