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
);
    addCommand(myExitCommand);
    setCommandListener(this);
    myCanvas = canvas;
    setItemStateListener(this);
    myWidthGauge = new Gauge("每一列的宽度", true,
           myCanvas.getMaxColWidth(),
           myCanvas.getColWidth());
    myColumnsGauge = new Gauge("迷宫的列数", false, 
             myCanvas.getMaxNumCols(),
             myCanvas.getNumCols());
    // Warning: the setLayout method does not exist in
    // MIDP 1.4.  If there is any chance that a target
    // device will be using MIDP 1.4, comment out the
    // following two lines:
    //myWidthGauge.setLayout(Item.LAYOUT_CENTER);
    //myColumnsGauge.setLayout(Item.LAYOUT_CENTER);
    append(myWidthGauge);
    append(myColumnsGauge);
  }

  //----------------------------------------------------------------
  //  implementation of ItemStateListener

  /**
   * Respond to the user changing the width.
   */
  public void itemStateChanged(Item item) {
    if(item == myWidthGauge) {
      int val = myWidthGauge.getValue();
      if(val < myCanvas.getMinColWidth()) {
  myWidthGauge.setValue(myCanvas.getMinColWidth());
      } else {
  int numCols = myCanvas.setColWidth(val);
  myColumnsGauge.setValue(numCols);
      }
    }
  }

  //----------------------------------------------------------------
  //  implementation of CommandListener

  /*
   * Respond to a command issued on this screen.
   * (either reset or exit).
   */
  public void commandAction(Command c, Displayable s) {
    if(c == myExitCommand) {
      myCanvas.newMaze();
    }
  }
}

/*
 * MazeCanvas.java
 *
 * Created on 2005年12月2日, 下午1:05
 *
 * 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.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

/**
 * This class is the display of the game.
 *
 * @author Carol Hamer
 */
class MazeCanvas extends javax.microedition.lcdui.Canvas {
   
    //---------------------------------------------------------
    //   static fields
   
    /**
     * color constant
     */
    public static final int BLACK = 0;
   
    /**
     * color constant
     */
    public static final int WHITE = 0xffffff;
   
    //---------------------------------------------------------
    //   instance fields
   
    /**
     * a handle to the display.
     */
    private Display myDisplay;
   
    /**
     * The data object that describes the maze configuration.
     */
    private Grid myGrid;
   
    /**
     * Whether or not the currently displayed maze has
     * been completed.
     */
    private boolean myGameOver = false;
   
    /**
     * maze dimension: the width of the maze walls.
     */
    private int mySquareSize;
   
    /**
     * maze dimension: the maximum width possible for the maze walls.
     */
    private int myMaxSquareSize;
   
    /**
     * maze dimension: the minimum width possible for the maze walls.
     */
    private int myMinSquareSize;
   
    /**
     * top corner of the display: x-coordiate
     */
    private int myStartX = 0;
   
    /**
     * top corner of the display: y-coordinate
     */
    private int myStartY = 0;
   
    /**
     * how many rows the display is divided into.
     */
    private int myGridHeight;
   
    /**
     * how many columns the display is divided into.
     */
    private int myGridWidth;
   
    /**
     * the maximum number columns the display can be divided into.
     */
    private int myMaxGridWidth;
   
    /**
     * the minimum number columns the display can be divided into.
     */
    private int myMinGridWidth;
   
    /**
     * previous location of the player in the maze: x-coordiate
     * (in terms of the coordinates of the maze grid, NOT in terms
     * of the coordinate system of the Canvas.)
     */
    private int myOldX = 1;
   
    /**
     * previous location of the player in the maze: y-coordinate
     * (in terms of the coordinates of the maze grid, NOT in terms
     * of the coordinate system of the Canvas.)
     */
    private int myOldY = 1;
   
    /**
     * current location of the player in the maze: x-coordiate
     * (in terms of the coordinates of the maze grid, NOT in terms
     * of the coordinate system of the Canvas.)
     */
    private int myPlayerX = 1;
   
    /**
     * current location of the player in the maze: y-coordinate
     * (in terms of the coordinates of the maze grid, NOT in terms
     * of the coordinate system of the Canvas.)
     */
    private int myPlayerY = 1;
   
    //-----------------------------------------------------
    //    gets / sets
   
    /**
     * Changes the width of the maze walls and calculates how
     * this change affects the number of rows and columns
     * the maze can have.
     * @return the number of columns now that the the
     *         width of the columns has been updated.
     */
    int setColWidth(int colWidth) {
        if(colWidth < 2) {
            mySquareSize = 2;
        } else {
            mySquareSize = colWidth;
        }
        myGridWidth = getWidth() / mySquareSize;
        if(myGridWidth % 2 == 0) {
            myGridWidth -= 1;
        }
        myGridHeight = getHeight() / mySquareSize;
        if(myGridHeight % 2 == 0) {
            myGridHeight -= 1;
        }
        myGrid = null;
        return(myGridWidth);
    }
   
    /**
     * @return the minimum width possible for the maze walls.
     */
    int getMinColWidth() {
        return(myMinSquareSize);
    }
   
    /**
     * @return the maximum width possible for the maze walls.
     */
    int getMaxColWidth

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

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

用户名: 查看更多评论

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

内 容:

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