Foreword, my previous article, "J2ME-MIDP1.0 game entry - five children" post, have a lot of friends send an email, plus QQ, add MSN and I talk about J2ME content, I am very happy. It is also very emotional, nothing is not because of how many people write me, but there are so many friends who have learned J2ME, I originally thought that there is no one will contact the author of the article now, it seems that I am wrong; Feeling because I know that my first article is actually very poor. From everyone's reaction, I can feel the lack of original information on the Internet, although the official or books is authoritative, but the time feeling and the actual situation It is a bit distance. When I am watching this information, I often think that the actual game development company is how to handle this code, how the structure of the program will work. The biggest a bit of the original article is that it contains the experience of the author and the actual development experience. Instrument to me and the online original article is limited, I am very happy to continue writing some of my own experiences and technologies, enrich our Chinese resources. So I will then write this document, the document content is true, not writing articles for writing articles, which is completely completed in recent time, and now write it into a teaching article. I hope that everyone will learn, and there are many exchanges (MSN: yinowl@163.com QQ: 47599318 E-mail: yinowl@163.com
Attention Platform: This game I was designed on the NOKIA platform, that is, using the Fullcanvas class and screen design for NOKIA-60 Series (7650), can run on other models of mobile phone (I Siemens-C65, NOKIA-7210, and all portions of mobile phones that support standard MIDP1.0 / 2.0, screen 128 x 128, need to contact me) code: The same as my previous article " J2ME-MIDP1.0 Game Getting Started - Five Suspension, the code lists the "J2ME Game Programming" book, and gives the relevant code according to the program function ideas, and the code of one file will be given according to the function. The article is over, the code is complete. This is different from the code in the usual book, which is all given in the unit. I think this more helps you understand a program from design to the last finish.
The game introduction, this game, everyone must be familiar, but the mine-sweeping game of this double mine-sweeping game may have never seen it. In fact, this is a network online game in the MSN software. Everyone is using MSN every day, but all Few attention or playing games in MSN, you can say that I am on the phone on the mobile phone on the MSN, exactly the same. If you are not convenient to go online or no one online, look at this game interface and play, it doesn't matter, I will introduce now, there are a total of 16 x 166 in the game area (mine), there are 256 lattices, of which 52 Thunder, operation in addition to the upper and lower keys (of course, the computer is used in the computer) only one digress, you have to dug out the thunder, not to use another button to mark the thunder. Two players, one party starting to dig up, if you dig (point to) thunder, add a point, don't dig, just like classic mines, showing a few thunders around this location, if a thunder is not In the same way as classic mines, all around the lattice will be opened around the lattice and another circle on the outside, and another player digs, who first digs to more than half (more than 26) thunder. Who will win, actually simple, the interface interface is as follows: Game logic design data structure: This game belongs to two-dimensional chess games, so we naturally think of designing a BOMB class representing each light position, using a bmb type 2 The dimension group represents all the lights of the entire mine area, each lighting bit (ie, each BOMB instance) contains a variable indicating whether the light is a Boolean type, a boolean type that is already excavated. Variables, a variable indicating whether it is a Boolean type that is excavated by the player (no representation is two digging by players), an int type variable that represents a total of several thunders around the mine. This is fully described in the state of the entire mine area, and then draws the game interface process based on this two-dimensional table: The entire game uses only a unique Fullcanvas class, with the only variable that represents the state to control the status of the entire game, The entire game also only uses its own unique thread player to switch: use a Boolean type variable to represent the currently executed player, expressed in the color of the game's outer box color and the flag of the lower right corner.
Next, you will start detailing each of the part of the game program, the end of the article, the entire game is completed.
Application Class: miningmidlet.java is first a MIDLET class. The MiningMidlet class inherits from the MIDLET class, which is used to connect the device's application manager, and notify the start, pause, and destruction of the game by methods startapp, pauseapp, design. The source code is as follows:
Package com.imy.yinowl.miningscroll; import javax.microedition.lcdui.display;
Import javax.microedition.midlet.midlet; public class miningmidlet extends MIDlet {miningcanvas miningcanvas;
/ / Define the object of the game interface Miningcanvas Miningcanvas public miningmidlet () {
Display = display.getdisplay (this);
Miningcanvas = new miningcanvas (this); // Generate Miningcanvas class object MiningCanvas
} Protected void startapp () {
Miningmidlet.display.Setcurrent (Miningcanvas);
// Painted the game on the screen Miningcanvas
} Protected void pauseapp () {
} Protected void destroyApp (boolean arg0) {
NotifyDestroyed ();
}
Mines: Bomb.javabomb class defines all information on each of the lights in the two-dimensional thunder, which can fully describe the state of the entire mine area with a BOMB type 2D table. In the BOMB class, I used three Boolean variables and an INT variable to describe a lighting bit: Boolean variable ISBOMB: Describes whether this location is a thunder; Boolean variable HASFound: Describes this location Has been excavated; Boolean ISPLAYER1: Description If this location is thunder, and has been excavated, then whether it is a player to dig, if the value is false, it is called by the player two, only when ISBOMB and HASFound are True, this variable value It is meaningful; INT type Bombaround: describes eight positions around this location A total of several thunders, in addition to telling the player around the mine around it. With these four variables, different images can be presented in front of the player's different states of the entire mine area. The source code of the BOMB class is as follows:
Package com.imy.ynowl.miningscroll; public class bomb {int bombaround;
Boolean isplayer1;
Boolean Hasfound;
Boolean ISBOMB;
Public bomb () {
Bombaround = 0;
ISBOMB = FALSE;
HASFOUND = FALSE;
Isplayer1 = true;
}
Interface Logical Class Frame: Miningcanvas.java General games There are many interfaces, such as game logo (also known as flash screen or boot interface), start menu (main menu), set interface, game interface, help interface, on the interface, When the game helps menu, I don't need to set up, so we don't have the interface. All of these interfaces we are integrated in this inheritance of the Fullcanvas class in the Nokia-API, we have to be as small as possible. To reduce the overhead of resources, how is it integrated, we will introduce a detail later. Fullcanvas does not support Command and CommandListener, so we can no longer use the original command method; we will need to start the interface through the thread control, so you will use the game's main thread. The entire game we use an INT variable GameState to control the Switch structure to control the interface status in the game, that is, the currently displayed interface, draw different interfaces according to different values in the Switch structure, change GameState in the corresponding time The value, then Repaint, also changed the interface that the player saw, and also use this method to control the corresponding framework source code of the player button in the keypressed method:
Package com.imy.yinowl.miningscroll;
Import java.io.ioException;
Import java.util.random; import java.util.vector;
Import javax.microedition.lcdui.alert;
Import javax.microedition.lcdui.alerttype;
Import javax.microedition.lcdui.font;
Import javax.microedition.lcdui.graphics; import javax.microedition.lcdui.image; import com.nokia.mid.ui.fullcanvas;
Public Class Miningcanvas Extends Fullcanvas IMPLEments Runnable {
Miningmidlet miningmidlet;
INT Gamestate;
Static Final Int GameState_Splash = 0;
Static Final Int GameState_Menu = 1;
Static Final Int GameState_gameing = 2;
Static final int gamestate_help = 3;
Static Final Int Gamestate_Setting = 4;
Static Final Int GameState_about = 5;
Static Final Int GameState_GameMenu = 6;
Static Final Int GameState_count = 7;
Public miningcanvas (Miningmidlet miningmidlet) {
Super ();
this.miningmidlet = miningmidlet;
GameState = 0; // When the game is loaded, the default interface is the startup interface.
}
Protected Void Paint (Graphics G) {
G.SetColor (0x00FFFFF);
G.FillRect (0, 0, canvasw, canvash);
Switch (Gamestate) {
Case GameState_Splash:
PaintSplashScreen (g); // Draw game start interface
Break;
Case GameState_Menu:
Paintmenuscreen (g); // Draw a game main menu
Break;
Case gameState_help:
PainThelpscreen (g); // Draw a help interface
Break;
Case gameState_gameing:
Paintgamescreen (g); // Draw game interface
Break;
Case GameState_GameMenu:
PAINTGAMEMENUSCREEN (G); // Draw a game menu interface
Break;
DEFAULT:
Paintmenuscreen (g); // Draw a game main menu
Break;
}
Public void run () {
}
Protected synchronized void keypressed (int keycode) {
Int action = getGameAction (keycode);
Switch (Gamestate) {
}
}
}
Next, you start designing every interface of the game.
The startup screen is a static or dynamic picture (static), which shows the name of the game's logo and the release of the game or producer, the residence time is three seconds, and cannot skip by pressing, if the game needs Some time-time initialization can be carried out simultaneously in these three seconds. This is three seconds to stay through the control of this thread. Image: The source code is as follows:
Add the following code in MiningCanvas.java
"Image splashimage;
INT SPLASHDELAYTIME;
Public miningcanvas (Miningmidlet miningmidlet) {
...
SplashdeLayTime = 3000;
Try {
SplashImage = image.createImage ("/ occo.png");
} catch (ioexception e) {}
Thread = new thread (this);
Thread.start ();
}
Private void PaintSplashScreen (graphics g) {
g.setcolor (0x00000000);
G.drawImage (SplashImage, getWidth () / 2, getHeight () / 2-5, graphics.hcenter | graphics.vcenter;
}
Public void run () {
Try {
Thread.Sleep (SplashdeLaytime);
} catch (interruptedexception e) {}
GameState = GameState_Menu; // After starting the animation for 3 seconds, change the game status variable value, jump to the main menu status
Repaint ();
}
Game main menu interface The main menu displays two types of fonts in different sizes, indicating the currently selected menu item with an INT variable, and stores the contents of the menu item with a string type one-dimensional array. In the keypresseed method, according to the menu option variable MENUIDX to change the status of the game, the game jumps to the corresponding state. The source code to be added is as follows:
Add the following code in MiningCanvas.java
Static Final Font LowFont = Font.GetFont (font.face_monospace, font.style_plain, font.size_small);
Static final font highfont = font.getfont (font.face_monospace, font.style_bold, font.size_medium);
// The two fonts are fonts that are unchecked and selected.
Static final int lowcolor = 0x000000FF; // Unchecked color static final int highcolor = 0x00FF0000; // Select the color static final int highbgcolor = 0x00cccccc; // Select the background color of the status
static final int MAIN_NEW_GAME = 0; static final int MAIN_SETTINGS = 1; static final int MAIN_HELP = 2; static final int MAIN_ABOUT = 3; static final int MAIN_EXIT = 4; static final int MAIN_MENU_ITEM_COUNT = 5; static String [] mainMenu = new String [Main_menu_item_count];
Static int canvasw; // screen width static int CANVASH; / / screen high static int startHeight; // STATIC INT SPACING; // Menu Item Width static int menuidx; // Currently selected menu item
Public miningcanvas (Miningmidlet miningmidlet) {
...
Menuidx = 0;
Canvasw = getWidth (); canvash = GetHeight (); spacing = highfont.getHeight () / 2;
Startheight = (canfont.getHeight () * MainMenu.length) - (MainMenu.length-1) * spacing) / 2;
Mainmenu [0] = "new game";
Mainmenu [1] = "settings";
Mainmenu [2] = "Help";
Mainmenu [3] = "about";
Mainmenu [4] = "exit";
}
Private void Paintmenuscreen (Graphics G) {
For (int i = 0; I IF (i == menuidx) { 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 (MAINMENU [I], (canvasw-highfont.stringwidth (Mainmenu [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 (MainMenu [i], (Canvasw - LowFont.StringWidth (MainMenu [i])) / 2, StartHeight i * (LowFont.getHeight () spacing, graphics.top | graphics.Left); } } } Add from the Switch structure in the KeyPressed method Case GameState_Menu: { IF (getGameAction (keycode) == fullcanvas.up && menuidx - 1> = 0) { Menuidx - } Else if (getGameAction) == Fullcanvas.down && Menuidx 1 MENUIDX ; } Else if (getGameAction (Keycode) == fullcanvas.fire) { After switch (menuidx) {//, follow the option value to change the game status value, jump to the corresponding state Case main_new_game: Gamestate = Gamestate_Gameing; Break; Case main_settings: Gamestate = Gamestate_Setting; Break; Case main_help: Gamestate = gamestate_help; Break; Case main_about: Gamestate = Gamestate_About; Break; Case main_exit: Miningmidlet.destroyApp (false); Break; } } Break; } }