WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章J2ME技术程序开发一个基于MIDP的迷宫游戏

一个基于MIDP的迷宫游戏
作者:J2MEDEV  来源:J2MEDEV  发布时间:2006-7-25 9:08:00

/*
 * Maze.java
 *
 * Created on 2005年12月2日, 下午1:04
 */

package com.j2medev.maze;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * This is the main class of the maze game.
 *
 * @author Carol Hamer
 */
public class Maze extends MIDlet implements CommandListener {
   
    //----------------------------------------------------------------
    //  game object fields
   
    /**
     * The canvas that the maze is drawn on.
     */
    private MazeCanvas myCanvas;
   
    /**
     * The screen that allows the user to alter the size parameters
     * of the maze.
     */
    private SelectScreen mySelectScreen;
   
    //----------------------------------------------------------------
    //  command fields
   
    /**
     * The button to exit the game.
     */
    private Command myExitCommand = new Command("Exit", Command.EXIT, 99);
   
    /**
     * The command to create a new maze.  (This command may appear in a menu)
     */
    private Command myNewCommand = new Command("New Maze", Command.SCREEN, 1);
   
    /**
     * The command to dismiss an alert error message.  In MIDP 2.0
     * an Alert set to Alert.FOREVER automatically has a default
     * dismiss command.  This program does not use it in order to
     * allow backwards com
     */
    private Command myAlertDoneCommand = new Command("完成", Command.EXIT, 1);
   
    /**
     * The command to go to the screen that allows the user
     * to alter the size parameters.  (This command may appear in a menu)
     */
    private Command myPrefsCommand
            = new Command("迷宫规模设置", Command.SCREEN, 1);
   
    //----------------------------------------------------------------
    //  initialization
   
    /**
     * Initialize the canvas and the commands.
     */
    public Maze() {
        try {     myCanvas = new MazeCanvas(Display.getDisplay(this));
        myCanvas.addCommand(myExitCommand);
        myCanvas.addCommand(myNewCommand);
        myCanvas.addCommand(myPrefsCommand);
        myCanvas.setCommandListener(this);
        } catch(Exception e) {
            // if there's an error during creation, display it as an alert.
            Alert errorAlert = new Alert("error",
                    e.getMessage(), null, AlertType.ERROR);
            errorAlert.setCommandListener(this);
            errorAlert.setTimeout(Alert.FOREVER);
            errorAlert.addCommand(myAlertDoneCommand);
            Display.getDisplay(this).setCurrent(errorAlert);
        }
    }
   
    //----------------------------------------------------------------
    //  implementation of MIDlet
   
    /**
     * Start the application.
     */
    public void startApp() throws MIDletStateChangeException {
        if(myCanvas != null) {
            myCanvas.start();
        }
    }
    /**
     * Clean up.
     */
    public void destroyApp(boolean unconditional)
    throws MIDletStateChangeException {
        myCanvas = null;
        System.gc();
    }
   
    /**
     * Does nothing since this program occupies no shared resources
     * and little memory.
     */
    public void pauseApp() {
    }
   
    //----------------------------------------------------------------
    //  implementation of CommandListener
   
  /*
   * Respond to a command issued on the Canvas.
   * (reset, exit, or change size prefs).
   */
    public void commandAction(Command c, Displayable s) {
        if(c == myNewCommand) {
            myCanvas.newMaze();
        } else if(c == myAlertDoneCommand) {
            try {
                destroyApp(false);
                notifyDestroyed();
            } catch (MIDletStateChangeException ex) {
            }
        } else if(c == myPrefsCommand) {
            if(mySelectScreen == null) {
                mySelectScreen = new SelectScreen(myCanvas);
            }
            Display.getDisplay(this).setCurrent(mySelectScreen);
        } else if(c == myExitCommand) {
            try {
                destroyApp(false);
                notifyDestroyed();
            } catch (MIDletStateChangeException ex) {
            }
        }
    }
}

/*
 * SelectScreen.java
 *
 * Created on 2005年12月2日, 下午1:06
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package com.j2medev.maze;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;

/**
 * This is the screen that allows the user to modify the
 * width of the maze walls..
 *
 * @author Carol Hamer
 */
class SelectScreen extends Form
  implements ItemStateListener, CommandListener  {

  //----------------------------------------------------------------
  //  fields

  /**
   * The "Done" button to exit this screen and return to the maze.
   */
  private Command myExitCommand = new Command("完成", Command.EXIT, 1);

  /**
   * The gague that modifies the width of the maze walls.
   */
  private Gauge myWidthGauge;

  /**
   * The gague that displays the number of columns of the maze.
   */
  private Gauge myColumnsGauge;

  /**
   * A handle to the main game canvas.
   */
  private MazeCanvas myCanvas;

  //----------------------------------------------------------------
  //  initialization

  /**
   * Create the gagues and place them on the screen.
   */
  public SelectScreen(MazeCanvas canvas) {
    super("Size Preferences"

[1] [2] [3] [4] [5]  下一页

[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码