|
|
扫雷游戏的设计【转】【收藏】 |
|
|
作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站 |
J2ME伪高手先锋开讲 --扫雷游戏的设计 首先我要装得像高手一样,来假装把系统稍微分析一下。
一般,按照java得开发模式,这种程序一般是分为三个模块来开发。
如下三个:
一个程序运作的主文件,也就是一个midlet的继承;
一个界面的表示类,也就是一个canvas的继承,界面上应该有些菜单,如new、exit 什么的,那就应该要 implements一个 commandListener消息监听类(大家可以把java的消息监听理解为一个线程,一直像倭寇那样对看得顺眼的东西虎视耽耽,当然这里指的是他所能触及到的消息,当收到消息的时候,会调用一个抽象函数public void commandAction(Command c, Displayable d),而这个抽象函数使得我们可以通过对他的实现来处理收到的消息,即消息响应)
最后一个当然就是与界面无关的逻辑单元了,在这里我们定义整个游戏的逻辑,做到逻辑与界面分开。这是我学java的最大收获,呵呵。
来自:低调一贱男, 时间:2003-10-25 23:28:00, ID:2252869 | 编辑
首先正式开始第一讲 <扫雷游戏的逻辑>
我的设想是,扫雷的地图一般是一个矩形,因为,圆形屏幕的手机看起来蛮变态的,没有必要迁就他,所以,我用一个a*b的二维数组就完全可以表示整个地图。
有了地图以后地图里面的类容自然就有一部分是表示地雷啦,既然这样,那不如就这样<废话来的,小朋友不要学>
/**
* 20 标志该位置为地雷
* <=10的数字表示未翻开的方块及周围的地雷数目
* >=10的数字表示已翻开的方块及周围的地雷数目
* */
表示方法就出来了,逻辑也明朗起来了。
我要将某个块翻开,只要将他加上10就可以了。
Java编程第一步,当然是先要class啊
package games;
import java.util.Random;
import java.lang.Math;
class gamelogic {
/**表示一个10*10的棋盘*/
private int[][] pan = new int;
private Random random;//一个随机变量,主要作用是用来指定哪些位置为地雷
private int BombNum = 0; //统计地雷总数
/**游戏是否结束*/
private boolean GameOver;
来自:低调一贱男, 时间:2003-10-25 23:38:00, ID:2252871 | 编辑
接下来就是要初始化地图了,地图首先要扔一个雷在上面啊,不然怎么叫扫雷呢,扔完了地雷以后接下来当然是遍历一次地图(我们还是很仁慈地,我们得告诉扫雷的同志,某某位置,有多少雷,比如这样:"01、01、12点中方向有地雷,14点钟方向有幺鸡,2点钟方向有东风之类的啊")。
/**初始化数组,生成地图*/
public void InitArray() { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { pan[i][j] = 0; } }
RandomArray(); CountBomb(); BombNum = Bomb(); }
/**统计地雷总数 * @return int 返回地雷总数 */
private int Bomb() { int count = 0; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (pan[i][j] == 20) { count += 1; } } }
return count; }
/**随机决定地雷的位置*/
private void RandomArray() { int i, j, k;
// 先扔15个左右的地雷吧,注意,这里不一定有15个哦,因为随机值可能重复,我不管啦 for (int r = 0; r < 15; r++) { k = java.lang.Math.abs(random.nextInt()) % 64; //random.nextInt(100); i = k / 8; j = k % 8; this.pan[i][j] = 20; //指定该位置为地雷 }
}
/**统计棋盘上的数据*/
private void CountBomb() { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { int count = 0;
//当需要检测的单元格本身无地雷的情况下,统计周围的地雷个数 if (pan[i][j] != 20) { if ( (i - 1 >= 0) && (j - 1 >= 0)) { if (pan[i - 1][j - 1] == 20) { count += 1; //检测左上方空格是否是地雷 } }
if ( (i - 1 >= 0)) { if (pan[i - 1][j] == 20) { count += 1; //检测上方空格是否为地雷 } }
if ( (i - 1 >= 0) && (j + 1 <= 7)) { if (pan[i - 1][j + 1] == 20) { count += 1; //检测右上方是否为地雷 } }
if ( (j - 1 >= 0)) { if (pan[i][j - 1] == 20) { count += 1; //检测左边是否为地雷 } }
if ( (i >= 0) && (j + 1 <= 7)) { if (pan[i][j + 1] == 20) { count += 1; //右边 } }
if ( (j - 1 >= 0) && (i + 1 <= 7)) { if (pan[i + 1][j - 1] == 20) { count += 1; //左下 } }
if ( (i + 1 <= 7)) { if (pan[i + 1][j] == 20) { count += 1; //下 } }
if ( (j + 1 <= 7) && (i + 1 <= 7)) { if (pan[i + 1][j + 1] == 20) { count += 1; //右下 } }
pan[i][j] = count; } } } }
来自:低调一贱男,时间:2003-10-25 23:41:00, ID:2252872 | 编辑
/**检测已经被揭开的位置总和
* @return 返回被揭开的数量 */
private int countOpen() { int count = 0; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (pan[i][j] < 20 && pan[i][j] > 9) { count += 1; } } }
return count; }
/**检测是否胜利
* @return 是否胜利boolean值 */
public boolean isWin() {
// System.out.println(BombNum +""+ countOpen()); if ( (BombNum + countOpen()) == 64) { this.GameOver = true; return true; } else { return false; } }
来自:低调一贱男,时间:2003-10-25 23:42:00, ID:2252873 | 编辑
/**选中棋盘上的位置,并翻开
* @param matrix 位置 */
public void openpan(int matrix) { switch (getBomb(matrix)) { case 20: //当选中的位置为地雷,游戏结束 setGameOver(); break; case 0: isNull(matrix); //当选中的位置为空,则翻开周围的地图 break; default: this.isNotNull(matrix); //否则,翻开当前位置,并作上翻开的标记 } }
/**当选中的位置为空,则翻开周围的地图
* @param matrix 位置 */
private void isNull(int matrix) { int i, j; i = matrix / 8; j = matrix % 8;
if (pan[i][j] < 9) { pan[i][j] += 10; }
if ( (i - 1 >= 0) && (j - 1 >= 0)) { //检测左上方空格是否是空 if (pan[i - 1][j - 1] == 0) { isNull( (i - 1) * 8 + (j - 1)); } if (pan[i - 1][j - 1] < 9) { pan[i - 1][j - 1] += 10; } }
if ( (i - 1 >= 0)) { //检测上方空格是否为空 if (pan[i - 1][j] == 0) { isNull( (i - 1) * 8 + j); } if (pan[i - 1][j] < 9) { pan[i - 1][j] += 10; } }
if ( (i - 1 >= 0) && (j + 1 <= 7)) { //检测右上方是否为空 if (pan[i - 1][j + 1] == 0) { isNull( (i - 1) * 8 + (j + 1)); } if (pan[i - 1][j + 1] < 9) { pan[i - 1][j + 1] += 10; } }
if ( (j - 1 >= 0)) { //检测左边是否为空 if (pan[i][j - 1] == 0) { isNull(i * 8 + (j - 1)); } if (pan[i][j - 1] < 9) { pan[i][j - 1] += 10; } }
if ( (i >= 0) && (j + 1 <= 7)) { //右边 if (pan[i][j + 1] == 0) { isNull(i * 8 + (j + 1)); } if (pan[i][j + 1] < 9) { pan[i][j + 1] += 10; } }
if ( (j - 1 >= 0) && (i + 1 <= 7)) { //左下 if (pan[i + 1][j - 1] == 0) { isNull( (i + 1) * 8 + (j - 1)); } if (pan[i + 1][j - 1] < 9) { pan[i + 1][j - 1] += 10; } }
if ( (i + 1 <= 7)) { //下 if (pan[i + 1][j] == 0) { isNull( (i + 1) * 8 + j); } if (pan[i + 1][j] < 9) { pan[i + 1][j] += 10; } }
if ( (j + 1 <= 7) && (i + 1 <= 7)) { //右下 if (pan[i + 1][j + 1] == 0) { isNull( (i + 1) * 8 + (j + 1)); } if (pan[i + 1][j + 1] < 9) { pan[i + 1][j + 1] += 10; } } }
/**选中棋盘上的位置,并翻开当前位置
* @param matrix 位置 */
private void isNotNull(int matrix) { int i, j; i = matrix / 8; j = matrix % 8; pan[i][j] += 10; }
/**取得指定位置的数据
* @param matrix 位置
* @return int 数据 */
public int getBomb(int matrix) { int i, j; i = matrix / 8; j = matrix % 8; return this.pan[i][j]; }
/**检测游戏是否结束
* @return boolean 游戏是否结束的状态 */
public boolean isGameOver() { return GameOver; }
/**设置游戏结束 */
private void setGameOver() { GameOver = true; }
/**开新局*/
public void setNewGame() { this.GameOver = false; }
/**指定位置是否被揭开
* @param matrix 位置
* @return boolean 返回是否可以被揭开 */
public boolean isFree(int matrix) { int i, j; i = matrix / 8; j = matrix % 8; return pan[i][j] < 8 || pan[i][j] == 20; }
}
来自:低调一贱男, 时间:2003-10-25 23:46:00, ID:2252881 | 编辑
public void openpan(int matrix) 是整个程序的核心所在,他描述了所有的动作,
1、有可能你踩到屎,踩到屎当然就gameover啦
2、有可能踩到钱,有一大片空地给你玩
3、命好,没有炸死你,继续探索
来自:低调一贱男, 时间:2003-10-25 23:47:00, ID:2252882 | 编辑
呵呵,第一讲先到这里,下次讲界面的实现
来自:低调一贱男, 时间:2003-10-27 17:38:00, ID:2255425 | 编辑
今天考试,所以现在才开始第二讲,见谅,不过好像感兴趣的人不多,所以,我讲完这个例子就停了,如转载请著名 作者 刘昆
来自:yanyandt2, 时间:2003-10-27 17:45:00, ID:2255447
请问你这个东西我在PC上能实现吗?
我最近也想学java,可是我不知道能用java来做什么。
jbuilder太大,我的机器跑不动,
做网页又不喜欢,
还希望楼主能给迷茫的我指导一下,谢谢!!
来自:低调一贱男, 时间:2003-10-27 17:57:00, ID:2255484 | 编辑
用jbuilder配置j2me比较方便,几乎不用配置,用记事本也可以写,用命令行运行,但是要装j2se,j2ee,然后配置一堆环境变量,然后还要用命令行来编译,审核,运行,一个基本上会死人,这也是我不用的原因,呵呵,具体步骤你到
http://www.cnjm.net/
上去看看
来自:低调一贱男, 时间:2003-10-28 14:19:00, ID:2257384 | 编辑
呵呵,无人问津就只贴代码算了,搞到我都没有动力
来自:低调一贱男, 时间:2003-10-28 14:22:00, ID:2257393 | 编辑
//gamecCanvans.java
package games;
import java.lang.System.*;
import java.util.Random;
import java.util.Vector;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
*
标题: Canvas测试 *
描述: 游戏的界面 *
版权: 2003 *
公司: none * @author 刘昆
* @version 1.0*/
/**游戏动作类*/
class gameCanvas
extends Canvas
implements CommandListener {
/**黑*/ private static final int BLACK = 0x00000000;
/**白*/ private static final int WHITE = 0x00FFFFFF;
/**红*/ private static final int RED = 0x00FF0000;
/**蓝*/ private static final int BLUE = 0x000000FF;
/**没有移动的标志*/ private static final int NO_MOVE = -1;
/**主窗体类*/ private final control midlet;
/**逻辑类*/ private final gamelogic game;
/**退出菜单*/ private final Command exitCommand;
/**重新开始菜单*/ private final Command newGameCommand;
/**随机数*/ private final Random random = new Random();
/**屏幕宽度*/ private int screenWidth;
/**屏幕高度*/ private int screenHeight;
/**boardCellSize 正方形单元格的大小, * boardTop 棋盘top的位置, * boardLeft 棋盘left位置*/ private int boardCellSize, boardTop, boardLeft;
/**preCursorPosition 前一次光标选择的位置,cursorPosition 当前光标选择的位置*/ private int preCursorPosition, cursorPosition;
/**用于存储被标记的地雷的位置 */ private Vector BombVector = new Vector();
private boolean isRestart;
/**构造器
* @param midlet 主窗体类 */
public gameCanvas(control midlet) { this.midlet = midlet; game = new gamelogic(random); initializeBoard();
/**初始化菜单*/ exitCommand = new Command("退出", Command.EXIT, 1); newGameCommand = new Command("新游戏", Command.SCREEN, 2); addCommand(exitCommand); addCommand(newGameCommand); setCommandListener(this);
/**开始游戏*/ initialize(); }
/**添加一个地雷位置标记
*@param matrix 位置标记 */
private void addBomb(int matrix) { BombVector.addElement(Integer.toString(matrix)); }
/**删除一个地雷位置标记
*@param matrix 位置标记 */
private void delBomb(int matrix) { BombVector.removeElement(Integer.toString(matrix)); }
/**搜索该位置是否被标记
* @param matrix 位置标记
* @return boolean 该位置是否被记录,false为被记录 */
private boolean searchBomb(int matrix) { return BombVector.indexOf(Integer.toString(matrix)) == -1; //-1表示没有找到该位置的信息 }
/**初始化屏幕,取得棋盘的初始位置*/
private void initializeBoard() { screenWidth = getWidth(); //取得屏幕宽度 screenHeight = getHeight(); //取得屏幕高度 if (screenWidth > screenHeight) { boardCellSize = (screenHeight - 1) / 8; boardLeft = (screenWidth - (boardCellSize * 8)) / 2; boardTop = 1; } else { boardCellSize = (screenWidth - 1) / 8; boardLeft = 1; boardTop = (screenHeight - boardCellSize * 8) / 2; } }
/** 初始化游戏和屏幕. 使游戏重新启动*/
private void initialize() { preCursorPosition = cursorPosition = 0; game.setNewGame(); game.InitArray(); isRestart = true; BombVector.removeAllElements(); repaint(); }
/**重画canvas
* @param g 重画的Graphics对象*/
public void paint(Graphics g) { game.isWin();
if (!game.isGameOver()) { paintGame(g); } else { paintGameOver(g); } }
/**游戏未结束时的重画动作
* @param g 重画的Graphics对象*/
private void paintGame(Graphics g) { if (isRestart) { /*清除画板*/ g.setColor(0xbbbbbb); g.fillRect(0, 0, screenWidth, screenHeight); drawBoard(g); paintAll(g); //System.out.println("sss");//test drawBombTag(g); }
drawCursor(g); }
/**游戏结束时的重画动作,画出游戏统计结果
* @param g 重画的Graphics对象 */
private void paintGameOver(Graphics g) { if (game.isGameOver()) { if (game.isWin()) { GameOver(g); } else { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (game.getBomb(i * 8 + j) == 20) { drawCircle(g, i * 8 + j); } } } } } }
/**游戏结束时的重画动作
* @param g 重画的Graphics对象 */
private void GameOver(Graphics g) {
String tallyMsg = "你赢了,恭喜!"; Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,Font.SIZE_LARGE); int strHeight = font.getHeight(); int tallyMsgWidth = font.stringWidth(tallyMsg); int strWidth = tallyMsgWidth;
/*计算绘制文本的起始位置*/ int x = (screenWidth - strWidth) / 2; x = x < 0 ? 0 : x; int y = (screenHeight - 2 * strHeight) / 2; y = y < 0 ? 0 : y;
/* 清除画板*/ g.setColor(WHITE); g.fillRect(0, 0, screenWidth, screenHeight);
/* 画出文本结果*/ g.setColor(RED); g.drawString(tallyMsg, x + 5, (y + 1 + strHeight),(Graphics.TOP | Graphics.LEFT)); }
/**监听器响应接口
* @param c 命令
* @param d 消息源 */
public void commandAction(Command c, Displayable d) { if (c == exitCommand) { midlet.quit(); } else if (c == newGameCommand) { initialize(); } }
/** 画圆,表示地雷位置
* @param g 图形物件
* @param matrix 位置*/
private void drawCircle(Graphics g, int matrix) { int x, y; y = matrix / 8; x = matrix % 8; g.setColor(RED); g.fillArc(x * boardCellSize + boardLeft + 3, y * boardCellSize + boardTop + 2, boardCellSize - 2, boardCellSize - 2, 0, 360); }
/** 画叉,表示地雷爆炸
* @param g 图形物件
* @param x x坐标
* @param y y坐标 */
private void drawCross(Graphics g, int x, int y) { g.setColor(RED); g.drawLine(x + 1 + boardLeft, y + boardTop, x + boardCellSize - 4 - 4 + boardLeft, y + boardCellSize - 5 + boardTop); g.drawLine(x + 1 + boardLeft, y + boardCellSize - 5 + boardTop, x + boardCellSize - 4 - 4 + boardLeft, y + boardTop); }
/**根据玩家和电脑的移动重画棋盘*/
private void doPlayerMove() { if (game.isFree(cursorPosition)) { game.openpan(cursorPosition); } repaint(); //画出移动的图形结果 }
/**重画所有被揭开的文字
* @param g 图形物件 */
private void paintAll(Graphics g) { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (game.getBomb(i * 8 + j) >= 10 && game.getBomb(i * 8 + j) < 20) { paintNum(g, i * 8 + j); } } } }
/**画出文字
* @param g 图形物件
* @param matrix 位置 */
private void paintNum(Graphics g, int matrix) { int i, j, s; Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL); s = game.getBomb(matrix); //System.out.println(s); i = matrix / 8; j = matrix % 8; g.setColor(WHITE); if (this.searchBomb(matrix)) { if (s != 20) { g.drawString(String.valueOf(s - 10), boardLeft + j * boardCellSize + 5, boardTop + i * boardCellSize - 2, (Graphics.TOP | Graphics.LEFT)); } } }
/**捕获按键消息
* @param keyCode 按键代码 */
protected void keyPressed(int keyCode) { /**当游戏结束时返回*/ if (game.isGameOver()) { return; }
int gameAction = getGameAction(keyCode);
switch (gameAction) { case FIRE: if (searchBomb(cursorPosition)) { //如果该位置被做出了地雷标志,则该位置不能被揭开 doPlayerMove(); } break; case RIGHT: doMoveCursor(1, 0); break; case DOWN: doMoveCursor(0, 1); break; case LEFT: doMoveCursor( -1, 0); break; case UP: doMoveCursor(0, -1); break; default: if (searchBomb(cursorPosition)) { addBomb(cursorPosition); } else { delBomb(cursorPosition); } repaint(); break; } }
/**画出棋盘上的地雷标志
* @param g 图形物件 */
private void drawBombTag(Graphics g) { int s, j, k; g.setColor(RED); for (int i = 0; i < BombVector.size(); i++) { s = Integer.parseInt( (String) BombVector.elementAt(i)); j = s % 8; k = s / 8; g.drawString("!", boardLeft + j * boardCellSize + 7, boardTop + k * boardCellSize - 2, (Graphics.TOP | Graphics.LEFT)); } }
/**指定棋盘上的坐标
* @param dx 左右偏移量x
* @param dy 上下偏移量y */
private void doMoveCursor(int dx, int dy) {
int newCursorPosition = cursorPosition + dx + 8 * dy; if ( (newCursorPosition >= 0) && (newCursorPosition < 64)) { preCursorPosition = cursorPosition; cursorPosition = newCursorPosition; repaint(); } }
/** 在棋盘上画出选择光标
* @param g 图形物件 */
private void drawCursor(Graphics g) {
/** 清除之前的选择光标*/ g.setColor(0xbbbbbb); g.drawRect( ( (preCursorPosition % 8) * boardCellSize) + 3 + boardLeft, ( (preCursorPosition / 8) * boardCellSize) + 1 + boardTop, boardCellSize - 2, boardCellSize - 2);
/**在当前选择位置画出选择光标*/ g.setColor(this.BLUE); g.drawRect( ( (cursorPosition % 8) * boardCellSize) + 3 + boardLeft, ( (cursorPosition / 8) * boardCellSize) + 1 + boardTop, boardCellSize - 2, boardCellSize - 2); }
/** 画出空位置
* @param g 图形物件 */
private void drawNull(Graphics g) {
/**在当前选择位置画出选择光标*/ g.setColor(this.WHITE); g.fillRect( ( (cursorPosition % 8) * boardCellSize) + 3 + boardLeft, ( (cursorPosition / 8) * boardCellSize) + 1 + boardTop, boardCellSize - 2, boardCellSize - 2); }
/**画一个盘
* @param g 图形物件 */
private void drawBoard(Graphics g) {
/** 清除盘*/ g.setColor(0xbbbbbb); g.fillRect(0, 0, screenWidth, screenHeight);
/**画盘*/ g.setColor(BLACK); for (int i = 0; i < 9; i++) { g.fillRect(boardLeft + 2, boardCellSize * i + boardTop, (boardCellSize * 8) + 1, 1); /*画四条黑色的横线*/ g.fillRect(boardCellSize * i + boardLeft + 2, boardTop, 1, boardCellSize * 8); /*画四条黑色的竖线*/ } } }
来自:低调一贱男, 时间:2003-10-28 14:23:00, ID:2257395 | 编辑
package games;
import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
public class control
extends MIDlet {
private gameCanvas gameScreen;
/**构造器*/ public control() { }
/**开始MIDlet*/ public void startApp() { Displayable current = Display.getDisplay(this).getCurrent(); if (current == null) { Image logo = null; try { logo = Image.createImage("/icons/mine.png"); } catch (IOException e) { System.err.println("none image"); }
Alert splashScreen = new Alert(null,
" 扫雷 1.0 \n 刘昆制作 \n eMail:[email protected]", logo,
AlertType.INFO); //显示一个logo splashScreen.setTimeout(4000);
gameScreen = new gameCanvas(this);
Display.getDisplay(this).setCurrent(splashScreen, gameScreen); } else { Display.getDisplay(this).setCurrent(current); } }
/*暂停MIDlet*/
public void pauseApp() {}
/**销毁MIDlet中需要销毁的对象
*@param unconditional 是否被立即销毁 */
public void destroyApp(boolean unconditional) {}
/**退出MIDlet*/
public void quit() { destroyApp(false); notifyDestroyed(); }
} 
|
|
相关文章:相关软件: |
|