手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>程序设计>Java技术>列表

J2ME-MIDP1.0游戏完整实现-双人扫雷1.0(二)

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!


在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() 1);
y=Math.abs(random.nextInt() 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 ;

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!