J2ME 2D Game Getting Started (1) Framework

xiaoxiao2021-03-06  128

Favoyang reproduced please contact the author to respond to www.j2medev.com stationmaster Mingjava's call, I also share my experience with everyone, I hope everyone will advise. At the same time www.j2medev.com welcomes the original articles of your masters.

A few days ago, I saw Tony published my own learning work on 9CBS. "It's a man who insists on 60s." Although the creative is simple but it is very play, it is the entrance classic of learning mobile game production, so the clone has a picture. Still using Tony's picture, pure learning. If you are interested in this game, you can contact Tony or visit his blog (his blog: blog.9cbs.net/yesming).

From the development trend, MIDP2.0 is trend, the cheapest MIDP2.0 mobile phone such as OT735i, has been around 1700 yuan; the Siemens have a high-end CX65 a year ago, now only 2500; and 2500-3000 MIDP2 .0 mobile phones have a variety of options, Siemens, SE, and N machines. I personally like CX65, if the cost of mobile phone manufacturers will continue to decrease in the future, I believe that the MIDP of 1,500 yuan will not be a dream ... Of course, I have to see if the application is rich.

For the collateration, we will use MIDP 2.0 to develop our game, code fly. Development tool JBulider. After all articles are all written, I will provide SRC download.

table of Contents:

First, the frame of the game

Second, improve the peripheral tool (image, GameObject, font)

Third, control the movement of the aircraft

Fourth, join the bullet group to achieve collision operation

5. Implement the explosion effect, and join the props missile

Six, less than enough, what do you think?

Seven, source code

First, the frame of the game

Our game requires a universal game framework, which is also convenient for development, but implementing an engine is complicated. As an initiator, if you want you to consider too many questions, I am afraid you will let you deviate from the main line, here only give the Canvas code, you can see another series of articles "using MIDP2.0 development game".

Use Singlon implementation because each Gamecanvas requires a lot of memory spaces. In addition, for us, as long as you rewrite Gameinit (), GameMain (), a one-time initialized code written in the constructor. public class MyGameCanvas extends GameCanvasimplements Runnable, CommandListener {private static MyGameCanvas instance; Graphics g; boolean running; Thread t; Command startcmd, exitcmd, restartcmd; int keystate; boolean keyevent; boolean key_up, key_down, key_left, key_right, key_fire; private boolean allowinput Public int Screenwidth; Public Int ScreenHeight; Boolean GameOver; // Define Your Variable Here // Define Your Variable End

Protected mygamecanvas () {super (true); g = getgraphics (); running = false; t = null; addcommand (startcmd = new command ("start", command.ok, 1)); addcommand (exitcmd = new Command "exit", Command.EXIT, 1)); setCommandListener (this); screenwidth = getWidth (); screenheight = getHeight (); // put your init once code here // put your init once code end} synchronized public static MyGameCanvas GetInstance () {if (instance == null) {instance = new mygamecanvas (); system.out.println ("new mygamecanvas");} Return Instance;

Public void run () {system.out.println ("MyGamecanvas Run Start"); long st = 0, et = 0, DIFF = 0; int Re = 50; // 16-17 frame per second while ST = system.currenttimemillis (); GameInput (); GameMain (); et = system.currenttimemillis (); diff = etc {//system.out.println ("Sleep" Rate-double); try {thread.sleep (Rate - DIFF);} catch (interruptedException ex) {}} else {//system.out.println ("Rush, and the frame useing time: á" DIFF) ;}} System.out.println ("MyGamecanvas Run end");

Public void start () {if (! running) {Running = true; t = new thread (this); t.start ();}}

Private void Gamemain () {G.SetColor (0, 0); // Clear Screen G.FillRect (0, 0, getWidth (), GetHeight ());

Background.paint (g); // Draw Background //g.SetColor (255, 255, 255); //g.drawstring ("Hello ", 1, 1, g.top|g.LEFT); flushgraphics ();

private void gameInit () {gameover = false; gametime = 0; gametimeoffset = System.currentTimeMillis (); allowinput = true; key_up = key_down = key_left = key_right = key_fire = false;} public void stop () {if (running) { Running = false;}}

Public void CommandAction (Command C, Displayable D) {string cmdstr = c.getlabel (); if (cmdstr.equals ("start")) {Gameinit (); start (); removCommand (startcmd); addcommand (restartcmd = new Command ("Restart", Command.ok, 1));} else if (cmdstr.equals ("restart")) {stop (); while (t.isalive ()); gameinit (); start ();} Else IF ("exit")) {stop (); navigate.midlet.destroyApp (false); navigate.midlet.notifyDestroyed ();}}

private void gameinput () {if (allowinput) {keystate = getKeyStates (); keyevent = false; if (! (keystate & UP_PRESSED) = 0) {// up key_up = true; keyevent = true; // deal your unstop job Code head //system.out.println ("up press "); // DEAL Your unstop job code end} else if ((KeyState & up_pressed) == 0) {// Release Key if (Key_up == true) { Key_UP = false; // deal your one press-one job code here //system.out.println ("up release "); // deal your one press-one job code end}}

IF (KeyState & Down_Pressed)! = 0) {// Down Key_down = true; keyevent = true; // deal your unstop job code here //system.out.println ("Down press "); // DEAL Your unstop Job code end} else if (KeyState & Down_Pressed) == 0) {// Release Key IF (key_down == true) {key_down = false; // deal your one press-one job code here //system.out. Println ("Down Release"); // DEAL YOUR One Press-One Job Code End}} f ((KeyState & Left_Pressed)! = 0) {// Left key_left = true; keyevent = true; // DEAL Your unstop Job Code head //system.out.println ("left press "); // deal your unstop job code end} else f ((KeyState & Left_pressed) == 0) {// Release Key if (Key_LEFT == true) { Key_LEFT = false; // deal your one press-one job code here //system.out.println ("left release "); // deal your one press-one job code end}}

IF (KeyState & Right_Pressed)! = 0) {// Right key_right = true; keyevent = true; // DEAL Your unstop job code here //system.out.println ("right press "); // DEAL Your unstop Job code end} else if ((KeyState & right_pressed) == 0) {// Release Key IF (key_right == true) {key_right = false; // deal your one press-one job code here //system.out. Println ("Right Release"); // DEAL YOUR One Press-One Job Code End}}

IF (KeyState & Fire_Pressed)! = 0) {// Fire Key_Fire = true; keyevent = true; // deal your unstop job code here //system.out.println ("Fire Press "); // DEAL Your unstop Job code end} else if (KeyState & Fire_Pressed) == 0) {// Release Key IF (key_fire == true) {key_fire = false; // deal your one press-one job code here //system.out. Println ("fire release"); // deal your one press-one job code end}} f (! keyevent) {// no keyevent here //system.out.println ("no key press "); // no KeyEvent end}}} public static void cleanjob () {instance = null;}

}

转载请注明原文地址:https://www.9cbs.com/read-99732.html

New Post(0)