Preliminary Experience of mobile game programming with j2m's

Source: Internet
Author: User
Departure

It has been a while to learn about the technology of the platform. There have been countless examples mentioned in the book. Although I have basically understood the overall programming idea of the platform, I still have no clue about the game program.

I tried to beat the game [Magic tower] for a long time and thought about it for a long time. I felt that the complexity was high, and some details, such as the settings of the level, and I had never touched any other program, so I decided to make a simple push box game with two levels.

Although it is much simpler, it is difficult to start when you really want to do it. After thinking for a long time, it is still messy in your head, and there is no idea at all. It is better to calm down and make a simple design.

Design

As the demand is simple, go directly to the design. Pai_^

First, you must have a main application class, and I believe everyone should know it. Second, inevitably, you must have a canvas to present the screen of the program, I think it is easier to manage the map by separating the map from the canvas, and it is more convenient to add or modify the map. Then you must have the main character, right? always find a person who pushes the box; when it comes to boxes, you may need to manage the boxes.

Create an image

I have always thought that I have no aesthetic point of view, but I still need to make some pictures in the program. I can't help it. I just need to show my scalp and do it. Thanks to some simple Photoshop processing that I learned a while ago, it is not very difficult to do it, but the selected image is a little worse. Use it.

Commencement

Even though our design is simple, everything can always be used.

Draw background

Let's leave the box (boxsprite) and the box pushing (playersprite) classes aside and concentrate all the experiences on the map (which is actually the first map ).

First, you need to know several classes, such as gamecanvas, layermanager, and tiledlayer. Here we mainly use tiledlayer to construct the background.

Gamecanvas: gamecanvas is actually a printable area on the screen. However, each gamecanvas class has an independent buffer, which not only reduces the heap usage, but also can use a separate heap control program.

Layermanager class: this class is used to manage each layer of an image. It renders all layers under its control in the correct position and order.

Tiledlayer class: You can use this function to define a specific area of the game background and reuse them to generate a complete image.

For more information, see.

Main Program

Import javax. microedition. lcdui .*;

Import javax. microedition. MIDlet. MIDlet;

Public class mybox extends MIDlet implements commandlistener

{

Private boxcanvas;

Protected display;

  

Public void Startapp ()

{

Display = display. getdisplay (this );

Try {

Boxcanvas = new boxcanvas (this );

Boxcanvas. Start ();

  

Command exitcommand = new command (/"Exit/", command. Exit, 1 );

Boxcanvas. addcommand (exitcommand );

Boxcanvas. setcommandlistener (this );

} Catch (exception e ){

}

Display. setcurrent (boxcanvas );

}

  

Public display getdisplay ()

{

Return display;

}

  

Public void pauseapp (){

}

  

Public void destroyapp (Boolean unconditional ){

System. GC ();

Yydestroyed ();

}

  

Public void commandaction (command C, displayable s ){

If (C. getcommandtype () = command. Exit ){

Destroyapp (true );

Yydestroyed ();

}

}

}
  
Boxcanvas class

Import javax. microedition. lcdui .*;

Import javax. microedition. lcdui. Game .*;

Import java. util .*;

Public class boxcanvas extends gamecanvas implements runnable

{

Private mybox MIDlet;

Private boxmaps;

  

Private thread gamethread = NULL;

Private layermanager;

Private tiledlayer tl_ground;

  

Private Static final int millis_per_tick = 50;

  

Public boxcanvas (mybox MIDlet) throws exception

{

Super (true );

This. MIDlet = MIDlet;

  

Boxmaps = new boxmaps ();

Tl_ground = boxmaps. gettiled ();

  

Layermanager = new layermanager ();

Layermanager. append (tl_ground );

}

  

Public void start ()

{

Gamethread = new thread (this );

Gamethread. Start ();

}

  

Public void stop ()

{

Gamethread = NULL;

}

  

Public void run ()

{

Graphics G = getgraphics ();

  

Thread currentthread = thread. currentthread ();

Try {

While (currentthread = gamethread ){

Long starttime = system. currenttimemillis ();

If (isshown ()){

// Tick ();

// Input ();

Render (g );

}

Long timetake = system. currenttimemillis ()-starttime;

If (timetake <millis_per_tick ){

Synchronized (this ){

Wait (millis_per_tick-timetake );

}

} Else {

Currentthread. Yield ();

}

}

} Catch (interruptedexception ex ){

// Won't be thrown

}

}

  

Private void render (Graphics g ){

// G. setcolor (0xf8ddbd );

Int W = getwidth ();

Int H = getheight ();

  

Int x = (W-192)/2;

Int y = (H-192)/2;

  

G. setcolor (boxmaps. getgroundcolor ());

G. fillrect (0, 0, getwidth (), getheight ());

G. setcolor (0x0000ff );

  

Tl_ground = boxmaps. gettiled ();

  

Layermanager. Paint (G, x, y );

Flushgraphics ();

}

}
Boxmaps class

Import javax. microedition. lcdui .*;

Import javax. microedition. lcdui. Game .*;

Public class boxmaps

{

Private Static final int [] [] map1 = {

},

},

},

},

{0, 0, 0, 0, 0, 0, 0 },

},

},

},

},

},

},

{0, 0, 0, 0, 0, 3, 3, 0, 0}

};

  

Private Static final int tile_width = 16;

Private Static final int tile_height = 16;

  

Private int [] [] currentmap;

Private tiledlayer tl_boxground;

Private int groundcolor;

  

Public boxmaps () throws exception {

Setmap (1 );

}

  

Public void setmap (INT level) throws exception {

Image tileimages = image. createimage (/"/wall.png /");

  

Switch (level)

{

Case 1:

Currentmap = map1;

Groundcolor = 0xf8ddbe;

Break;

Default:

Groundcolor = 0xf8ddbe;

Break;

}

Tl_boxground = new tiledlayer (12, 12, tileimages, tile_width, tile_height );

  

For (int row = 0; row <12; row ++ ){

For (INT Col = 0; Col <12; Col ++ ){

Tl_boxground.setcell (COL, row, currentmap [row] [col]);

}

}

}

Public tiledlayer gettiled (){

Return tl_boxground;

}

Public int getgroundcolor (){

Return groundcolor;

}

}
  

Run it and check the effect. Haha, the effect is displayed. How can this problem be solved? Relatively simple

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.