J2ME mobile game Engine program architecture

Source: Internet
Author: User

In game development and any software design need to build a stable engine structure, as the basic structure of software design, software architecture is the core framework of support, all the implementation of functions and expansion of this infrastructure to complete. A game's design involves the game control, the role, the game scene, the props, the game sound effect as well as the game process event monitoring mechanism and so on element, therefore establishes the good game engine to be able to carry on the effective control to above each element. This paper introduces the basic program architecture of game engine in J2ME game design.

The game engine can build different structures to achieve, but any game design is inseparable from the design of a main loop loops to control the entire game process, of course, the whole game's core structure (background Control, wizard coordinates change, action frame screen changes, etc.) are included in the loop. In the J2ME game design, the program architecture generally needs to establish two core classes, that is midlet and displayable two kinds.

1, where MIDlet is the main class of J2ME, any program main classes need to inherit midlet to control the entire game life cycle, need to cover midlet three cycle methods Startapp, Pauseapp and Destoryapp, respectively, for execution start, pause, An action body that ends such an action.

Public Classgametest extends MIDlet implements commandlistener{
/**
* Game Engine Introduction
*/
Private Command Exitcommand;
Private Gamecanvastest Gamecanvas;  
Public gametest () {
Exitcommand=new Command ("exit?,command.exit,1");
Gamecanvastest gamecanvas=new gamecanvastest (this);
Sfcan.addcommand (Exitcommand);
Sfcan.setcommandlistener (this);
Display.getdisplay (this). Setcurrent (Gamecanvas);  
//TODO automatically generate constructor stubs
}
protected void startApp () throws Midletstatechangeexception {
//Todo automatically generate method stubs
GameCanvas.thread.start ();  
}
protected void Pauseapp () {
//TODO automatically generates method stubs
try{
gameCanvas.thread.wait ();
}catch (Exception e) {}
}
protected void Destroyapp (Boolean arg0) throws Midletstatechangeexception {
//TODO Self Dynamic generation method Stub
Gamecanvas=null;
}  
public void Commandaction (Command c,displayable d) {
if (c==exitcommand) {
notifydestroyed ();

}
//Implementation of the game process interface with different operational changes to the scheduling
public void Changetale (int taleindex)
{
Concrete code implementation ....
Display.getdisplay (this). Setcurrent (...);
}
}

Gametest class inherits the MIDlet class, realizes the control of the game lifecycle, and only controls the game lifecycle and the UI scheduling in the game, and puts the specific game core module into the Gamecanvastest class.

2, Displayable is the game's display of the core, in the J2ME game design using Gamecanvas class to complete the game canvas work, design class inheritance Gamecanvas to implement the game core module, usually the game Master loop placed in the class to achieve the control of the entire game, Therefore, it is necessary to implement the cyclic runnable interface while inheriting Gamecanvas. The
 public class GameCanvasTest extends GameCanvas implements Runnable {
   //声明变量.......
 public GameCanvasTest(MIDlet midlet)
 {
   super(true);
   //初始化变量.......
 }
  public void run(){
   keyPressed(getKeyStates());
   spritePixel();
   drawCanvas();
   //其他方法以及处理.......
  }
  protected void keyPressed(int keyCode){
  //按键处理.......
}
 public void spritePixel(){
 //精灵以及地图坐标变幻控制......
}
 public void drawCanvas(){
 //根据坐标变化重绘画面处理........
}
}
Gamecanvastest class completes the game backbone, including keyboard key event handling, coordinate changes, picture redraw, and, of course, game Elves and map backgrounds are also defined in this class and are managed using Layermanager.

The above is the general game structure, of course, in the more complex game design, the use of the above architecture will be more cumbersome, so that the program is not clear. Therefore, in the program design, we can use the MVC pattern to build the game engine, to abstract different layers (UI display, line program control system and input monitor, etc.) to complete their functions, so as to improve the readability and maintainability of the software.

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.