Application of State mode in J2ME game design (require source code to leave e-mail)

Source: Internet
Author: User
Tags implement mail require
Design | source code
The state transition in game design is a very important and complex part. When I was doing a transplant the other day, I saw a few 2 million lines of code in the middle of a public void keypressed (int keycode), so that lengthy code not only does not conform to Oo's thinking but also disturbs our program thinking. This two-day use of state mode to do a J2ME game in the status of the transition because it is just an example of an experiment so only to do the Start menu and help conversion, in this framework to add other states is actually very simple.

Now there are only two states in the demo, two states under the keypressed and paint function is different, we use state mode to achieve a variety of state keypressed and paint function.

First, define the state interface for abstract states, which specifies the behavior of the keypressed and paint
Public interface State {
public void onkeypressed (int keycode,statemanage _current);

public void Paint (Graphics g);
}
State interface Keypressed and paint methods, we implement two subclass Helpmenustate and Mainmenustate, representing the status of the main menu state and the Help menu respectively, and implement the specific onkeypressed and paint methods:
public class Mainmenustate implements state {
/** the declaration of the omitted variable and the first part of the initialization.
public void onkeypressed (int keycode, statemanage _current) {
Switch (whichmenu) {
Case 0:
Mainmenukeypressed (keycode,_current);
Break
Case 1:
Break
}
}

private void mainmenukeypressed (int keycode,statemanage _current) {
Switch (keycode) {
Case 1://key_up = 1;
MenuIndex = MenuIndex <= 0? Menumain.length-1:--menuindex;
Break
Case 6://key_down = 6;
MenuIndex = MenuIndex >= menumain.length-1? 0: ++menuindex;
Break
Case 8://key_middle = 8;
if (MenuIndex = 2)
_current.setstate (New Helpmenustate ());
Break
}
}

public void Paint (Graphics g) {
Switch (whichmenu) {
Case 0:
Drawmainmenu (g);
Break
}
}

private void Drawmainmenu (Graphics g) {
G.drawimage (imgmenu, 0, 0, 0);
G.setcolor (0, 0, 0);
G.setfont (Gameinfo.font_large);
for (int i = 0; i < menumain.length; i++)
g.DrawString (Menumain[i], a. + I *, graphics.top
| Graphics.hcenter);
G.drawimage (Imgindex, MenuIndex, 0); Draw the index chart
G.setcolor (255, 0, 0);
g.DrawString (Menumain[menuindex], MenuIndex,
Graphics.top | Graphics.hcenter);
}
}

public class Helpmenustate implements state {

/** the declaration of the omitted variable and the first part of the initialization.
public void onkeypressed (int keycode, statemanage _current) {
Switch (whichmenu) {
Case 0:
Helpmenukeypressed (KeyCode, _current);
Break
Case 1:
Break
}
}

public void helpmenukeypressed (int keycode, statemanage _current) {
Switch (keycode) {
Case 1://key_up = 1;
MenuIndex = MenuIndex <= 0? Helpmenustring.length-1
:--menuindex;
Break
Case 6://key_down = 6;
MenuIndex = MenuIndex >= helpmenustring.length-1? 0
: ++menuindex;
Break
Case 8://key_middle = 8;
if (MenuIndex = = 4) {
_current.setstate (New Mainmenustate ());
}
Break
}
}

public void Paint (Graphics g) {
Drawhelp (g);
}

private void Drawhelp (Graphics g) {
for (int i = 0; I <= i++) {
for (int j = 0; J <= J) {
G.drawimage (IMG, 0 + * I, 0 + * j, 0);
}
}
G.setcolor (251, 220, 48);
G.setfont (Gameinfo.font_large);
g.DrawString ("<< help >>", Graphics.top | Graphics.hcenter);

for (int i = 0; i < helpmenustring.length; i++)
g.DrawString (Helpmenustring[i], a. + I *, graphics.top
| Graphics.hcenter);
G.setcolor (255, 0, 0);
g.DrawString (Helpmenustring[menuindex], MenuIndex,
Graphics.top | Graphics.hcenter);
}
}
Finally, define the Statemanage,statemanage instance as the controller for state transitions:
public class Statemanage {
State _current;
Public Statemanage () {
_current = new Mainmenustate (); Start with main Menu
}

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.