J2ME-MIDP1.0 game full implementation - Double mine 1.0 (2)

xiaoxiao2021-03-06  37

Author: yinowl February 2005

The help interface helps the interface is simple, put the width of the required text in a string array, then draw on the screen, if a screen can't put it, add the button response to turn the screen, actually to re-draw an array in front or behind Several values, the source code is as follows:

Add the following code in MiningCanvas.java

Final string [] strgamehelp = new string [10];

Public miningcanvas (Miningmidlet miningmidlet) {

...

STRGAMEHELP [0] = "This game is a double battle game";

STRGAMEHELP [1] = ", and the classic mine clearance, this";

STRGAMEHELP [2] = "In a game, we have to dig";

STRGAMEHELP [3] = "Thunder, exchanging the player."

STRGAMEHELP [4] = "A total of 52 mine, 256 gscare;

STRGAMEHELP [5] = "Sub, the first digging to 27th";

STRGAMEHELP [6] = "Winning. 1. upper, lower, left, right";

STRGAMEHELP [7] = "Don't be numeric keys 2, 8, 4, 6, digging";

STRGAMEHELP [8] = "Lei 5; 2. The color of the screen frame";

STRGAMEHELP [9] = "Color is the current player;";

}

Private void Painthelpscreen (graphics g) {

G.SetColor (0x00FFFFF);

G.FillRect (0, 0, canvasw, canvash);

g.setfont (lowfont);

g.setcolor (0x00000000);

For (int i = 0; i

g.drawstring (strgameHelp [i], 5, 5 (lowfont.getHeight () 3) * i, graphics.top | graphics.Left);

}

}

Add from the Switch structure in the KeyPressed method

Case gameState_help: // If the game is now in the help status, the player will jump to the main menu no matter which key is pressed.

Gamestate = Gamestate_Menu;

Break;

The drawing of the main interface of the game main interface is divided into several parts, one is the drawing of the mine board, according to the variable value in each BOMB object in the two-digit group, if the HASFOUND value is false, draw; If the HASFOUND value is true and the ISBOMB value is false, then the mine surrounding this lightning is drawn ,,,,,, if the HASFOUND value is True and the ISBOMB value is True, which one will draw this lightite? The players are digging, respectively. The second part is the player's selection box. The third part is the information box of the game, that is, the current score of the two players, there are still few thunders, the picture of the information box is. The last part is to prompt the current round to which player mine, in the outfit of the mine area, use the player's color prompt, and draw the player's banner and in the information box. Here are a few points here: 1. The mine area cannot be painted in the screen, so it will scroll through the reel. The entire mine area is 16x16, our screen preparatory 10x12, with two variables (Paintx and Painty) to control the mine coordinates of the upper left corner of the thunder area to be drawn, for example, the values ​​of these two variables are 3 and 4, then draw the horizontal axis 3-10, the longitudinal axis 4-13 light, select the coordinates of the box (SelectEx and Selecty) don't have to worry, when he exceeds the screen, only need to increase or reduce the value of PainTX and PaintY However, as long as the coordinates of the selection box and the PAINTX / Y coordinate are not exceeding 9 (10-1) and 11 (12-1). It is very much care here that all coordinate values ​​in our coordinate values ​​and the two array bombs in the mine are the opposite of the coordinates, that is, the selection box (5, 6) is a class Bombs [6] [5]. 2. When creating a two-dimensional BMB array array of mine-zone, the size of the two arrays is 18x18 (New Bomb [MiningmapGrid 2] [MiningmapGrid 2]), the purpose is to add a lap in the real mine area of ​​the size 16x16, Avoiding a Bombout () method and other we need to survey the boundary exception when you search the lights. 3. Because the index 1-16 in the BMMBS array is the real representative of the mine sitting in the mine area (1, 1) to (16, 16), so in the entire program, all places regarding coordinates and array index values To be very careful, do not appear in a position error 4. The value of the coordinate variables (Player1x / Y, Player2x / Y, Bombnowx / Y) that needs to be drawn are all relative coordinates of the upper left corner of the message box. We can also define a pair of variables separately for the coordinates of the message box, which is clearer. The source code is as follows: add the following code to MiningCanvas.java

Int empty;

Static final int miningmapgrid = 16; // mine is 16 X16 grid

Static Final Int Bombnum = 52; // A total of 52 thunders

Int miningmaplength, mininggridens; // The side of the entire mine area, each lightning lattice

INT miningmapx, miningmapy; // mine-left upper corner coordinate

INT selectedX, selectedy; // Select the coordinates of the box, is the coordinate in the mine (1-16)

INT Player1Found, Player2Found; // The score of two players, that is, the mine found

INT PAINTX, PAINTY; / / The upper left corner coordinates of the area to be drawn is the coordinates (1-16) int bombleft; // remain unmesed out of the mine

Static Final Int Player1x = 10, Player1Y = 30; // Draw a score of the player in the information box, relative to the left corner of the information frame

Static final int player2x = 10, player2y = 90; // Draw the player's two-point coordinate position in the information box, relative to the left upper left corner

Static final int bombnow = 8, bombnowy = 52; // Draw the remaining mine coordinate position, relative to the left corner of the information frame

Static final int bombmapw = 10, bombmaph = 12; // The number of lightning bits can be painted on the screen

Bomb [] [] Bombs;

Boolean isplayer1; // Whether it is currently turned to the player

Alert Winalrt; / / Show a Alert object for winning information

String Winstring; // Information to output after winning

Boolean sbwon; // Do you get lost to lose?

Static final font font = font.getfont (font.face_monospace, font.style_plain, font.size_small);

/ / The font used in the message box

Image infoimg, splashimage;

Image unfoundgroundImg;

Image FoundgroundImg;

Image Player1bombimg, Player2bombimg, Player1Turnimg, Player2Turnimg;

Image Bomb1Img, Bomb2Img, Bomb3Img, Bomb4img, Bomb5img, Bomb6Img, Bomb7Img, Bomb8Img;

// All the images you want to use

Public miningcanvas (Miningmidlet miningmidlet) {

...

Try {// Instantiate the image object you need in the game

Unfoundgroundimg = image.createImage ("/ infoundgroundbig.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; // Initialize the player's order

MiningGridLength = 14; // Initialize the edge length of each light

Miningmaplength = 14 * 12; // The side length of the entire mines

MiningMapx = (canvasw-miningmaplength) / 2; // screen on the upper left corner of the screen on the screen

MiningMapy = (canvash-miningmaplength) / 2; // On the top left corner of the on the upper left corner of the screen

SelectedX = SELECTEDY = MiningmapGrid / 2; // Initialize the coordinates of the selection box

Player1Found = Player2Found = 0; // Initialize the score of two players

Paintx = PaintY = 3; // Initialize the range of the entire mine area to display on the screen

SBWON = false; // Initialization No players win

Bombleft = Bombnum; // Initialization The remaining mine is the total mine

Bombs = new bomb [miningmapgrid 2] [MiningMapGrid 2];

Bombinit (); // Initialization

}

Private void PaintGamescreen (graphics g) {

PaintPlayer (g, isplayer1);

PaintminingMap (g);

Paintinfo (g);

PaintSelected (g);

}

Protected void Painfo (Graphics g) {

G.DrawImage (InfoImg, Miningmapx Bombmapw * MiningGridLength 1,

MiningMapy, Graphics.top | Graphics.Left;

G.SetFont (font);

G.SetColor (0x00FFFFF);

g.drawstring (String.Valueof (Bomblef), MiningMapx BombMapw * MiningGridLength Bombnowx,

MiningMapy Bombnowy, Graphics.top | Graphics.Left);

g.drawstring (String.Valueof (Player1Found), MiningMapx Bombmapw * MiningGridLength Player1x,

MiningMapy Player1Y, Graphics.top | Graphics.Left);

g.drawstring (String.Valueof (Player2Found), MiningMapx BombMapw * MiningGridLength Player2x,

MiningMapy Player2y, Graphics.top | Graphics.Left);

/ / The code next to this method is used to draw a small map in the information box, which is the mine area displayed on the screen throughout the mine area.

In the position

g.setcolor (0x00777777);

G.FillRect (MiningMapx Bombmapw * mininggridens 1, MiningMapy 8 * mininggridLength 1,

2 * mininggridens-2, 2 * mininggridLength-1);

g.setcolor (0x00000000);

G. DrawRect (MiningMapx Bombmapw * mininggridens 1, MiningMapy 8 * mininggridens 1, 2 * mininggridens-2, 2 * mininggridens ";

G.SetColor (0x00bbbbbb);

G.FillRect (MiningMapx Bombmapw * mininggridlength 4 2 * (PaintX-1),

MiningMapy 8 * MiningGridLength 4 2 * (Painty-1), 12, 17);

G.SetColor (0x00FFFFF);

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 the outer frame of player color outside the board

G.SetColor (0x000000FF);

Else

G.SetColor (0x00FF0000);

For (INT i = 1; i <= 5; i ) {

g.drawRect (miningmapx-i, miningmapy-i, miningmaplength 2 * i, miningmaplength 2 * i);

}

IF (isplayer1) // Draw a banner representing the player in the information box

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 (0x00FF0000); / / Draw the selection box, pay attention to the relationship between the total thunder and the area that can be displayed on the screen

g.drawroundRect (MiningMapx (SELECTEDX-PAINTX) * MiningGridLength-1,

MiningMapy * mininggridlength-1,

MiningGridLength 1, MiningGridLength 1, 2, 2);

}

Add from the Switch structure in the KeyPressed method

Case gameState_gameing:

{

IF (Keycode == fullcas.key_softkey1) {

Gamestate = Gamestate_GameMenu;

}

Else if (Action == fullcas.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 [SELECTEDY 1] [SELECTEDX 1] .hasfound) {

IF (Bombs [SELECTEDY 1] [SELECTEDX 1] .isbomb) {

Bombs [SELECTEDY 1] [SELECTEDX 1] .hasfound = true;

Bombs [SELECTEDY 1] [SELECTEDX 1] .isplayer1 = this.Isplayer1;

IF (Isplayer1)

Player1found ;

Else

Player2found ;

Bombleft -;

Checkwin (); // Monitor if it is excavated to the thunder

}

Else {

Bombout (SELECTEDY 1, SELECTEDX 1);

// If this light is not unreasonable, open all the mine stages that are connected to the same situation thereof and a lap around this area

Isplayer1 =! isplayer1;

}

}

}

// The following line of code is to adjust the coordinates of the area displayed on the screen to avoid selecting the box to run the screen range.

IF (SELECTEDX-PAINTX) <0)

Paintx = selectedX;

ELSE IF ((SELECTEDX-PAINTX)> = BombMapw)

PainTX = SELECTEDX-BOMBMAPW 1;

IF (SELECTEDY-PAINTY) <0)

Painty = classy;

Else IF (SELECTEDY-PAINTY)> = BombMaph)

Painty = SELECTEDY-BOMBMAPH 1;

Break;

}

Here, several methods should be used: We will call the call of this method in the player to dig the thunder, whenever a player digs to the thunder, it will change the value of Player1Found and Player2Found, this time detects the game. Winning, when there is a player's score greater than half of the total thunderstorm, the winning contract will be derived, at this time, the ALERT object shows the score and wins information, and it is necessary to prepare for the next game --- Game initialization Gameinit ()). Gameinit () method: This method is used to initialize the game. In both cases in two cases, the initialization game needs to be invoked, and the other is when the game is restarted, and the other is to prepare another start after the end of the game. Every time you need to initialize the game, you need to initialize all the variables of Bombinit () method: initialize the BOMB type two-dimensional table that represents the entire mine area, the patient's random arrangement of 52 mines is bombout () method: Classic mine, if We point to a thunder, it is not a thunder, and there is no thunder around. The computer will open all the similar thunderies around the surroundings, this method is this role. I have used it here, of course, we only need to monitor the flash bit of FASFOUND value of false, or you will find the following code to add the following code in MiningCanvas.java.

Public void gameinit () {

SBWON = false;

Bombinit ();

Player1Found = 0; Player2Found = 0;

SELECTEDX = MiningmapGrid / 4; selecty = miningmapgrid / 4;

Bombleft = Bombnum;

System.gc (); / / Manual garbage collection

}

Public void bombinit () {

INT BombIndex = 0;

INT x = 0, y = 0;

Random Random = new 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-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);

}

}

When the game is in the game, we need to have several functions: restart the game, open help, return to the main menu, exit the game, and enter the menu, you have to return to the ongoing game. When this game, the menu is very similar, I will not explain it in detail, just there is a defect here, after selecting the help during the game menu, choose to return in the help interface, will return to the main menu, and will not return this When the game is menu, of course, add a variable to solve this problem, the original code is as follows:

Add the following code in 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

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);

}

}

}

Add from the Switch structure in the KeyPressed method

Case GameState_GameMenu:

{

IF (getGameAction (keycode) == fullcanvas.up && gamemenuidx - 1> = 0) {

GameMenuidx -;

}

Else IF (Keycode) == Fullcanvas.down && GameMenuidx 1

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;

}

The final code is all here, according to the functional components of the game, the main code is basically given, and some code is a full program for the entire game, it is necessary to add the following: Modify the Switch structure in the keypressed method

Switch (Gamestate) {

...

DEFAULT:

Gamestate = Gamestate_Menu;

}

Repaint ();

Summarizing the entire game is all completed, everyone will feel very simple, but it is already a complete game, I hope to help some friends. Of course, we can do some extensions, such as plus sound, plus Bluetooth fighting, so that the game is slowly improved, and has commercial value. The article is slowly written here, and suddenly, suddenly found that the entire article completely deviates from the way I originally expected, I originally intended to ideas from the game, design, to the encoding, to the test removal of the entire process of writing articles The most important thing is the idea in the mind, including the wrong, imperfect and later revision, so that I can tell me the real process and experience of me. I didn't expect to write to the end or no effect, I hope this is just the experience of writing articles. I must improve the next time I will understand much. The article will not be written by writing, and every time you write, you can't wait to stick to you, even if you tell yourself every time you tell you to see if there is any mistake, you will be lazy. ,Ha ha. Since the "J2ME-MIDP1.0 game is complete - double mine (1)" a few days ago, it is close to see a game's code, you can't see it, just write this half, originally planned Supplement on an interface and a variable and a game thinking process, then write a post, by the way, the original code is put into packages together. Finally, everyone will learn J to smooth, more exchanges (MSN: Yinowl@hotmail.com QQ: 47599318 E-mail: yinowl@163.com).

转载请注明原文地址:https://www.9cbs.com/read-58358.html

New Post(0)