j2me-midp1.0 game Complete implementation-two-person Minesweeper 1.0 (i)

Source: Internet
Author: User
Tags array contains final mail variables variable thread
Author: yinowl
February 2005

Objective
My last article is also the first article "j2me-midp1.0 Small Game introduction-Gobang" posted later, there are a lot of friends to send mail, add QQ, plus MSN and I chat about J2ME content, I am very happy and very feeling, happy is not because of their article how to write how and there are many people contact me, But with so many friends learning J2ME, I thought that there was no one to contact the author of the article, it seems I was wrong; feeling is because, I know my first article is actually very bad, from everyone's reaction, I can feel the lack of original information online, official or book although authoritative and, But always feel the time and the actual situation a little distance, I look at this material, also often think, the actual game development company for how to deal with, how will write this code, the process of the structure of the program and so on. The biggest part of the original article is that it contains the author's experience, Practical development experience. I am very happy to continue to write some of my own experiences and techniques to enrich our Chinese resources. So I went on to write this document, the document content that is the process of the development of the program is true, not for writing articles and writing articles, is completely in the near period of time to complete the first procedure, and now write it as a teaching article. May everyone learn J smooth, a lot of communication (msn:yinowl@163.com qq:47599318 e-mail:yinowl@163.com)

Attention
Platform: This game I was designed on the Nokia platform, that is, the use of the Fullcanvas class and for the Nokia-60 series (7650) of the screen design, slightly modified to run on other models of the phone (I have made siemens-c65, NOKIA-7210, as well as a few porting versions of all mobile phones that support standard midp1.0/2.0, screen 128 x 128, you need to contact me.
Code: Same as my previous article "j2me-midp1.0 Small Game introduction-Gobang", the code listed in the form of interpretation of the "J2ME Game Programming" book, according to the program function to give the relevant code, a file of the code will be based on the function in different sections given, When the article is over, the code is complete. This is different from the usual code in the book to the file as a whole, I think this will help to let you understand a program from design to the final completion of ideas.

Game Introduction
Minesweeper this game we must be familiar with, but this two-person mine-clearing game of the new way we may have not seen, in fact, this is a network of MSN software Online games, we use MSN every day, but have little attention or play MSN Games Bar, It can be said that I am the MSN on the online two-person mine-clearance transplant to the phone, exactly the same. If you are now not convenient to surf the Internet or no one and you online look at the game's interface and play, it's okay, I'll introduce the game area (mined area) in a total of x 16 A total of 256 lattice, of which 52 ray, the operation in addition to the next key (of course, the computer is using the mouse point) only need a mine-digging key, All you have to do is dig up the thunder instead of using another key to mark the Thunder. Two players, one side began to dig thunder, if dug to (point to) ray, then add a point, no, just like the classic minesweeper, it shows a couple of mines around this position, and if a ray doesn't, like the classic minesweeper, it opens up all the squares that are not connected to the grid and then the outer circle. Then another player to dig thunder, who first dug to more than half (more than 26) of the Thunder who wins, in fact very simple, the game's interface is as follows:





Game Logic Design
Data structure: This game belongs to the two-dimensional chess game, so we naturally think of designing a Bomb class that represents each bit of a mine, using a two-dimensional array of Bomb types to represent all the minefields in the entire mined area, Each bit (that is, each bomb instance) contains a Boolean variable that indicates whether the mine is a ray. A Boolean variable that indicates whether the mine bit has been dug, a Boolean variable that indicates whether the player has been dug, or not, whether it was dug by the player two. An int type variable that represents a total of several mines in the eight mined places around the minefield. So the state of the entire minefield is fully described, and then the game interface is drawn based on the two-dimensional table.
Game flow: The entire game uses only a single Fullcanvas class, with only one state variable to control the state of the game, the entire game only uses its own unique thread
Player SWITCH: Use a Boolean variable to represent the currently executing player, using the outer frame color of the game and the color of the flag in the lower right corner.

The next step is to introduce and list the various parts of the game program code, the end of the article, the entire game is completed

Application class: Miningmidlet.java
The first is a MIDlet class. The Miningmidlet class inherits from the MIDlet class, which is used to connect the device's application Manager (Application Manager), by means of a method Startapp,pauseapp,destroyapp to notify the start of the game, pause, and destroy the end. 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;
Object that defines the Fullcanvas class Miningcanvas of the game interface Miningcanvas
Public Miningmidlet () {
Display=display.getdisplay (this);
Miningcanvas=new Miningcanvas (this);
To generate an object for the Miningcanvas class Miningcanvas
}
protected void startApp () {
MiningMIDlet.display.setCurrent (Miningcanvas);
Draw a game to meet on the screen Miningcanvas
}
protected void Pauseapp () {
}
protected void Destroyapp (Boolean arg0) {
Notifydestroyed ();
}
}


Mine Category: Bomb.java
The Bomb class defines all the information for each of the minefields in a two-dimensional minefield, so that a two-dimensional table of Bomb types can fully describe the state of the entire mined area. In the bomb class, I used three boolean variables and an int variable to describe a bit: Boolean variable Isbomb: Describes whether this position is thunder; Boolean variable Hasfound: Describes whether this location has been mined Boolean variable IsPlayer1: Description If this position is ray, and has been dug, then whether the player is a mining, if the value is false, indicating that the player is the second mining, only in Isbomb and Hasfound are true, the value of this variable is meaningful int variable Bombaround: Describes a total of several mines in eight locations around this location, and this variable can be used to open a range of non mined areas in addition to the number of mines surrounding the player. These four variables can be combined with different images to render the entire area of the graphic interface of the different states in front of the player. The source code for the bomb class is as follows:

Package com.imy.yinowl.miningscroll;
public class Bomb {
int bombaround;
Boolean isPlayer1;
Boolean hasfound;
Boolean Isbomb;

Public Bomb () {
Bombaround=0;
Isbomb=false;
Hasfound=false;
Isplayer1=true;
}
}


Interface Logic class Framework: Miningcanvas.java
General games will appear a lot of interface, such as the game logo (also known as splash screen or launch interface), Start menu (Main menu), Settings interface, game interface, help interface, about the interface, game Help menu and so on, my game does not need to set so no interface, All of these interfaces are integrated in the Fullcanvas class that inherits from Nokia-api, we want to minimize the number of classes as much as possible to reduce the cost of resources, specifically how to integrate, we will later in detail. Fullcanvas does not support command and Commandlistener, so we can no longer use the original command method, we will need to control the time of the start interface through the thread, so we use the game's own main thread.
The whole game we use an int variable gamestate with the switch structure to control the interface state in the game, that is, the current display interface, in the switch structure according to gamestate different values, draw a different interface, Changing the value of gamestate at the appropriate time, and then repaint, changes the interface that the player sees next, and also uses this method to control the corresponding key in the Keypressed method
The framework source code is as follows:

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;//game load when the default interface is the start interface
}
protected void Paint (Graphics g) {
G.setcolor (0X00FFFFFF);
G.fillrect (0,0,canvasw,canvash);
Switch (gamestate) {
Case Gamestate_splash:
Paintsplashscreen (g);//Draw Game Start interface
Break
Case Gamestate_menu:
Paintmenuscreen (g);//Draw Game main Menu
Break
Case GAMESTATE_HELP:
Painthelpscreen (g);//Draw Help interface
Break
Case gamestate_gameing:
Paintgamescreen (g);//Draw Game interface
Break
Case Gamestate_gamemenu:
Paintgamemenuscreen (g);//Menu interface when drawing games
Break
Default
Paintmenuscreen (g);//Draw Game main Menu
Break
}
public void Run () {
}
protected synchronized void keypressed (int keycode) {
int action = getgameaction (KeyCode);
Switch (gamestate) {
}
}
}


The next step is to design and draw every interface to the game.

Start screen
The General splash screen is a static or dynamic picture (mostly static), shows the game's logo and game distribution or the name of the producer, the stay time is three seconds, and can not be skipped through the key, if the game needs to do some time-consuming initialization, can be in the same time three seconds. This stay of three seconds is achieved by controlling this thread. Image: The source code is as follows:

Add the following code to the Miningcanvas.java

Thread thread;
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 animation stop 3 seconds, change the game state variable value, jump to the main menu state
Repaint ();
}


Game Main Menu Interface
The main menu displays the selected and unchecked menu items through two fonts of different sizes, an int variable to represent the currently selected menu item, and a string-one-dimensional array to store the contents of the item. In the Keypresseed method according to the menu option variable MENUIDX to change the game status, so that the game jump to the corresponding state. The source code you need to add is as follows:

Add the following code to the 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);
Two fonts, unselected and checked, respectively
static final int lowcolor = 0x000000ff;//unchecked color static final int highcolor = 0x00ff0000;//selected color static final int HIGHBGC Olor = 0x00cccccc;//The background color of the selected state
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 wide static int canvash;//screen high static int startheight;//menu list start height static int spacing;//menu item width static int menuidx;//The currently selected menu item
Public Miningcanvas (Miningmidlet miningmidlet) {
...
Menuidx = 0;
Canvasw=getwidth (); Canvash=getheight ();
spacing = Highfont.getheight ()/2;
Startheight = (canvash-(lowfont.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 < mainmenu.length;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 in the switch structure in the Keypressed method
Case Gamestate_menu:
{
if (getgameaction (keycode) = = fullcanvas.up && menuIdx-1 >= 0) {
menuidx--;
}
else if (getgameaction (keycode) = = Fullcanvas.down && menuidx + 1 < mainmenu.length) {
menuidx++;
}
else if (getgameaction (keycode) = = Fullcanvas.fire) {
Switch (MENUIDX) {//checked, change the game state value according to the option value, and 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
}
}






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.