Beginner's journey to j2-2d games (1) game framework

Source: Internet
Author: User
Tags clear screen

By favoyang

A few days ago, he saw Tony publish his learning work on csdn "60 s for a man". He thought that although his creativity was simple, he was very playable. He was an entry-level classic in learning how to create mobile games, as a result, Tony's image is still used for learning. If you are interested in this game, contact Tony or visit his blog.

In terms of development trends, midp2.0 is a trend. The cheapest midp2.0 mobile phone, such as ot735i, is already about 1700 yuan. The Siemens high-end machine cx65 a year ago is only about 2500 yuan; in addition, the price of-midp2.0 mobile phone has a variety of options, Siemens, Se, N machines are available. I personally like cx65. If the costs of mobile phone manufacturers keep decreasing in the future, I believe that 1500 yuan of MIDP will not be a dream... Of course, it depends on whether the application is rich.

To put it bluntly, we will use MIDP 2.0 to develop our game code: fly. Development Tool jbulider. After the article is fully written, the SRC download will be provided.

Directory:

I. Game framework

2. Improve peripheral tools (images, gameobject, and font)

Iii. control plane movement

4. Add a bullet group for collision calculation

5. Implement the explosive effect and add a prop Missile

6. What do you think?

VII. Source Code

I. Game framework

Our games require a general game framework for future development, but implementing an engine is complicated. As a beginner, if you want to think too much about it, I am afraid it will lead you away from the main line. Here we only provide the canvas code, if you do not understand it, you can refer to another series of articles on this site, "using midp2.0 to develop Games".

Use singlon, because each gamecanvas requires a lot of memory space. In addition, we only need to rewrite gameinit (), gamemain (), and write the code initialized at one time 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); // 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 character STR = C. getlabel ();
If (reverse Str. Equals ("START ")){
Gameinit ();
Start ();
Removecommand (startcmd );
Addcommand (restartcmd = new command ("restart", command. OK, 1 ));
} Else if (reverse Str. Equals ("restart ")){
Stop ();
While (T. isalive ());
Gameinit ();
Start ();
} Else if (reverse Str. Equals ("exit ")){
Stop ();
Navigate. MIDlet. destroyapp (false );
Navigate. MIDlet. policydestroyed ();
}
}

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;
}

}

In terms of development trends, midp2.0 is a trend. The cheapest midp2.0 mobile phone, such as ot735i, is already about 1700 yuan. The Siemens high-end machine cx65 a year ago is only about 2500 yuan; in addition, the price of-midp2.0 mobile phone has a variety of options, Siemens, Se, N machines are available. I personally like cx65. If the costs of mobile phone manufacturers keep decreasing in the future, I believe that 1500 yuan of MIDP will not be a dream... Of course, it depends on whether the application is rich.

To put it bluntly, we will use MIDP 2.0 to develop our game code: fly. Development Tool jbulider. After the article is fully written, the SRC download will be provided.

Directory:

I. Game framework

2. Improve peripheral tools (images, gameobject, and font)

Iii. control plane movement

4. Add a bullet group for collision calculation

5. Implement the explosive effect and add a prop Missile

6. What do you think?

VII. Source Code

I. Game framework

Our games require a general game framework for future development, but implementing an engine is complicated. As a beginner, if you want to think too much about it, I am afraid it will lead you away from the main line. Here we only provide the canvas code, if you do not understand it, you can refer to another series of articles on this site, "using midp2.0 to develop Games".

Use singlon, because each gamecanvas requires a lot of memory space. In addition, we only need to rewrite gameinit (), gamemain (), and write the code initialized at one time 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); // 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 character STR = C. getlabel ();
If (reverse Str. Equals ("START ")){
Gameinit ();
Start ();
Removecommand (startcmd );
Addcommand (restartcmd = new command ("restart", command. OK, 1 ));
} Else if (reverse Str. Equals ("restart ")){
Stop ();
While (T. isalive ());
Gameinit ();
Start ();
} Else if (reverse Str. Equals ("exit ")){
Stop ();
Navigate. MIDlet. destroyapp (false );
Navigate. MIDlet. policydestroyed ();
}
}

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.