Develop games using MIDP2.0 (1) Gamecanvas Foundation

xiaoxiao2021-03-06  79

MIDP2.0 provides strong support for games, through javax.microedition.lcdui.game package, there are many features that need to be written in MIDP1.0. It is now implemented as a standard API, including Gamecanvas, Sprite, Layer. and many more.

We will use MIDP2.0 to write a mobile game of a tank war, I am also a beginner J2ME soon, preparing to read the book, fighting this game! J2ME masters, please give more guidance, friends who have learned like me welcome a lot of exchanges!

Our development environment is Windows XP SP1 J2DK1.4 J2ME WTK2.1 Eclipse 3.0 Eclipseme, for how to configure Eclipse J2ME development environments, please refer to:

http://blog.9cbs.net/mingjava/archive/2004/06/23/24022.aspx

Here is the simplest Gamecanvas example, from "J2ME & Gaming":

// MyGameCanvas.java// write Canvas class import javax.microedition.lcdui *;. Import javax.microedition.lcdui.game *;. Public class MyGameCanvas extends GameCanvas implements Runnable {private boolean isPlay; // Game Loop runs when isPlay is true private long delay; // To give thread consistency private int currentX, currentY; // To hold current position of the 'X' private int width; // To hold screen width private int height; // To hold screen height // constructor and initialization public MyGameCanvas () {super (true); width = getWidth (); height = getHeight (); currentX = width / 2; currentY = height / 2; delay = 20;} // Automatically start thread for game loop Public void start () {isplay = true; new thread (this) .start ();} public void stop () {isplay = false;} // main game loop public void run () {graphics g = getGraphics (); While (isplay) {INPUT (); DrawScreen g); try {thread.sleep (DELAY);} catch (interruptedException IE) {}}} // method to handle user input private void input () {int keyStates = getKeyStates (); // Left IF ((KeyStates &) Left_pressed)! = 0) Currentx = Math.max (0, Currentx - 1); // Right IF ((KeyStates & Right_Pressed)! = 0) IF (CurrentX 5

Down_pressed)! = 0) IF (Currenty 10

After compiling, you can run in the simulator, one X in the center of the screen, you can use the upper and lower left to move it.

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

New Post(0)