J2ME-MIDP1.0 complete realization of the game - Double mine 1.0 (b)

  Abstract: J2ME-MIDP1.0 complete realization of the game - Double mine 1.0 (b) 

  Author: yinowl 
  February 2005 

  Help interface 
  Help interface is very simple, the text needs a good row width in a String array, and then drawing on the screen, if a resort on the screen button response to Fanbing increase is only to draw in front of or behind the array Several value, the source code is as follows: 

  <table Width=550 bgColor=#e7e7e7 border=0> <tr> <td width=1150 height=457> 

  In MiningCanvas.java add the following code strGamehelp final String [] = new String [10]; public MiningCanvas (MiningMIDlet miningMIDlet) (… strGamehelp [0] = "This game is the game for Double Versus"; strGamehelp [1] = ", and the classic mine different," strGamehelp [2] = "game, we have to dig out"; strGamehelp [3] = "Mine, mine was dug wrong exchange players."; strGamehelp [4] = "A total of 52 mines, 256 grid"; strGamehelp [5] = "-was first dug 27 minefields"; strGamehelp [6] = "win .1. upper and lower, left and right points "strGamehelp [7] =" other key figures for 2,8,4,6, digging "; strGamehelp [8] =" Lei 5, 2. screen frame Yen "; strGamehelp [9] =" for the color Under the current audience ";) private void paintHelpScreen (Graphics g) (g.setColor (0×00FFFFFF); g.fillRect (0,0, canvasW, canvasH); g.setFont (lowFont); g.setColor (0×00000000); for (int i = 0; i 

g.drawString(strGamehelp[i],5,5+(lowFont.getHeight()+3)*i,Graphics.TOP|Graphics.LEFT);

}

}

  KeyPressed method in the structure of the switch in the case GAMESTATE_HELP added: / / If the current state of the game to help the state, according to players that no matter which button Jump to the main menu will state gamestate = GAMESTATE_MENU; break;    </ Td> </ tr> </ table> 

  The main game interface mapping interface divided into several parts, is a minefield drawing board, according to the median of mined areas in each group Bomb targets in the variable values charted the entire board, if hasFound value of false, draw If hasFound isBomb value of true and false values, then this will be drawn around the mine-mine if a few ,,,,,,; hasFound isBomb value for the true value and true, then this will draw in which mine one player out, and were.    The second part is the choice of frame audience.    The third part of the game information box, which is currently two players score, and left few mines, information box for the picture.    The last part was to remind the audience turn which cleared minefields in the frame with the color tips audience, and draw players in the information box, and the banner. 
  Here are some attention: 
  1. Mined areas is not entirely in pictures on the screen, it will be rolling through the reel.    The entire area is 16 x16, we prepared to draw the screen 10 x12, two variables (paintX and paintY) to control the mined areas to draw the coordinates of the upper left corner of the mine, such as the value of these two variables were 3 and 4, it will draw horizontal 3-10, 4-13 longitudinal axis of minefields, the coordinates of the selection box (selectedX and selectedY) not to worry, when he exceeded the screen, only the need to increase or decrease the value of paintX and that paintY can be, as long as the guarantee that the selection box coordinates and paintX / Y coordinates worth distance not exceeding 9 (10-1) and 11 (12-1) can be.    We should be very careful here, we here all the coordinates and mined areas of the median group bombs in the coordinates of the opposite, that is, the selection box (5,6) is the type of bombs [6] [5]. 
  2. Bomb in the creation of two-dimensional array of mined areas, the median size of the group is 18 x18 (new Bomb [+2] [miningMapGrid miningMapGrid +2]), is aimed at the real size 16 x16 minefields mine-plus lap , in order to avoid bombOut () method and other needs of the surrounding mine search beyond the border will not be abnormal. 
  3. Bombs because of the array indexing 1-16 in the mined areas is the real representative of coordinates (1,1) to (Investigation of) mine at Therefore, in the whole process, all relevant coordinates and the array index values Local be very careful not to appear wrong location 
  4. News box needs several numerical mapping the coordinates of variables (Player1X / Y, Player2X / Y, bombnowX / Y) values are relative to the upper left corner of the message box of the relative coordinates, we can also frame for the news Coordinate separate definition of a variable, so even clearer. 
  Source code as follows: 

  <table Width=671 bgColor=#e7e7e7 border=0> <tr> <td width=665 height=2979> 

  In MiningCanvas.java add the following code int empty; static final int miningMapGrid = 16; / / 16 minefields lattice grid x16 static final int bombNum = 52; / / A total of 52 minefields int miningMapLength, miningGridLength; / / entire minefields The edge of each mine at the edge of lattice int miningMapX, miningMapY; / / mined areas of the upper left corner coordinates int selectedX, selectedY; / / coordinates the selection box, in the mined areas in the coordinates (1-16) int player1Found, player2Found; / / score of the two players, which have found a few minefields int paintX, paintY; / / the region to draw the upper left corner coordinates, in the mined areas in the coordinates (1-16) int bombLeft; / / So the rest of the mine a few static final int Player1X = 10, Player1Y = 30; / / draw players an information box scores coordinate position relative to the upper left corner box information static final int Player2X = 10, Player2Y = 90; / / information box scores of the two players draw coordinates position relative to the upper left corner box information static final int bombnowX = 8, bombnowY = 52; / / information box draw the coordinates of the remaining mine location, Information relative to the upper left corner box static final int bombMapW = 10, bombMapH = 12; / / can be drawn on the screen of the number of mine-Bomb [] [] bombs; boolean isPlayer1; / / is the turn of the current players a mine Alert winAlert; / / Alert information displayed win object String winString; / / after winning or losing than the output of information boolean sbWon; / / draw whether winning or losing static final Font font = Font.getFont (Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font . SIZE_SMALL); / / information box in the script used by Image infoImg, splashImage; Image unFoundGroundImg; Image foundGroundImg; Image player1BombImg, player2BombImg, player1TurnImg, player2TurnImg; Image bomb1Img, bomb2Img, bomb3Img, bomb4Img, bomb5Img, bomb6Img, bomb7Img, bomb8Img; / / All the picture to use public MiningCanvas (MiningMIDlet miningMIDlet) (… try (/ / examples of the game needs Image object unFoundGroundImg = Image.createImage ( "/ unfoundGroundbig.png"); foundGroundImg = Image.createImage ( " / foundGroundbig.png "); player1BombImg = Image.createImage (" / player1bombbig.png "); player2BombImg = Image.createImage (" / player2bombbig.png "); bomb1Img = Image.createImage (" / bomb1big.png "); bomb2Img = Image.createImage ( "/ bomb2big.png"); bomb3Img = Image.createImage ( "/ bomb3big.png"); bomb4Img = Image.createImage ( "/ bomb4big.png"); bomb5Img = Image.createImage ( "/ bomb5big . png "); bomb6Img = Image.createImage (" / bomb6big.png "); infoImg = Image.createImage (" / info.png "); splashImage = Image.createImage (" / occo.png "); player1TurnImg = Image . createImage ( "/ player1turn.png"); player2TurnImg = Image.createImage ( "/ player2turn.png");) catch (IOException e) () isPlayer1 = true; / / initialization sequence miningGridLength audience = 14; / / initialization of the mine each side miningMapLength = 14 * 12; / / The whole side of the chessboard mine miningMapX = (canvasW-miningMapLength) / 2 / / upper left corner of the chessboard on the screen coordinates miningMapY X = (canvasH - miningMapLength) / 2 / / upper left corner of the chessboard on the screen coordinates selectedX = Y = miningMapGrid selectedY / 2 / / initialize the selection box coordinates player1Found = player2Found = 0; / / initialization of the two players paintY scoring paintX = = 3; / / initialize the beginning of a minefield in the area displayed on the screen sbWon = false; / / initialization no player won bombLeft = bombNum; / / initialization for the few remaining mines the total number of mine-bombs = new Bomb [+2] [miningMapGrid miningMapGrid +2];) bombInit ();// initialization minefields private void paintGameScreen (Graphics g) (paintPlayer (g, isPlayer1); paintMiningMap (g); paintInfo (g); paintSelected (g);) protected void paintInfo (Graphics g) (g.drawImage (infoImg, miningMapX + bombMapW * miningGridLength +1, miningMapY, Graphics.TOP | Graphics.LEFT); g.setFont (font) ; g.setColor (0×00FFFFFF); g.drawString (String.valueOf (bombLeft), bombMapW miningMapX + + * miningGridLength bombnowX, miningMapY + bombnowY, Graphics.TOP | Graphics.LEFT); g.drawString (String.valueOf (player1Found) , bombMapW miningMapX + + * miningGridLength Player1X, miningMapY + Player1Y, Graphics.TOP | Graphics.LEFT); g.drawString (String.valueOf (player2Found), bombMapW miningMapX + + * miningGridLength Player2X, miningMapY + Player2Y, Graphics.TOP | Graphics . LEFT); / / This method is the following code in the message box to draw on the map, which is displayed on the screen of mines in the entire region in the location of minefields g.setColor (0×00777777); g.fillRect (* miningGridLength bombMapW miningMapX + +1, miningMapY +8 * miningGridLength +1, 2 * miningGridLength-2, 2 * miningGridLength-1); g.setColor (0×00000000); g.drawRect (miningMapX + bombMapW * miningGridLength +1, miningMapY +8 * miningGridLength +1, 2 * miningGridLength-2, 2 * miningGridLength-1); g.setColor (0×00BBBBBB); g.fillRect (miningMapX + bombMapW miningGridLength +4 +2 * * (paintX-1), miningMapY +8 * miningGridLength +4 +2 * (paintY-1), 12, 17); g.setColor (0×00FFFFFF); g.drawRect (miningMapX + bombMapW miningGridLength +4 +2 * * (paintX-1), miningMapY +8 * miningGridLength +4 +2 * (paintY-1), 12, 17);) protected void paintPlayer (Graphics g, boolean isPlayer1) (if (isPlayer1) / / draw players in the chessboard of the external color frame g.setColor (0×000000FF ); else g.setColor (0×00FF0000) for (int i = 1; i <= 5; i + +) (g.drawRect (miningMapX-i, i-miningMapY, miningMapLength +2 * i, miningMapLength * i +2); ) if (isPlayer1) / / draw in the information box on behalf of the players banner g.drawImage (player1TurnImg, miningMapX + (bombMapW +1) * miningGridLength +1, miningMapY +11 miningGridLength, Graphics.HCENTER | Graphics.VCENTER); else g.drawImage (player2TurnImg, miningMapX + (bombMapW +1) * miningGridLength +1, miningMapY +11 miningGridLength, Graphics.HCENTER | Graphics.VCENTER);) public void paintMiningMap (Graphics g) (for (int i = 0; i 

for(int j=0;j

if(!bombs[i+paintY+1][j+paintX+1].hasFound){

g.drawImage(unFoundGroundImg,miningMapX+j*miningGridLength,

miningMapY+i*miningGridLength,Graphics.TOP|Graphics.LEFT);

}

else {

if(!bombs[i+paintY+1][j+paintX+1].isBomb){

switch(bombs[i+paintY+1][j+paintX+1].bombaround){

case 0:

g.drawImage(foundGroundImg,miningMapX+j*miningGridLength,

miningMapY+i*miningGridLength,Graphics.TOP|Graphics.LEFT);

break;

case 1:

g.drawImage(bomb1Img,miningMapX+j*miningGridLength,

miningMapY+i*miningGridLength,Graphics.TOP|Graphics.LEFT);

break;

case 2:

g.drawImage(bomb2Img,miningMapX+j*miningGridLength,

miningMapY+i*miningGridLength,Graphics.TOP|Graphics.LEFT);

break;

case 3:

g.drawImage(bomb3Img,miningMapX+j*miningGridLength,

miningMapY+i*miningGridLength,Graphics.TOP|Graphics.LEFT);

break;

case 4:

g.drawImage(bomb4Img,miningMapX+j*miningGridLength,

miningMapY+i*miningGridLength,Graphics.TOP|Graphics.LEFT);

break;

case 5:

g.drawImage(bomb5Img,miningMapX+j*miningGridLength,

miningMapY+i*miningGridLength,Graphics.TOP|Graphics.LEFT);

break;

case 6:

g.drawImage(bomb6Img,miningMapX+j*miningGridLength,

miningMapY+i*miningGridLength,Graphics.TOP|Graphics.LEFT);

break;

default:

g.drawImage(foundGroundImg,miningMapX+j*miningGridLength,

miningMapY+i*miningGridLength,Graphics.TOP|Graphics.LEFT);

break;

}

}

else {

if(bombs[i+paintY+1][j+paintX+1].isPlayer1){

g.drawImage(player1BombImg,miningMapX+j*miningGridLength,

miningMapY+i*miningGridLength,Graphics.TOP|Graphics.LEFT);

}

else{

g.drawImage(player2BombImg,miningMapX+j*miningGridLength,

miningMapY+i*miningGridLength,Graphics.TOP|Graphics.LEFT);

}

}

}

}

}

}

public void paintSelected(Graphics g){

g.setColor(0×00FF0000);//画出选择框,注意其与总雷区和能在屏幕上显示出的区域间的关系

g.drawRoundRect(miningMapX+(selectedX-paintX)*miningGridLength-1,

miningMapY+(selectedY-paintY)*miningGridLength-1,

miningGridLength+1,miningGridLength+1,2,2);

}

  KeyPressed method in the structure of the switch in the case GAMESTATE_GAMEING added: (if (keyCode == FullCanvas.KEY_SOFTKEY1) = (gamestate GAMESTATE_GAMEMENU;) else if (action == FullCanvas.LEFT) (selectedX = (- selectedX + miningMapGrid)% ( miningMapGrid);) else if (action == FullCanvas.RIGHT) (selectedX = (+ + selectedX)% (miningMapGrid);) else if (action == FullCanvas.UP) (selectedY = (- selectedY + miningMapGrid)% ( miningMapGrid);) else if (action == FullCanvas.DOWN) (selectedY = (+ + selectedY)% (miningMapGrid);) else if (action == FullCanvas.FIRE) (if (! bombs [+1] [selectedX selectedY +1]. hasFound) (if (bombs [+1] [selectedY selectedX +1]. isBomb) (bombs [+1] [selectedY selectedX +1]. hasFound = true; bombs [+1] [selectedY selectedX +1 ]. isPlayer1 = this.isPlayer1; if (isPlayer1) player1Found + +; else player2Found + +; bombLeft -; checkWin ();// Whenever players dug on the monitoring of the mine-whether it wins) else (bombOut (selectedY +1, selectedX +1) / / If this mine has no place and the surrounding mines, connected to open all its minefields in the same situation in this area and the surrounding circle of mine isPlayer1 =! isPlayer1;))) / / the following lines of code to adjust displayed on the screen of the coordinates of the area to avoid the selection box outside the scope of the screen if ((selectedX-paintX) <0) = selectedX paintX; else if ((selectedX-paintX)> = bombMapW)-bombMapW selectedX paintX = +1; if ((selectedY-paintY) <0) = selectedY paintY; else if ((selectedY-paintY)> = bombMapH)-bombMapH selectedY paintY = +1; break;)    </ Td> </ tr> </ table> 

  Here will use several methods 
  CheckWin () method: This method we call on the players dug mine when the players dug mine whenever there is a time and will change player1Found and player2Found the value of this time of winning or losing the game, when a play home scores than half of the total number of mine, came to the victory, this time with Alert objects show the doubles victory information at the same time, to make preparations for the next game - the game initialization ( gameInit ()). 
  GameInit (): Use this method to initialize the game, proceedings in both cases need to call initialization games, a re-start the game, and the other is the end of one another for the start.    Each time necessary to initialize games, it needs all the initialization of variables 
  BombInit () method: the initialization said that the entire area of the two-dimensional Table Bomb, random arrangement of 52 mine position 
  BombOut () method: classic mine, if we point to one of mine, is not mine, not mine around, the computer will be connected to all the surrounding open their own similar mines and the outer ring of this region's most mine-lap, This is the role of this method.    Here I spent a recursive, of course, we only need to monitor fasFound false value of the mine, or else would have found it again 

  <table Width=550 bgColor=#e7e7e7 border=0> <tr> <td width=582 height=938> 

  In MiningCanvas.java add the following code public void gameInit () (sbWon = false; bombInit (); player1Found = 0; player2Found = 0; selectedX = miningMapGrid / 4; selectedY = miningMapGrid / 4; bombLeft = bombNum; System.gc ( ); / / manually recycling) public void bombInit () (int bombindex = 0; int x = 0, y = 0; = new Random Random random (); for (int i = 0; i 

for(int j=0;j

bombs[i][j]=new Bomb();

}

}

while(bombindex

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] [y-1 -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);))    </ Td> </ tr> </ table> 

  Menu in the game during the game, we need to have several functions: re-start the game, opening up help to return to the main menu, from the game, of course, entered on the menu are also able to return to the game.    The game menu and the main menu is very similar, I do not explained in detail, but there is one flaw, the game menu and select Help, help in choosing to return to the interface will return to the main menu, and will not return to the game menu, of course, plus a variable can solve this problem, the original code is as follows: 

  <table Width=550 bgColor=#e7e7e7 border=0> <tr> <td width=1150 height=1016> 

  In MiningCanvas.java add the following code 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; gameMenu static String [] = 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 

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 method in the structure of the switch in the case GAMESTATE_GAMEMENU added: (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;)    </ Td> </ tr> </ table> 

  The ultimate complement to the code here, in accordance with the game components, the main code basically given in a little to add some code is the integrity of the entire game procedures, the need to be supplemented as follows: 

  <table Width=550 bgColor=#e7e7e7 border=0> <tr> <td width=1150 height=132> 

  Laws keyPressed method switch in the structure of the switch (gamestate) (… default: gamestate = GAMESTATE_MENU;) repaint ();    </ Td> </ tr> </ table> 

  Summed up the entire game has been fully completed, we will certainly feel it is very simple, but is a complete game, I hope to help some friends.    Of course, we can carry out some expansion, for example, the voice of a battlefield with Bluetooth capabilities, the game gradually improved, and has commercial value. 
  The article also slowly wrote here almost the end, suddenly found entire article, I completely deviated from the original forecast of ways and effect, I intend to start the game originally envisaged, to the design, coding, debugging the whole process to the test article written The most important thing is the idea of mind, including the beginning of the wrong, imperfect, and later amended, so as to the real game I do the whole process and experience tell you.    Unexpectedly wrote last or no effect, I hope that this is only the experience of writing articles, and the next will try to improve, we could all be more understanding. 
  The finished article inevitably there will be some mistakes, and each one finished, and can not wait to always please everyone Tieshanglai guidance, although each told their own restraint in check see if there is any mistake, they would occasionally lazy to , huh.    As a "J2ME-MIDP1.0 achieve complete game - Double mine (1)" a few days ago on Tieshanglai, look at the code of a game can not be half half it out, actually had written half of this, the original and intends to add an interface variables marked map and a game thinking process, to write a postscript bar, the way the original code Tieshanglai finishing a package together. 
  Finally we would like to learn J smooth out more exchanges (MSN: yinowl@hotmail.com QQ: 47599318 E-mail: yinowl@163.com). 

  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: ,

Releated Java Articles

Comments

Leave a Reply