J2ME 2D Small Game Introduction (i) the framework of the game

Source: Internet
Author: User
Tags define exit clear screen diff sleep thread
In response to the call of www.j2medev.com webmaster Mingjava, I also share with you my experience, I hope you will advise. At the same time www.j2medev.com welcome you to master the original article.

A few days ago to see Tony on the Csdn to publish his study work "is a man to insist on 60s", feel creative although simple but very resistant to play, is to learn the introduction of mobile phone game to create a classic, so a temporary rise, clone a bit, the picture is still using Tony's pictures, pure learning. If you are interested in this game, you can contact Tony or visit his blog.

From the development trend that midp2.0 is the trend, the cheapest midp2.0 mobile phone such as ot735i, has been about 1700 yuan, and Siemens a year ago, the high-end machine cx65, now only about 2500, and 2500-3000 of the price of midp2.0 mobile phones have a variety of options, Siemens , SE, n machine all have. I personally like cx65, if the future handset manufacturers will continue to reduce costs, I believe that the 1500-dollar MIDP is not a dream ... Of course also depends on whether the application is rich.

In turn, we will use MIDP 2.0来 to develop our game, code-named Fly. development tool Jbulider. After all the articles have been written, will provide src download.

Directory:

I. The framework of the game

Second, perfect peripheral tools (image, Gameobject, Font)

Iii. controlling the movement of aircraft

Iv. join the submunition to realize the collision operation

V. To achieve the effect of explosion and to add prop missiles

Six, not many, you think?

Seven, the source code

I. The framework of the game
Our game needs a common game framework, but also easy to develop later, but the implementation of an engine is complex. As a beginner if you want to consider too many questions, I am afraid that will let you deviate from the main line, here only give canvas code, do not understand you can see this site's another series of articles "use MIDP2.0 development game."

Use Singlon implementations, because each gamecanvas requires a lot of memory space. In addition, for us, just rewrite gameinit (), Gamemain (), and the one-time initialization code is written in the constructor.

public class Mygamecanvas extends Gamecanvas
Implements 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 RATE=50;//16-17 frame per second
while (running) {
St=system.currenttimemillis ();
Gameinput ();
Gamemain ();
Et=system.currenttimemillis ();
Diff=et-st;
if (diff<rate) {
System.out.println ("Sleep" + (Rate-diff));
try {
Thread.Sleep (Rate-diff);
}
catch (Interruptedexception ex) {}
}else{
System.out.println ("Rush, and the frame using 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,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 ();
Removecommand (Startcmd);
AddCommand (restartcmd=new Command ("Restart", command.ok,1));
}else if (cmdstr.equals ("restart")) {
Stop ();
while (T.isalive ());
Gameinit ();
Start ();
}else if (cmdstr.equals ("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 here
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
}
}

if ((Keystate & left_pressed)!=0) {//left
Key_left=true;keyevent=true;
Deal your unstop job code here

System.out.println ("left Press");
Deal your Unstop Job code end
}else if ((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
}
}
if (!keyevent) {
No keyevent here
System.out.println ("NO KEY Press");
No KeyEvent end
}
}
}

public static void Cleanjob () {
Instance=null;
}


}




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.