Mobile game programming frameworks are always certain. Therefore, in order to save development time, I have compiled a general game framework based on the Nokia platform, so that when developing new projects, you only need to make a slight modification to go straight to the game topic. This framework performs button processing. The game subjects are divided into four major modules: gameinit (), gamemain (), gamedraw (), and gameexit (). The Code is as follows:
Import java. Io. inputstream;
Import javax. microedition. lcdui. Command;
Import javax. microedition. lcdui. commandlistener;
Import javax. microedition. lcdui. display;
Import javax. microedition. lcdui. displayable;
Import javax. microedition. lcdui. Font;
Import javax. microedition. lcdui. graphics;
Import javax. microedition. lcdui. image;
Import javax. microedition. Media. Manager;
Import javax. microedition. Media. player;
Import javax. microedition. MIDlet. MIDlet;
Import javax. microedition. MIDlet. midletstatechangeexception;
Import com. Nokia. Mid. UI. fullcanvas;
/**
* Project: j2metools
* JDK version used: jdk1.4.2
* Version: 1.00
* Created by: Floating white clouds
* Created Date: 2006-3-8 17:41:11
* Discription: Nokia game program framework
* Modification history
*
**/
Public class j2metools extends MIDlet
{
Gamecanvas canvas;
Display display;
Public j2metools (){
Super ();
Try {
Display = display. getdisplay (this );
Canvas = new gamecanvas (this, display );
} Catch (exception e ){}
// Todo auto-generated constructor stub
}
Protected void Startapp () throws midletstatechangeexception
{
// Todo auto-generated method stub
Display. setcurrent (canvas );
}
Protected void pauseapp ()
{
// Todo auto-generated method stub
}
Protected void destroyapp (Boolean arg0) throws midletstatechangeexception
{
// Todo auto-generated method stub
}
}
Class gamecanvas extends fullcanvas implements runnable, commandlistener
{
// ================================================ ====================
// System variable
// ================================================ ====================
Public thread;
Public j2metools app;
Display display;
Image offimage = NULL;
Public graphics G = NULL;
Public Boolean bepause = false;
Public Boolean beexit = false;
Public Boolean beinited = false;
Public static final int sleeptime = 35;
Static long starttime; // game start time
Static long Curt; // current time
Static long tmwait; // used for Timer
Static int scrw;
Static int scrh;
Static int fonth;
Static int fontw;
Static int mvx = 0; // adjust the reference origin here because the specific location of the hardware screen may be different.
Static int mvy = 0;
Static Font font;
// ================================================ ====================
// Mobile Phone platform related variables
// ================================================ ====================
Static final int gamew = 176; // screen width of the s60 Model
Static final int gameh = 208; // screen height of the s60 Model
// ================================================ ====================
// Keyboard Variables
// ================================================ ====================
Private Static int keynow = 0 ;//
Private Static int keyedge = 0 ;//
Private Static int keyprev = 0 ;//
Private Static int keyrep = 0 ;//
Private Static int keyrepwk = 0 ;//
Private Static int keyrepwait = 3; // after three consecutive buttons, calculate the duplicate button
Private Static final int k_right = 1;
Private Static final int k_left = 2;
Private Static final int k_up = 4;
Private Static final int k_down = 8;
Private Static final int k_select = 16;
Private Static final int k_soft1 = 32;
Private Static final int k_soft2 = 64;
// ================================================ ====================
// Depict related variables
// ================================================ ====================
// Back color
Public static final int bgcolorr = 41;
Public static final int bgcolorg = 174;
Public static final int bgcolorb = 255;
Private Static final int black = 0;
Private Static final int white = 0x00ffffff;
Private Static final int Red = 0x00ff0000;
Private Static final int Blue = 0x000000ff;
// ================================================ ====================
// Sound-related functions
// ================================================ ====================
Private int sndnum = 5; // background audio and effect audio. In this example, sndplayer [0] is the background audio, and others are the effect audio.
Private Boolean bgsoundon = true; // specifies the background tone switch flag.
Private Boolean efsoundon = true; // effect tone switch flag
Private int curbgs = 0; // the current background. If multiple background sounds exist
Public player [] sndplayer = new player [sndnum];
// ================================================ ====================
// Game status
// ================================================ ====================
Private int gamemode;
Private int submode;
// Master state Definition
Private Static final int gm_init = 0;
//.......
// Substate Definition
Private Static final int sm_init = 0;
Private Static final int sm_proc = 1;
Private Static final int sm_end = 2;
//.......
// ================================================ ====================
// Other variable definitions
// ================================================ ====================
// ================================================ ====================
// Function
// ================================================ ====================
Public void paint (Graphics _ g)
{
_ G. drawimage (offimage, 0, 0, 0 );
}
Public gamecanvas (j2metools _ app, display DSP)
{
APP = _ app;
Display = DSP;
Thread = new thread (this );
Thread. Start ();
}
Public void run ()
{
// Initialization
If (! Beinited ){
Gameinit ();
}
While (! Beexit ){
Curt = system. currenttimemillis ();
Try {
Thread. Sleep (sleeptime );
} Catch (exception e ){
}
Scankey (); // Keyboard Scan
Gamemain (); // game logic processing
Gamedraw (); // game profiling
}
If (beexit ){
Gameexit ();
}
}
/**
* Initialize the application
*/
Public void gameinit ()
{
// ================================================ ================
// Initialize system variables
// ================================================ ================
Starttime = system. currenttimemillis ();
Beexit = false;
Scrw = getwidth ();
Scrh = getheight ();
Mvx = (scrw-gamew)/2;
Mvy = (scrh-gameh)/2;
If (G = NULL | offimage = NULL ){
Offimage = image. createimage (scrw, scrh );
G = offimage. getgraphics ();
}
Repaint ();
Font = font. getfont (font. face_proportional,
Font. style_plain, Font. size_large );
G. setfont (font );
Fonth = font. getheight ();
Fontw = font. stringwidth ("word ");
// ================================================ ================
// Initialize other variables
// ================================================ ================
// Todo: Initialize other variables
Gamemode = gm_init;
Submode = sm_init;
Beinited = true;
}
/**
* Game logic processing
*/
Public void gamemain ()
{
If (gamemode = gm_init ){
If (submode = sm_init ){
// Timer example
Tmwait = Curt + 2000;
Submode = sm_proc;
}
Else if (submode = sm_proc ){
If (Curt> tmwait ){
Submode = sm_end;
}
}
Else if (submode = sm_end ){
// Button example
If (0! = (Keyrep | keyedge) & k_select )){
Beexit = true;
}
}
}
}
/**
* Game profiling
*/
Public void gamedraw ()
{
G. Translate (mvx, mvy );
If (gamemode = gm_init ){
If (submode = sm_proc ){
G. setcolor (bgcolorr, bgcolorg, bgcolorb );
G. fillrect (0, 0, scrw, scrh );
G. setcolor (white );
G. drawstring ("Nokia s60 demo! ", 30, 50, 0 );
}
Else if (submode = sm_end ){
G. setcolor (bgcolorr, bgcolorg, bgcolorb );
G. fillrect (0, 0, scrw, scrh );
G. setcolor (white );
G. drawstring ("timer use! ", 30, 50, 0 );
G. drawstring ("press enter to exit! ", 30, 70, 0 );
}
}
Repaint ();
}
/**
* Cleaning before the game ends
*/
Public void gameexit ()
{
// Todo: disconnect the network and release resources.
App. notifydestroyed ();
}
// ================================================ ====================
// Functions related to keyboard operations
// ================================================ ====================
Public void scankey ()
{
Keyedge = keynow &(~ Keyprev );
If (keynow = 0 | keynow! = Keyprev ){
Keyrepwk = 0;
Keyrep = keynow;
} Else {
If (keyrepwk ++> keyrepwait ){
Keyrepwk = 0;
Keyrep = keynow;
} Else {
Keyrep = 0;
}
}
Keyprev = keynow;
}
Private int getgamekey (INT keycode)
{
If (keycode = 0 ){
Return 0;
}
Int act = getgameaction (keycode );
If (Act = fire)
Return k_select;
Else if (Act = right)
Return k_right;
Else if (Act = left)
Return k_left;
Else if (Act = up)
Return k_up;
Else if (Act = down)
Return k_down;
// If (keycode = key_num0)
/Return 0x00000200;
// If (keycode = key_num1)
/Return 0x00000400;
If (keycode = key_num2)
Return k_up;
// If (keycode = key_num3)
/Return 0x00001000;
If (keycode = key_num4)
Return k_left;
If (keycode = key_num5)
Return k_select;
If (keycode = key_num6)
Return k_right;
// If (keycode = key_num7)
/Return 0x00010000;
If (keycode = key_num8)
Return k_down;
// If (keycode = key_num9)
/Return 0x00040000;
// If (keycode = key_pound)
/Return 0x00000100;
// If (keycode = key_star)
/Return 0x00000080;
If (keycode =-6)
Return k_soft1;
If (keycode =-7)
Return k_soft2;
Return 0;
}
Public void clearkey ()
{
Keynow = 0;
Keyprev = 0;
Keyedge = 0;
Keyrep = 0;
}
Protected synchronized void keypressed (INT keycode)
{
Int key = getgamekey (keycode );
Keynow | = key;
// Power key
If (keycode =-11 ){
Gameexit ();
}
}
Protected synchronized void keyreleased (INT keycode ){
Int key = getgamekey (keycode );
Keynow ^ = key;
}
// ================================================ ====================
// Sound-related functions
// ================================================ ====================
Private void loadsound (INT no ){
Try {
String loc = "/RES/snd" + integer. tostring (NO) + ". Mid ";
Inputstream is = getclass (). getresourceasstream (LOC );
String ctype = "audio/Midi ";
Sndplayer [No] = manager. createplayer (is, ctype );
Sndplayer [No]. Realize ();
Sndplayer [No]. prefetch ();
} Catch (exception e ){
E. printstacktrace ();
}
}
Private void playsound (INT No)
{
Try {
If (NO = 0 ){
If (bgsoundon ){
// If (sndplayer [No]. getstate ()! = Player. Started ){
Stopbgm ();
Loadsound (NO );
Sndplayer [No]. setloopcount (-1 );
Sndplayer [No]. Start ();
//}
Curbgs = no;
}
} Else {
If (efsoundon ){
Stopse ();
Loadsound (NO );
Sndplayer [No]. setloopcount (1 );
Sndplayer [No]. Start ();
}
}
} Catch (exception e ){
E. printstacktrace ();
}
}
Public void stopbgm ()
{
Try {
Sndplayer [0]. Close ();
System. GC ();
} Catch (exception e ){
}
}
Private void stopse ()
{
Try {
For (INT I = 1; I <6; I ++ ){
Sndplayer [I]. Close ();
}
System. GC ();
} Catch (exception e ){
}
}
Public void commandaction (command arg0, displayable arg1)
{}
}