用NetBeans开发J2ME游戏实例讲解

 

(6)增加菜单和启动画面: 菜单和启动画面是用一系列的画面组成,用Command等命令来连接

public class HuaRongDaoMidlet extends MIDlet implements
CommandListener{

    private Display display;

    private SplashScreen splash;

    private ControlLogic logic;

   

    /** The Form object for the Options command */

    private Form optionsForm;

 

    /** Set of choices for the levels */

    private ChoiceGroup levelChoice;

    private final static Command CMD_EXIT = new Command("退出", Command.EXIT,
1);

    private final static Command CMD_OK = new Command("确认", Command.OK,
1);

    private final static Command CMD_OPTION = new Command("选项",
Command.SCREEN, 1);

    private final static Command CMD_START = new Command("开始",
Command.SCREEN, 1);

    private final static Command CMD_PAUSE = new Command("暂停",
Command.SCREEN, 1);

    public HuaRongDaoMidlet(){

        display = Display.getDisplay(this);

    }

     public void startApp() {

        splash=new SplashScreen();

         splash.addCommand(CMD_START);

        splash.addCommand(CMD_EXIT);

        splash.addCommand(CMD_OPTION);

         splash.setCommandListener(this);

        display.setCurrent(splash);

        genOptions();  //产生选项     

    }

    public void pauseApp() {

    }

      public void destroyApp(boolean unconditional) {

    }

    public void commandAction(Command c, Displayable d) {

        if ( c == CMD_EXIT && d == splash ){

            //退出

            destroyApp(false);

            notifyDestroyed();

        }else if (c == CMD_EXIT && d == logic){

            //从游戏中返回

            logic = null;

                display.setCurrent(splash);

        }else if (c == CMD_OPTION){

            //进入选项

            display.setCurrent(optionsForm);

        }else if (c == CMD_OK && d == optionsForm) {

            // 从选项回到主界面

                display.setCurrent(splash);

            }else if (c == CMD_START && d == splash) {

            // 开始新游戏

            logic=new ControlLogic(levelChoice.getSelectedIndex()+1);

            display.setCurrent(logic);

            logic.addCommand(CMD_PAUSE);

            logic.addCommand(CMD_EXIT);

            logic.setCommandListener(this);

            }else if (c == CMD_PAUSE) {//处理“暂停”

            logic.addCommand(CMD_PAUSE);

            logic.addCommand(CMD_EXIT);

            logic.pause();

        }

    }

   

    private Screen genOptions() {

            if (optionsForm == null) {

                optionsForm = new Form("选项");

                optionsForm.addCommand(CMD_OK);

                optionsForm.setCommandListener(this);

 

                levelChoice = new ChoiceGroup("Speed", Choice.EXCLUSIVE);

                levelChoice.append("过五关", null);

                levelChoice.append("横刀立马", null);

                levelChoice.append("水泄不通", null);

                levelChoice.append("小燕出巢", null);

                levelChoice.append("近在咫尺", null);

                levelChoice.append("走投无路", null);

                optionsForm.append(levelChoice);

              optionsForm.append("小毛驴工作室,2005, 版权没有,违者打屁股");

            }

            return optionsForm;

    }

    }

 

(7)制造暂停:我这里的实现是设一个控制逻辑中的全局变量,判断是否是暂停状态,如果是,在keyPressed()和paint()事件里判断,直接跳出,也就是不接受任何除了暂停键的其它输入.

4.改进程序

 (1)记录历史步骤,以便可以悔棋:

记录历史步骤的方法是实现一个History类,这个类实际上是一个Vector的封装,用来保存每一步的走法,走法被定义为一个包含5个元素的数组,分别是X,Y,width,height,direction.
这里需要注意的是,Java当中实际上是没有局部变量的,每一个局部变量都需要new出来,所以在使用Vector的addElement()函数时,由于它是传引用,我们必须要新创建一个element,而不能使用全局的,因为如果使用全局的,下一次addElement时,会因为该变了变量的值使得刚才加到Vector中的值也改变了。

import java.util.Vector;
 
/**

 *

 * @author lin

 */

public class History {

    private static Vector steps = new Vector();   

    /** Creates a new instance of History */

    public History() {

        clear();

    }

    public static void addStep(Object step){

        steps.addElement(step);

    }

    public static void removeLastStep(){

        steps.removeElement(steps.lastElement());

    }

    public static Object getLastStep(){

        return steps.lastElement();

    }

    public static Object getStepAt(int index){

        return steps.elementAt(index);

    }

    public static int getSize(){

        return steps.size();

    }

    private void clear(){

        if (!steps.isEmpty())

            steps.removeAllElements();

    }

}

在每一步移动结束后,记录这一步的信息:

ContorlLogic.java: Move()

……

    moves++;// 增加移动的步骤

    byte[] step = new byte[5];
//五个参数分别为,前四个和SelectArea一样,最后一个表示上1,下2,左3,右4。

    //将此次移动记录到历史记录当中;

    step[0]= this.SelectArea[0];

    step[1]= this.SelectArea[1];

    step[2]= this.SelectArea[2];

    step[3]= this.SelectArea[3];

    step[4]= this.getMoveDirection();

    history.addStep(step);

……

增加一个悔棋的按钮,增加一个unMove()函数:

    public void unMove(){

        if ( moves == 0 )

            return;

        byte[] step = new byte[5];
//五个参数分别为,前四个和SelectArea一样,最后一个表示上1,下2,左3,右4。

        step = (byte []) history.getLastStep();//取得上一步移动

        history.removeLastStep();//减少一步;

        moves–;

        for (int i= 0; i< 4;i++){

            this.MoveArea[i] = step[i];//重设MoveArea  

            this.SelectArea[i] = step[i];//重设SelectArea

        }

        if (step[4] == 1){

            this.SelectArea[1] = (byte) (step[1]-1);

            this.loc[1]++;

        }

        else if (step[4] == 2){

            this.SelectArea[1] = (byte) (step[1]+1);

            this.loc[1]–;

        }

        else if (step[4] == 3){

            this.SelectArea[0] = (byte) (step[0]-1);

            this.loc[0]++;

        }

        else if (step[4] == 4){

            this.SelectArea[0] = (byte) (step[0]+1);

            this.loc[0]–;

        }

        //移动回来.

        byte[][] temp = new byte[this.SelectArea[3]][this.SelectArea[2]];

        //复制要移动的区域,因为这块区域可能会被覆盖掉

        for (int i = 0; i < this.SelectArea[2]; i++) {

            for (int j = 0; j < this.SelectArea[3]; j++) {

                temp[j][i] = this.MyMap.Grid[this.SelectArea[1]
+j][this.SelectArea[0] + i];

            }

        }

        //将要移动的区域移动到刚选中的区域(即要移动到的区域)

        for (int i = 0; i < this.SelectArea[2]; i++) {

            for (int j = 0; j < this.SelectArea[3]; j++) {

                this.MyMap.Grid[this.MoveArea[1] + j][this.MoveArea[0] + i] =
temp[j][i];

            }

        }

        //将要移动的区域中无用内容置成空白

        for (int i = 0; i < this.SelectArea[3]; i++) {

            for (int j = 0; j < this.SelectArea[2]; j++) {

                if (!isInRange2(this.SelectArea[0] + j,this.SelectArea[1] +
i)) {

                    //该点是不在要移动到的区域之内,需置空

                    this.MyMap.Grid[this.SelectArea[1] +
i][this.SelectArea[0] + j] = Images.BLANK;

                }

            }

        }

        //交换SelectArea和MoveArea

        byte tempbyte;

        tempbyte= SelectArea[0];

        SelectArea[0]=MoveArea[0];

        MoveArea[0]=tempbyte;

        tempbyte= SelectArea[1];

        SelectArea[1]=MoveArea[1];

        MoveArea[1]=tempbyte;

       this.selected = false;

        repaint();

    }

 增加处理悔棋的按钮:

 HuaRongDaoMidlet.java:

 private final static Command CMD_UNDO = new Command("上一步", Command.SCREEN,
1);

 ……

 else if (c == CMD_UNDO) {//处理“上一步”

            logic.unMove();

        }

 ……  

注意:A.在NetBeans当中,有许多方便的按钮,当编辑代码的时候,代码编辑区上面的最右边有两个注释和反注释的按钮,和VS的功能一样,只是没有/* 
*/形式的注释,还有缩进反缩进等按钮,编辑很方便,而且当函数参数输入完成后,直接按";"就可以自动在行尾加入分号。同样,可以加入标签:
BookMark,使得快速回到上一个位置成为可能。
 B.NetBeans把搜索也加到这个工具栏里面,可以搜索,标记,非常方便。

(2).改变移动方式,程序提供的移动方块的方式非常难操作,我希望能够点一下方块他就智能地自己寻找能够移动的位置。这里还有一点需要注意,就是

不能绕弯,也就是A-B-A-B这样来回走,如果还有其他走法,因此算法中加入了许多判断,但是比原来的代码要简单清晰易懂,操作也比原来简单多了。

代码如下:

public class ControlLogic extends Canvas implements CommandListener {

    public static final byte DIRECTION_UP    = (byte) ‘1’; //方向常量

    public static final byte DIRECTION_DOWN  = (byte) ‘2’; //方向常量

    public static final byte DIRECTION_LEFT  = (byte) ‘3’; //方向常量

    public static final byte DIRECTION_RIGHT = (byte) ‘4’; //方向常量

    private byte[] currentCursor = new byte[4];
//当前光标所在位置,四个参数分别是X,Y,width,height.

    private byte[] nextCursor    = new byte[4]; //要移动到的位置的光标区域,参数同上.

    private Map MyMap = new Map();//地图类

    private int level;//当前的关

    public int moves=0;//所用的步数.

    private History history = new History();

    public boolean isWin=false;

    public ControlLogic(int gameLevel) {//构造函数

        try {

            this.level = gameLevel;

            isWin=false;

            nbInit();//NetBeans定义的初始化函数

        }catch (Exception e) {

            e.printStackTrace();

        }

    }

    private void Init_game(){

        //初始化游戏,读取地图,设置选择区域,清空要移动到的区域

        this.currentCursor =
MyMap.read_map(this.level);//读取地图文件,并返回光标的初始位置

        //0为水平位置,1为竖直位置, 2为宽,3为高.

        nextCursor[0]=currentCursor[0]; //初始化要移动到的区域              

        nextCursor[1]=currentCursor[1];               

        nextCursor[2]=currentCursor[2];               

        nextCursor[3]=currentCursor[3];     

    }

    private void nbInit() throws Exception {//NetBeans定义的初始化函数

        //初始化实例变量

        Images.init();//初始化图片常量

        Init_game();//初始化游戏,读取地图,设置选择区域,清空要移动到的区域

        //setCommandListener(this);//添加命令监听,这是Displayable的实例方法

        //addCommand(CMD_PAUSE);//添加“暂停”按钮

    }

    public void commandAction(Command command, Displayable displayable)
{

        //命令处理函数

    }

    protected void paint(Graphics g) {       

        //画图函数,用于绘制用户画面,即显示图片,勾画选中区域

        try {

            g.drawImage(Images.image_Frame, 0, 0,Graphics.TOP |
Graphics.LEFT);//画背景

            MyMap.draw_map(g);//按照地图内容画图

            g.setColor(0, 255, 0);//绿色画笔

            g.drawRect(this.currentCursor[0] * Images.UNIT + Images.LEFT,

                this.currentCursor[1] * Images.UNIT + Images.TOP,

                this.currentCursor[2] * Images.UNIT,

                this.currentCursor[3] * Images.UNIT);//画出选择区域,

            g.setColor(255,255,255);//恢复画笔颜色

        }catch (Exception ex) {

        }

        //显示步数

        Draw.paint(g,String.valueOf(moves), 60, 15, Graphics.BASELINE |
Graphics.HCENTER);

        if ( win()) {

            isWin=true;

            Draw.paint(g,"你赢了!", 60, 70, Graphics.TOP |
Graphics.HCENTER);

        }

    }

    private void setRange() {

        //设置移动后能够选中的区域

        //调整当前光标位置到地图的主位置,即记录人物信息的位置

        if (this.MyMap.Grid[this.currentCursor[1]][this.currentCursor[0]] ==
Images.DLEFT) {

            this.currentCursor[0] -= 1;//向左调

        }else if
(this.MyMap.Grid[this.currentCursor[1]][this.currentCursor[0]] == Images.DUP)
{

            this.currentCursor[1] -= 1;//向上调

        }else if
(this.MyMap.Grid[this.currentCursor[1]][this.currentCursor[0]] ==
Images.DLEFTUP) {

            this.currentCursor[0] -= 1;//向左调

            this.currentCursor[1] -= 1;//向上调

        }

        if (this.currentCursor[0] + 1 < Images.WIDTH) {

            this.currentCursor[2] =
this.MyMap.Grid[this.currentCursor[1]][this.currentCursor[0] + 1] != (byte) ‘1’
?

            (byte)1 : (byte)2;//是横向么?

        }else {

            this.currentCursor[2] = 1;

        }

        //设置光标的高度

        if (this.currentCursor[1] + 1 < Images.HEIGHT) {

            this.currentCursor[3] = this.MyMap.Grid[this.currentCursor[1] +
1][this.currentCursor[0]] != (byte) ‘2’ ?

            (byte)1 : (byte)2;//是纵向么?

        }else {

            this.currentCursor[3] = 1;

        }

    }

    private boolean setMoveRange() {

        //设置要移动到的区域,能够移动返回true,否则返回false

        for (int i = 0; i < this.nextCursor[2]; i++) {

            for (int j = 0; j < this.nextCursor[3]; j++) {

                if (this.nextCursor[1] + j >= Images.HEIGHT ||

                    this.nextCursor[0] + i >= Images.WIDTH ||

                    (!isInRange(this.nextCursor[0] + i, this.nextCursor[1] +
j) &&

                    this.MyMap.Grid[this.nextCursor[1] +
j][this.nextCursor[0] + i] !=

                    Images.BLANK)) {

                return false;

                }

            }

        }

        return true;

    }

    private boolean isInRange(int x, int y) {

        //判断给定的(x,y)点是否在选定区域之内,x是水平坐标,y是竖直坐标

        if (x >= this.currentCursor[0] &&

            x < this.currentCursor[0] + this.currentCursor[2]
&&

            y >= this.currentCursor[1] &&

            y < this.currentCursor[1] + this.currentCursor[3]) {

            return true;

        }else {

            return false;

        }

    }

    private boolean isInRange2(int x, int y) {

        //判断给定的(x,y)点是否在要移动到的区域之内,x是水平坐标,y是竖直坐标

        if (x >= this.nextCursor[0] &&

            x <  this.nextCursor[0] + this.nextCursor[2] &&

            y >= this.nextCursor[1] &&

            y <  this.nextCursor[1] + this.nextCursor[3]) {

            return true;

        }else {

            return false;

        }

    }

   

    private boolean canMove( int direction ){

        nextCursor[0]=currentCursor[0];               

        nextCursor[1]=currentCursor[1];               

        nextCursor[2]=currentCursor[2];               

        nextCursor[3]=currentCursor[3];       

        switch (direction){

            case DIRECTION_UP:

                if (this.currentCursor[1] – 1 >= 0) {//向上还有移动空间

                    this.nextCursor[1]–;//向上移动一下

                    if (!setMoveRange()){ //不能移动

                        this.nextCursor[1]++;//退回来

                        return false;

                    }

                    else {

                        this.nextCursor[1]++;//退回来

                        return true;

                    }

                }

                else

                    return false;

            case DIRECTION_DOWN:

                if (this.currentCursor[1] + 1 < Images.HEIGHT)
{//向下还有移动空间

                    this.nextCursor[1]++;//向下移动一下

                    if (!setMoveRange()){ //不能移动

                        this.nextCursor[1]–;//退回来

                        return false;

                    }

                    else {

                        this.nextCursor[1]–;//退回来

                        return true;

                    }

                }

                else

                    return false;

            case DIRECTION_LEFT:

                if (this.currentCursor[0] – 1 >= 0) {//向左还有移动空间

                    this.nextCursor[0]–;//向左移动一下

                    if (!setMoveRange()){ //不能移动

                        this.nextCursor[0]++;//退回来

                        return false;

                    }

                    else {

                        this.nextCursor[0]++;//退回来

                        return true;

                    }

                }

                else

                    return false;

            case DIRECTION_RIGHT:

                if (this.currentCursor[0] + 1 < Images.WIDTH)
{//向右还有移动空间

                    this.nextCursor[0]++;//向右移动一下

                    if (!setMoveRange()){ //不能移动

                        this.nextCursor[0]–;//退回来

                        return false;

                    }

                    else {

                        this.nextCursor[0]–;//退回来

                        return true;

                    }

                }

                else

                    return false;

            default:

                return false;

        }

    }

 

    protected void keyPressed(int keyCode) {

        //处理按下键盘的事件,这是Canvas的实例方法

        switch (getGameAction(keyCode)) {//将按键的值转化成方向常量

            case Canvas.UP://向上

                if (this.currentCursor[1] – 1 >= 0) {//向上还有移动空间

                    this.currentCursor[1]–;//向上移动一下

                    setRange();

                    repaint();//重新绘图

                }

                break;

            case Canvas.DOWN://向下

                if (this.currentCursor[1] + 1 < Images.HEIGHT)
{//向下还有移动空间

                    this.currentCursor[1]+=currentCursor[3];//向下移动一下

                    setRange();

                    repaint();//重新绘图

                }

                break;

            case Canvas.LEFT://向左

                if (this.currentCursor[0] – 1 >= 0) {//向左还有移动空间

                    this.currentCursor[0]–;//向左移动方块长度

                    setRange();

                    repaint();//重新绘图

                }

            break;

            case Canvas.RIGHT://向右

                //看向右还能否移动.

                if (this.currentCursor[0] + 1 < Images.WIDTH)
{//向右还有移动空间

                    this.currentCursor[0]+=currentCursor[2];//向右移动一下

                    setRange();

                    repaint();//重新绘图

                }

                break;

            case Canvas.FIRE:

               

                nextCursor[0]=currentCursor[0];               

                nextCursor[1]=currentCursor[1];               

                nextCursor[2]=currentCursor[2];               

                nextCursor[3]=currentCursor[3]; 

                if( moves == 0 ){//第一步

                    if ( canMove(DIRECTION_UP) )

                        this.nextCursor[1]–;//向上移动一下

                    else if ( canMove(DIRECTION_DOWN) )

                        this.nextCursor[1]++;//向下移动一下

                    else if ( canMove(DIRECTION_LEFT) )

                        this.nextCursor[0]–;//向左移动一下

                    else if ( canMove(DIRECTION_RIGHT) )

                        this.nextCursor[0]++;//向右移动一下

                    else

                        break;

                    Move();

                    repaint();

                    break;

                }

               

                byte[] lastStep = new byte[5];
//五个参数分别为,前四个和CurrentCursor一样,最后一个表示上1,下2,左3,右4。

                lastStep = (byte []) history.getLastStep();//取得上一步移动

             

                //向上

                if (canMove(DIRECTION_UP)) {

                    if ( ( nextCursor[0] == lastStep[0] &&
nextCursor[1]-1 == lastStep[1] && lastStep[4] == DIRECTION_DOWN)
&& //如果是走回头路

                         (
!(canMove(DIRECTION_DOWN))&&!(canMove(DIRECTION_LEFT))&&!(canMove(DIRECTION_RIGHT)))
|| //而且只有这一种走法

                         !( nextCursor[0] == lastStep[0] &&
nextCursor[1]-1 == lastStep[1] && lastStep[4] == DIRECTION_DOWN)
)//或者不是走原路

                        this.nextCursor[1]–;//向上移动一下

                    else if (canMove(DIRECTION_DOWN))

                        this.nextCursor[1]++;//向下移动一下

                    else if (canMove(DIRECTION_LEFT))

                        this.nextCursor[0]–;//向左移动一下

                    else if (canMove(DIRECTION_RIGHT))

                        this.nextCursor[0]++;//向右移动一下

                    Move();

                    repaint();

                    break;

                }

                 //向下

                if (canMove(DIRECTION_DOWN)) {

                    if ( ( nextCursor[0] == lastStep[0] &&
nextCursor[1]+1 == lastStep[1] && lastStep[4] == DIRECTION_UP)
&& //如果是走回头路

                         (
!(canMove(DIRECTION_UP))&&!(canMove(DIRECTION_LEFT))&&!(canMove(DIRECTION_RIGHT)))||//而且只有这一种走法

                         !( nextCursor[0] == lastStep[0] &&
nextCursor[1]+1 == lastStep[1] && lastStep[4] == DIRECTION_UP)
)//或者不是走原路

                        this.nextCursor[1]++;//向下移动一下

                    else if (canMove(DIRECTION_UP))

                        this.nextCursor[1]–;//向上移动一下

                    else if (canMove(DIRECTION_LEFT))

                        this.nextCursor[0]–;//向左移动一下

                    else if (canMove(DIRECTION_RIGHT))

                        this.nextCursor[0]++;//向右移动一下

 

                    Move();

                    repaint();

                    break;

                }

                 //向左

                if (canMove(DIRECTION_LEFT)) {

                    if ( ( nextCursor[0]-1 == lastStep[0] &&
nextCursor[1] == lastStep[1] && lastStep[4] == DIRECTION_RIGHT)
&& //如果是走回头路

                         (
!(canMove(DIRECTION_UP))&&!(canMove(DIRECTION_DOWN))&&!(canMove(DIRECTION_RIGHT)))||//而且只有这一种走法

                         !( nextCursor[0]-1 == lastStep[0] &&
nextCursor[1] == lastStep[1] && lastStep[4] == DIRECTION_RIGHT)
)//或者不是走原路

                        this.nextCursor[0]–;//向左移动一下

                    else if (canMove(DIRECTION_UP))

                        this.nextCursor[1]–;//向上移动一下

                    else if (canMove(DIRECTION_DOWN))

                        this.nextCursor[1]++;//向下移动一下

                    else if (canMove(DIRECTION_RIGHT))

                        this.nextCursor[0]++;//向右移动一下

                    Move();

                    repaint();

                    break;

                }

                 //向右

                if (canMove(DIRECTION_RIGHT)) {

                    if ( ( nextCursor[0]+1 == lastStep[0] &&
nextCursor[1] == lastStep[1] && lastStep[4] == DIRECTION_LEFT)
&& //如果是走回头路

                         (
!(canMove(DIRECTION_UP))&&!(canMove(DIRECTION_DOWN))&&!(canMove(DIRECTION_LEFT)))
||//而且只有这一种走法

                         !( nextCursor[0]+1 == lastStep[0] &&
nextCursor[1] == lastStep[1] && lastStep[4] == DIRECTION_LEFT)
)//或者不是走原路

                        this.nextCursor[0]++;//向右移动一下

                    else if (canMove(DIRECTION_UP))

                        this.nextCursor[1]–;//向上移动一下

                    else if (canMove(DIRECTION_DOWN))

                        this.nextCursor[1]++;//向下移动一下

                    else if (canMove(DIRECTION_LEFT))

                        this.nextCursor[0]–;//向左移动一下

                    Move();

                    repaint();

                    break;

                }

        }

    }

    private boolean win(){

        //判断是否已经救出了曹操

        if ( this.MyMap.Grid[Images.HEIGHT – 2 ][Images.WIDTH – 3 ] ==
Images.CAOCAO )

        {

            return true;

        }

        else

            return false;

    }

    private void PrintGrid(String a) {

        //打印当前地图的内容,用于调试

        System.out.println(a);

        for (int i = 0; i < Images.HEIGHT; i++) {

            for (int j = 0; j < Images.WIDTH; j++) {

                System.out.print( (char)this.MyMap.Grid[i][j]);

            }

            System.out.println("");

        }

    }

    private void PrintHistory(String a) {

        //打印当前地图的内容,用于调试

        byte [] temp =new byte[5];

        System.out.println(a);

        for (int i = 0; i < this.history.getSize(); i++) {

            temp = (byte [])this.history.getStepAt(i);

            for (int j = 0; j < 5; j++) {

                System.out.print(String.valueOf(temp[j]));

            }

            System.out.println("");

        }

    }

    private byte getMoveDirection(){

        byte direction=0;

        if ( this.currentCursor[0] < P>

            direction=DIRECTION_RIGHT;//向右移动;

        if ( this.currentCursor[0]>this.nextCursor[0])

            direction=DIRECTION_LEFT;//向左移动;

        if ( this.currentCursor[1] < P>

            direction=DIRECTION_DOWN;//向下移动;

        if ( this.currentCursor[1]>this.nextCursor[1])

            direction=DIRECTION_UP;//向上移动;

        return direction;

    }

    public void unMove(){

        if ( moves == 0 )

            return;

        //因为unMove()的上一步肯定是可行的,所以不用setMoveRange().

        byte[] step = new byte[5];
//五个参数分别为,前四个和CurrentCursor一样,最后一个表示上1,下2,左3,右4。

        step = (byte []) history.getLastStep();//取得上一步移动

        history.removeLastStep();//减少一步;

        moves–;

        for (int i= 0; i< 4;i++){

            this.nextCursor[i] = step[i];//重设nextCursor  

            this.currentCursor[i] = step[i];//重设currentCursor

        }

        //根据上一步的方向,反方向移动

        if (step[4] == DIRECTION_UP)

            this.currentCursor[1] = (byte) (step[1]-1);

        else if (step[4] == DIRECTION_DOWN)

            this.currentCursor[1] = (byte) (step[1]+1);

        else if (step[4] == DIRECTION_LEFT)

            this.currentCursor[0] = (byte) (step[0]-1);

        else if (step[4] == DIRECTION_RIGHT)

            this.currentCursor[0] = (byte) (step[0]+1);

        //移动回来.

        byte[][] temp = new
byte[this.currentCursor[3]][this.currentCursor[2]];

        //复制要移动的区域,因为这块区域可能会被覆盖掉

        for (int i = 0; i < this.currentCursor[2]; i++) {

            for (int j = 0; j < this.currentCursor[3]; j++) {

                temp[j][i] = this.MyMap.Grid[this.currentCursor[1]
+j][this.currentCursor[0] + i];

            }

        }

        //将要移动的区域移动到刚选中的区域(即要移动到的区域)

        for (int i = 0; i < this.currentCursor[2]; i++) {

            for (int j = 0; j < this.currentCursor[3]; j++) {

                this.MyMap.Grid[this.nextCursor[1] + j][this.nextCursor[0] +
i] = temp[j][i];

            }

        }

        //将要移动的区域中无用内容置成空白

        for (int i = 0; i < this.currentCursor[3]; i++) {

            for (int j = 0; j < this.currentCursor[2]; j++) {

                if (!isInRange2(this.currentCursor[0] +
j,this.currentCursor[1] + i)) {

                    //该点是不在要移动到的区域之内,需置空

                    this.MyMap.Grid[this.currentCursor[1] +
i][this.currentCursor[0] + j] = Images.BLANK;

                }

            }

        }
  

        //交换currentCursor和nextCursor

        this.currentCursor[0]=nextCursor[0];

        this.currentCursor[1]=nextCursor[1];

        repaint();

    }

    private void Move() {

        if ( this.currentCursor[0]==this.nextCursor[0] &&
this.currentCursor[1]==this.nextCursor[1] )

            return;  //不需要移动就返回.       

        //将要移动的区域移动到刚选中的区域

        if (this.nextCursor[0] == -1 || this.nextCursor[1] == -1 ||

            this.currentCursor[0] == -1 || this.currentCursor[1] == -1)
{//没有选中区域

        }

        else {//已经选中了要移动的区域和要移动到的区域

            byte[][] temp = new
byte[this.currentCursor[3]][this.currentCursor[2]];

            //复制要移动的区域,因为这块区域可能会被覆盖掉

            for (int i = 0; i < this.currentCursor[2]; i++) {

                for (int j = 0; j < this.currentCursor[3]; j++) {

                    temp[j][i] = this.MyMap.Grid[this.currentCursor[1]
+j][this.currentCursor[0] + i];

                }

            }

            // 调试信息

            //将要移动的区域移动到刚选中的区域(即要移动到的区域)

            for (int i = 0; i < this.currentCursor[2]; i++) {

                for (int j = 0; j < this.currentCursor[3]; j++) {

                    this.MyMap.Grid[this.nextCursor[1] +
j][this.nextCursor[0] + i] = temp[j][i];

                }

            }

            //PrintGrid("2");// 调试信息

            //将要移动的区域中无用内容置成空白

            for (int i = 0; i < this.currentCursor[3]; i++) {

                for (int j = 0; j < this.currentCursor[2]; j++) {

                    if (!isInRange2(this.currentCursor[0] +
j,this.currentCursor[1] + i)) {

                        //该点是不在要移动到的区域之内,需置空

                        this.MyMap.Grid[this.currentCursor[1] +
i][this.currentCursor[0] + j] = Images.BLANK;

                    }else {

                    }

                }

            }
           
            moves++;// 增加移动的步骤
        
            byte[] step = new byte[5];
//五个参数分别为,前四个和SelectArea一样,最后一个表示上1,下2,左3,右4。

            //将此次移动记录到历史记录当中;

            step[0]= this.currentCursor[0];

            step[1]= this.currentCursor[1];

            step[2]= this.currentCursor[2];

            step[3]= this.currentCursor[3];

            step[4]= this.getMoveDirection();

            history.addStep(step);

            //PrintHistory("record a step");// 调试信息

            //PrintGrid("3");// 调试信息

            this.currentCursor[0] = this.nextCursor[0];//重置选中位置的水平坐标

            this.currentCursor[1] = this.nextCursor[1];//重置选中位置的竖直坐标

            this.nextCursor[0] = -1;//清空要移动到的位置

            this.nextCursor[1] = -1;//清空要移动到的位置

            this.nextCursor[2] = 0;//清空要移动到的位置

            this.nextCursor[3] = 0;//清空要移动到的位置

        }

    }

}