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

WAP之家技术文章J2ME技术程序开发MIDP1.0游戏完整实现-双人扫雷1.0(二)

MIDP1.0游戏完整实现-双人扫雷1.0(二)
作者:yinowl  来源:J2MEDV  发布时间:2005-9-7 13:00:04

这里会使用到几个方法
checkWin()方法:我们把这个方法的调用放在玩家挖到雷的时候,每当有玩家挖到雷的时候,会改变player1Found和player2Found的值,这时候检测游戏的输赢,当有玩家的分数大于半数的总雷数的时候,胜负就得出了,这时候用Alert对象显示出比分和胜负信息,同时要为下一局比赛作准备---游戏初始化(gameInit())。
gameInit()方法:这个方法用来初始化游戏,程序中在两种情况需要调用初始化游戏,一个是重新开始游戏时,另一个是在一局结束后准备另一局的开始。每一次需要初始化游戏时,需要将所有的变量初始化
bombInit()方法:初始化表示整个雷区的Bomb型的二维表,病随机的安排52颗雷的位置
bombOut()方法:经典扫雷中,如果我们点到一雷位,本身不是雷,周围也没有雷,电脑会自己打开周围所有相连的类似的雷和这块区域最外圈的一圈雷位,这个方法就是这个作用。我在这里用了递归,当然我们只需要监测fasFound值为false的雷位,要不然就会都搜一遍

在MiningCanvas.java中添加如下代码
public void gameInit(){
    sbWon=false;
    bombInit();
    player1Found=0;player2Found=0;
    selectedX=miningMapGrid/4;selectedY=miningMapGrid/4;
    bombLeft=bombNum;
    System.gc();//手动进行垃圾回收
}
public void bombInit(){
    int bombindex=0;
    int x=0,y=0;
    Random random=new Random();
    for(int i=0;i<miningMapGrid+2;i++){
        for(int j=0;j<miningMapGrid+2;j++){
            bombs[i][j]=new Bomb();
        }
    }
    while(bombindex<bombNum){
        x=Math.abs(random.nextInt()%16+1);
        y=Math.abs(random.nextInt()%16+1);
        if(!bombs[x][y].isBomb && x<=16 && x>=1 && y<=16 && y>=1){
            try{
                bombs[x][y].isBomb=true;
                bombindex++;
                bombs[x-1][y].bombaround++;
                bombs[x-1][y-1].bombaround++;
                bombs[x][y-1].bombaround++;
                bombs[x+1][y-1].bombaround++;
                bombs[x+1][y].bombaround++;
                bombs[x+1][y+1].bombaround++;
                bombs[x][y+1].bombaround++;
                bombs[x-1][y+1].bombaround++;
            }catch(ArrayIndexOutOfBoundsException e){}
        }
    }
}
public void bombOut(int x,int y){
    if(bombs[x][y].hasFound==false && x>=1 && y>=1 && 
            x<=MiningCanvas.miningMapGrid && y<=MiningCanvas.miningMapGrid){
        bombs[x][y].hasFound=true;
        if(bombs[x][y].bombaround==0){
            this.bombOut(x-1,y);
            this.bombOut(x-1,y-1);
            this.bombOut(x,y-1);
            this.bombOut(x+1,y-1);
            this.bombOut(x+1,y);
            this.bombOut(x+1,y+1);
            this.bombOut(x,y+1);
            this.bombOut(x-1,y+1);
        }
    }
}
public void checkWin(){
    if(player1Found>26){
        winString="Player1 has won!";
        sbWon=true;
    }
    if(player2Found>26){
        winString="Player2 has won!";
        sbWon=true;
    }
    if(sbWon){
        winAlert=new Alert("",winString+"\nPlayer1 "+player1Found
                    +" : "+player2Found+" player2",null,AlertType.INFO);
        gameInit();
        winAlert.setTimeout(Alert.FOREVER);
        MiningMIDlet.display.setCurrent(winAlert);
    }
}

游戏时菜单
游戏进行时,我们需要有几个功能:重新开始游戏,打开帮助,返回主菜单,退出游戏,当然,进入了菜单就还要能返回正在进行的游戏。这个游戏时菜单和主菜单非常相似,我就不详细解释了,只是这里有一个缺陷,游戏时菜单中选择帮助后,在帮助界面中选择返回,会回到主菜单,而不会回到这个游戏时菜单,当然,加一个变量就可以解决这个问题,原代码如下:

在MiningCanvas.java中添加如下代码
static final int GAME_RETURN = 0;
static final int GAME_RESTART = 1;
static final int GAME_HELP = 2;
static final int GAME_MENU = 3;
static final int GAME_EXIT = 4;
static final int GAME_MENU_ITEM_COUNT = 5;
static String[] gameMenu = new String[GAME_MENU_ITEM_COUNT];
static int gamemenuIdx;
public MiningCanvas(MiningMIDlet miningMIDlet){
    ...
    gamemenuIdx=0;
    gameMenu[0] = "RETURN";
    gameMenu[1] = "RESTART";
    gameMenu[2] = "HELP";
    gameMenu[3] = "MENU";
    gameMenu[4] = "EXIT";
}
private void paintGameMenuScreen(Graphics g){
    for(int i=0;i<gameMenu.length;i++){
        if(i==gamemenuIdx){
            g.setColor(highBGColor);
            g.fillRect(0,startHeight+i*(lowFont.getHeight()+spacing)
                    -(highFont.getHeight()-lowFont.getHeight())/2,
                    canvasW,highFont.getHeight());
            g.setFont(highFont);
            g.setColor(highColor);
            g.drawString(gameMenu[i],(canvasW-highFont.stringWidth(gameMenu[i]))/2,
                    startHeight+i*(lowFont.getHeight()+spacing)-(highFont.getHeight()
                    -lowFont.getHeight())/2,Graphics.TOP|Graphics.LEFT);
        } else {
            g.setFont(lowFont);
            g.setColor(lowColor);
            g.drawString(gameMenu[i],(canvasW-lowFont.stringWidth(gameMenu[i]))/2,
                    startHeight+i*(lowFont.getHeight()+spacing),Graphics.TOP|Graphics.LEFT);
        } 
    }
}
在keyPressed方法中的switch结构中添加
case GAMESTATE_GAMEMENU:
{
    if (getGameAction(keyCode) == FullCanvas.UP && gamemenuIdx - 1 >= 0) {
        gamemenuIdx--;
    }
    else if (getGameAction(keyCode) == FullCanvas.DOWN && gamemenuIdx + 1 < gameMenu.length) {
        gamemenuIdx++;
    }
    else if (getGameAction(keyCode) == FullCanvas.FIRE) {
        switch(gamemenuIdx) {
            case GAME_RETURN:
                gamestate=GAMESTATE_GAMEING;
                break;
            case GAME_RESTART:
                gameInit();
                gamestate=GAMESTATE_GAMEING;
                break;
            case GAME_HELP:
                gamestate=GAMESTATE_HELP;
                break;
            case GAME_MENU:
                gamestate=GAMESTATE_MENU;
                break;
            case GAME_EXIT:
                miningMIDlet.destroyApp(false);
                break;
        }
    }
    break;
}

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

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

用户名: 查看更多评论

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

内 容:

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