J2ME Game Engine Program structure

Source: Internet
Author: User
Tags contains count sleep thread
The program game engine has a lot of structure, but it is basically implemented in a game master loop. The main loop inside the program contains the most important structure of the program framework. J2ME programs typically contain two class files, one is MIDlet, and the other is displayable. I usually put the main code of the game in displayable this class. This class is an event-driven program that has three main corresponding functions void paint (Graphics g), void keypressed (int keycode), void keyreleased (int keycode).



1. Use runnable and create a thread's main loop

The general subject's approach is to let displayable this class implement runnable this interface, and then create a thread in its constructor, start its run () function, and the run function contains the main loop of the game. Here is my snippet code inside the Paladin.



public class Gamemidlet extends MIDlet {

static Gamemidlet instance;

Display display;

Gamedisplayable displayable = null;



Public Gamemidlet () {

instance = this;

display = Display.getdisplay (this);

displayable = new Gamedisplayable ();

}



public void startApp () {

Display.setcurrent (displayable);

}



public void Pauseapp () {

}



public void Destroyapp (Boolean unconditional) {

Displayable.running = false;

}



public static void Quitapp () {

Instance.destroyapp (TRUE);

Instance.notifydestroyed ();

instance = null;

}

}



public class Gamedisplayable extends Fullcanvas implements Runnable {

/** Main CONTROL Thread * *

Thread mainthread = null;

/** Game Clock interval millisecond unit * *

public static long timeinterval = 20;

public static Boolean isstable = true;



* * For the game clock variable * *

public static Long timeold = 0;

public static Long TimeNow = 0;

public long interval = 0;

public static Long Frames_per_second = 0;

int count = 0;

Long second = 0;

public static Boolean running = TRUE;

Public gamedisplayable () {

Start main thread

Thread mainthread = new Thread (this);

Mainthread.start ();

}

public void Run () {

while (running) {

TimeNow = System.currenttimemillis ();

Interval = Timenow-timeold;



if (interval >= timeinterval) {

Timeold = TimeNow;

Game_process ();

if (second!= (System.currenttimemillis ()/1000)) {

Second = System.currenttimemillis ()/1000;

Frames_per_second = count;

Count = 1;

}

Else

count++;

}

Lib.sleep (30);

}



}



The code to control the speed of the main cycle can not, but Lib.sleep (30) must be retained, because on the Nokia 60 phone, if the sleep (30), then the game will not be able to switch back. At the same time, in the game in any one of the internal loop, you must also join the sleep (30) This wait, in order to allow the game to switch back, as to why this, I am not clear yet. 30ms is a number that I've tested for no problem, and maybe a smaller value than 30ms is no problem.



At the same time, in Moto's mobile phone, the game must be the main loop in a thread, the game can switch back, but can not add the above sleep (30) delay.



2. The main loop approach without using threads

This approach can only be implemented on the Nokia platform, and I only recommend that in the Nokia 40 platform to do so do not need a thread, the reason is to save some memory, if not a very tight memory model, it is best to use the previous approach.



The main loop of the game is placed in the MIDlet class, as follows:

public class Gamemidlet extends MIDlet {

Gamedisplayable displayable = null;



/** Game Clock interval millisecond unit * *

public static Long timeinterval = 0;

Variables for the game clock

public static Long timeold = 0;

public static Long TimeNow = 0;

public long interval = 0;

public static long frames_per_second=0;

int count=0;

Long second = 0;

public static Boolean running = FALSE;

static Boolean Exitapp =false;



Public Gamemidlet () {

displayable = new Gamedisplayable ();

Running =true;

}



public void startApp () {

Running =true;

Display.getdisplay (This). Setcurrent (displayable);

while (running) {

TimeNow = System.currenttimemillis ();

Interval = Timenow-timeold;

if (interval >= timeinterval) {

Timeold = TimeNow;

Displayable. Game_process ();

if (second!= (System.currenttimemillis ()/1000)) {

Second = System.currenttimemillis ()/1000;

Frames_per_second = count;

Count = 1;

}else

Count + +;

}

}

if (Exitapp) {

Destroyapp (TRUE);

Notifydestroyed ();

}



}



public void Pauseapp () {

Running =false;

}



public void Destroyapp (Boolean unconditional) {

running = false;

}



public static void Quitapp () {

Running =false;

Exitapp =true;

}



}





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.