现在的位置: 首页 > 综合 > 正文

用NetBeans开发J2ME游戏实例讲解(二)

2013年12月07日 ⁄ 综合 ⁄ 共 18914字 ⁄ 字号 评论关闭

作者:林刚 文章来源:http://blogs.sun.com/lirincy

3).建立Draw类用来显示图形:

public class Draw {

      /** Creates a new instance of Draw */

    public Draw(Canvas canvas) {

    }

      public static boolean paint(Graphics g, byte img, int x, int y) {

        //在地图的x,y点绘制img指定的图片

        try {

            paint(g, img, x, y, Images.UNIT);//把地图x,y点转化成画布的绝对坐标,绘图

            return true;

        }

        catch (Exception ex) {

            return false;

        }

    }

     public static boolean paint(Graphics g, byte img, int x, int y, int unit) {

        try {

            switch (img) {

                case Images.CAOCAO://画曹操

                    //变成绝对坐标,并做调整

                    g.drawImage(Images.image_Caocao, Images.LEFT + x * unit,

                    Images.TOP + y * unit,Graphics.TOP | Graphics.LEFT);

                    break;

                case Images.GUANYU://画关羽

                    g.drawImage(Images.image_Guanyu, Images.LEFT + x * unit,

                    Images.TOP + y * unit,Graphics.TOP | Graphics.LEFT);

                    break;

                case Images.HUANGZHONG://画黄忠

                    g.drawImage(Images.image_Huangzhong, Images.LEFT + x * unit,

                    Images.TOP + y * unit,Graphics.TOP | Graphics.LEFT);

                    break;

                case Images.MACHAO://画马超

                    g.drawImage(Images.image_Machao, Images.LEFT + x * unit,

                    Images.TOP + y * unit, Graphics.TOP | Graphics.LEFT);

                    break;

                case Images.ZHANGFEI://画张飞

                    g.drawImage(Images.image_Zhangfei, Images.LEFT + x * unit,

                    Images.TOP + y * unit,Graphics.TOP | Graphics.LEFT);

                    break;

                case Images.ZHAOYUN://画赵云

                    g.drawImage(Images.image_Zhaoyun, Images.LEFT + x * unit,

                    Images.TOP + y * unit,

                    Graphics.TOP | Graphics.LEFT);

                    break;

                case Images.ZU://画卒

                    g.drawImage(Images.image_Zu, Images.LEFT + x * unit,

                    Images.TOP + y * unit, Graphics.TOP | Graphics.LEFT);

                    break;

                case Images.BLANK://画空白

                    g.drawImage(Images.image_Blank, Images.LEFT + x * unit,

                    Images.TOP + y * unit, Graphics.TOP | Graphics.LEFT);

                    break;

                case Images.CURSOR://画光标

                    g.drawRect(Images.LEFT + x * unit,

                    Images.TOP + y * unit,Images.UNIT,Images.UNIT);

                    break;

            }

            return true;

        }catch (Exception ex) {

            return false;

        }

    }

}

 (4)建立Map类来读取布局信息:

package HuaRongDao;

import java.io.InputStream;

import javax.microedition.lcdui.*;

 /**

 *

 * @author lin

 */

public class Map {

    //处理游戏的地图,负责从外部文件加载地图数据,存放地图数据,并按照地图数据绘制地图

     public byte Grid[][];//存放地图数据

     public Map() {//构造函数,负责初始化地图数据的存储结构

        this.Grid = new byte[Images.HEIGHT][Images.WIDTH];

        //用二维数组存放地图数据,注意第一维是竖直坐标,第二维是水平坐标

    }

     public int[] read_map(int i) {

        //从外部文件加载地图数据,并存放在存储结构中,返回值是光标点的位置

        //参数是加载地图文件的等级

        int[] a = new int[2];//光标点的位置,0是水平位置,1是竖直位置

        try {

            InputStream is = getClass().getResourceAsStream("/levels/level".concat(String.valueOf(i)));

            if (is != null) {

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

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

                        this.Grid[k][j] = (byte) is.read();

                        if ( this.Grid[k][j] == Images.CURSOR ) {

                            //判断出光标所在位置

                            a[0] = j;//光标水平位置

                            a[1] = k;//光标竖直位置

                            this.Grid[k][j] = Images.BLANK;//将光标位置设成空白背景

                        }

                    }

                    is.read();//读取回车(13),忽略掉

                    is.read();//读取换行(10),忽略掉

                }

                is.close();

            }else {

            //读取文件失败

            a[0] = -1;

            a[1] = -1;

        }

        }catch (Exception ex) {

            //打开文件失败

            a[0] = -1;

            a[1] = -1;

        }

        return a;

    }

     public boolean draw_map(Graphics g) {

        //调用Draw类的静态方法,绘制地图

        try {

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

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

                    Draw.paint(g, this.Grid[i][j], j, i);//绘制地图

                }

            }

            return true;

        }catch (Exception ex) {

            return false;

        }

    }

}

注意这里的读文件操作的文件位置同样是相对于res文件夹的。

 (5) 建立主逻辑控制:

package HuaRongDao;

 /**

 *

 * @author lin

 */

 import javax.microedition.lcdui.*;

 public class ControlLogic extends Canvas implements CommandListener {

     private int[] loc = new int[2]; //光标的当前位置,0是水平位置,1是竖直位置

    private int[] SelectArea = new int[4];//被选定的区域,即要移动的区域

    private int[] MoveArea = new int[4];//要移动到的区域

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

    private boolean selected;//是否已经选中要移动区域的标志

    private int level;//当前的关

     public ControlLogic() {//构造函数

        try {

            jbInit();//JBuilder定义的初始化函数

        }catch (Exception e) {

            e.printStackTrace();

        }

    }

     private void Init_game(){

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

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

        //0为水平位置,1为竖直位置

        this.SelectArea[0] = this.loc[0];//初始化选中的区域

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

        this.SelectArea[2] = 1;

        this.SelectArea[3] = 1;

        this.MoveArea[0] = -1;//初始化要移动到的区域

        this.MoveArea[1] = -1;

        this.MoveArea[2] = 0;

        this.MoveArea[3] = 0;

    }

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

        //初始化实例变量

        this.selected = false;//设置没有被选中的要移动区域

        this.level = 1;

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

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

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

        //addCommand(new Command("", Command.EXIT, 1));//添加“退出”按钮

    }

     public void commandAction(Command command, Displayable displayable) {

        //命令处理函数

        if (command.getCommandType() == Command.EXIT) {//处理“退出”

        //HuaRongDaoMidlet.quitApp();

        }

    }

     protected void paint(Graphics g) {

        //画图函数,用于绘制用户画面,即显示图片,勾画选中区域和要移动到的区域

        try {

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

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

            if ( this.selected )

                g.setColor(0,255,0);//如果被选中,改用绿色画出被选中的区域

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

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

                this.SelectArea[2] * Images.UNIT,

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

            //如果被选中,就用绿色

            //否则,使用黑色

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

            if (this.selected) {//已经选中了要移动的区域

                g.setColor(255, 0, 255);//改用红色

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

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

                this.MoveArea[2] * Images.UNIT,

                this.MoveArea[3] * Images.UNIT);//画出要移动到的区域

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

            }

        }catch (Exception ex) {

        }

       

        System.out.println(Runtime.getRuntime().freeMemory());

        System.out.println(Runtime.getRuntime().totalMemory());

    }

 

    private void setRange() {

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

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

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

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

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

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

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

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

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

        }

        this.SelectArea[0] = this.loc[0];//设置光标的水平位置

        this.SelectArea[1] = this.loc[1];//设置光标的竖直位置

        //设置光标的宽度

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

            this.SelectArea[2] = this.MyMap.Grid[this.loc[1]][this.loc[0] + 1] != (byte) '1' ?

            1 : 2;

        }else {

            this.SelectArea[2] = 1;

        }

        //设置光标的高度

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

        this.SelectArea[3] = this.MyMap.Grid[this.loc[1] + 1][this.loc[0]] != (byte) '2' ?

        1 : 2;

        }else {

            this.SelectArea[3] = 1;

        }

    }

     private boolean setMoveRange() {

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

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

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

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

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

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

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

                    Images.BLANK)) {

                return false;

                }

            }

        }

        this.MoveArea[0] = this.loc[0];

        this.MoveArea[1] = this.loc[1];

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

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

        return true;

    }

     private boolean isInRange(int x, int y) {

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

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

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

            y >= this.SelectArea[1] &&

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

            return true;

        }else {

            return false;

        }

    }

     private boolean isInRange2(int x, int y) {

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

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

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

            y >= this.MoveArea[1] &&

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

            return true;

        }else {

            return false;

        }

    }

     protected void keyPressed(int keyCode) {

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

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

            case Canvas.UP://向上

                if (!this.selected) {//还没有选定要移动的区域

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

                    this.loc[1]--;//向上移动一下

                    setRange();//设置光标移动的区域,该函数能将光标移动到地图主位置

                    repaint();//重新绘图

                }

                }else {//已经选定了要移动的区域

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

                        this.loc[1]--;//向上移动一下

                        if (setMoveRange()) {//能够移动,该函数能够设置要移动到的区域

                            Move();

                            repaint();//重新绘图

                        }else {//不能移动

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

                        }

                    }

                }

                break;

            case Canvas.DOWN://向下

                if (!this.selected) {//还没有选定要移动的区域

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

                        if (this.MyMap.Grid[this.loc[1] + 1][this.loc[0]] == Images.DUP){//该图片有两个格高

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

                            if (this.loc[1] + 1 < Images.HEIGHT) {//向下还有

                                //移动空间

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

                                setRange();//设置光标移动的区域,

                                //该函数能将光标移动到地图主位置

                                repaint();//重新绘图

                            }else {//向下没有移动空间

                                this.loc[1]--;//退回来

                            }

                        }else {//该图片只有一个格高

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

                            setRange();//设置光标移动的区域,

                            //该函数能将光标移动到地图主位置

                            repaint();//重新绘图

                        }

                    }else {

                    }

                }else {//已经选定了要移动的区域

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

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

                        if (setMoveRange()) {//能够移动,该函数能够设置要移动到的区域

                            Move();

                            repaint();//重新绘图

                        }else {//不能移动

                            this.loc[1]--;//退回来

                        }

                    }

                }

                break;

            case Canvas.LEFT://向左

                if (!this.selected) {//还没有选定要移动的区域

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

                    this.loc[0]--;//向左移动一下

                    setRange();//设置光标移动的区域,该函数能将光标移动到地图主位置

                    repaint();//重新绘图

                }

                }else {//已经选定了要移动的区域

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

                        this.loc[0]--;//向左移动一下

                        if (setMoveRange()) {//能够移动,该函数能够设置要移动到的区域

                            Move();

                            repaint();//重新绘图

                        }else {//不能移动

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

                        }

                    }

                }

            break;

            case Canvas.RIGHT://向右

                if (!this.selected) {//还没有选定要移动的区域

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

                        if (this.MyMap.Grid[this.loc[1]][this.loc[0] + 1] == Images.DLEFT) {//该图片有两个格宽

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

                            if (this.loc[0] + 1 < Images.WIDTH) {//向右还有

                                //移动空间

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

                                setRange();//设置光标移动的区域,

                                //该函数能将光标移动到地图主位置

                                repaint();//重新绘图

                            }else {//向右没有移动空间

                                this.loc[0]--;//退回来

                            }

                        }else {//该图片只有一个格宽

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

                            setRange();//设置光标移动的区域,

                            //该函数能将光标移动到地图主位置

                            repaint();//重新绘图

                        }

                    }else {

                    }

                }else {//已经选定了要移动的区域

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

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

                        if (setMoveRange()) {//能够移动,该函数能够设置要移动到的区域

                            Move();

                            repaint();//重新绘图

                        }else {//不能移动

                            this.loc[0]--;//退回来

                        }

                    }

                }

                break;

            case Canvas.FIRE:

                if (this.selected) {//已经选定了要移动的区域

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

                    repaint();//重新绘图

                    this.selected = false;//清除已选定要移动区域的标志

                    if ( win()) {

                        System.out.println("win");

                    }

                }else {//还没有选定要移动的区域

                    if (this.MyMap.Grid[this.loc[1]][this.loc[0]] == Images.BLANK) {//要移到的位置是一个空白

                    }else {//要移到的位置不是空白

                        this.selected = true;//设置已选定要移动区域的标志

                    }

                    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 Move() {

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

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

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

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

            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];

                }

            }

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

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

            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;

                    }else {

                    }

                }

            }

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

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

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

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

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

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

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

        }

    }

}

 

 

(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()事件里判断,直接跳出,也就是不接受任何除了暂停键的其它输入.

抱歉!评论已关闭.