J2ME game -Fly

xiaoxiao2021-03-06  39

1, flymidlet.javaPackage fly;

Import javax.microedition.midlet. *; import javax.microedition.lcdui. *; import java.util. *;

/ ** *

Title: *

Description: *

Copyright: Copyright (c) 2004 *

Company: * @ Author not attributable * @version 1.0 * /

public class FlyMidlet extends MIDlet {Navigate ng; public FlyMidlet () {ng = Navigate.getInstance (this);} protected void startApp () {System.out.println ( "startApp"); ng.display.setCurrent (ng.mc ); Printinfo ();} protected void pauseapp () {system.out.println ("pauseApp");} protected void destroyApp (boolean parm1) {system.out.println ("destroyApp"); navigate.mc.stop ); Mygamecanvas.cleanjob (); navigate.cleanjob ();

Private void printinfo () {system.out.println ("Flymidlet Printinfo () Start:"); System.out.Println ("Flymidlet Printinfo () end:");}

} 2, Bullets.javaPackage fly; import javax.microedition.lcdui.game. *; Import javax.microedition.lcdui. *; Import java.util.random; / ** *

title: * < P> Description: *

Copyright: CopyRight (c) 2004 *

company: * @Author not attributable * @version 1.0 * /

public class Bullets extends GameObject {private int [] [] bullets; private int bulletstotal; private Random rnd; public static final int BULLET_TYPE_LEFT = 0; public static final int BULLET_TYPE_RIGHT = 1; public static final int BULLET_TYPE_TOP = 2; public static final int Bullet_type_bottom = 3; Private Int Width, Height;

public Bullets (Image img, int picwidth, int picheight, int bulletstotal, int width, int height) {super (img, picwidth, picheight); this.bulletstotal = bulletstotal; bullets = new int [bulletstotal] [6]; rnd = New random (); this.width = width; this.height = height;} public void initBullets () {for (int i = 0; i

Private void initBullet (INT i) {bullets [i] [0] = (Rnd.nextint () & 0x7ffffff)% 4; // Type Bullets [i] [5] = 1; // Alive Switch (Bullets [i] [0]) {CASE BULLET_TYPE_LEFT: BULLETS [I] [1] = -5; Bullets [i] [2] = (rnd.nextint () & 0x7fffffff)% Height; Bullets [i] [3] = (RND. Nextint () & 0x7fffffff)% 3 1; // vx bullets [i] [4] = (Rnd.nextint ())% 3; // vy break; case bullet_type_right: bullets [i] [1] = width 5; Bullets [i] [2] = (rnd.nextint () & 0x7fffffff)% Height; Bullets [i] [3] = ((Rnd.nextINT () & 0x7fffffff)% 3 1) * -1; / / vx bullets [i] [4] = (rnd.nextint ())% 3; // vy break; case bullet_type_top: bullets [i] [1] = (rnd.nextint () & 0x7fffffff)% width; bullets [ I] [2] = -5; Bullets [i] [3] = (rnd.nextint ())% 3; // vx bullets [i] [4] = (rnd.nextint () & 0x7fffffff)% 3 1; // Vy Break; case bullet_type_bottom: Bullets [i] [1] = (rnd.nextint () & 0x7ffffff)% Width; Bullets [i] [2] = height 5; Bullets [i] [3] = (rnd.nextint ())% 3; // vx bullets [i] [4] = ((rnd.nextint () & 0x7ffffffffFFFFFFFF )% 3 1) * -1; // vy Break;}}

Public void Updata (INT i) {bullets [i] [1] = bullets [i] [3]; Bullets [i] [2] = bullets [i] [4]; if (Bullets [i] [1 ] <5 || Bullets [i] [1]> Width 5) {Bullets [i] [3] * = - 1;} if (Bullets [i] [2] <- 5 || Bullets [i] [2]> Height 5) {Bullets [i] [4] * = - 1;}} Private Void Paint (Graphics G, INT i) {UpdataSpritePos (i); sprite.paint (g);}

Public void Paint (GRAPHICS G) {for (int i = 0; i

Public void refreshbullets (sprite planesprite, boolean nesedcollision) {for (int i = 0; i

Private Boolean IsCollision (Sprite Sprite, Int i, int RANGE) {// UpdataaspritePos (i); // Return Sprite.collideswith (this.sprite, true); boolean result = false; int shapexcenter = sprite.getx () 12 ; Int shape () 12; int bulletxcenter = bullets [i] [1] 3; int BulletyCenter = BULLETS [i] [2] 3; if (Math.abs (PlanexCenter-BulletXCenter)

/ * no use now public void resetdeadbullet () {for (int i = 0; i

Public void killbullets (sprite planesprite, int {for (int i = 0; i Title: *

description: *

Copyright: Copyright (c) 2004 *

Company: * @Author not attributable * @version 1.0 * /

Public class font {sprite sprite; int width, height; int width; graphics g;

Public Font (Graphics G, Image Img, Int Width, Int Height, CHAR [] Chars) {this.g = g; sprite = new sprite (img, width, height); this.width = width; this.height = height Charhash = new int [128]; for (int i = 0; i = 0) {sprite.setframe (charhash [hashcode]); sprite.paint (g);}}

Public void DrawString (String Str, INT X, INT Y) {INT Length = Str.Length (); for (INT I = 0; I title: * < P> Description: *

Copyright: CopyRight (c) 2004 *

company: * @Author not attributable * @version 1.0 * /

Public class gameObject {public sprite sprite; public boolean alive; private int life = 0; public int life = 0; public int speed = 0; private Int animcount = 0;

Public GameObject (Image Img, Int Width, Int Height) {sprite = new sprite (IMG, Width, Height); reset ();

Public void move (int dx, int dy) {sprite.move (dx, dy);

Public void moveto (int x, int y) {sprite.setposition (x, y);

Public void update () {if (! alive) return; if ( animcount> speed) {animcount = 0; sprite.nextframe (); if (lifetime! = 0 && lifecount> Lifetime) alive = false;} } public void Paint (graphics g) {if (! alive) return; sprite.paint (g);} public void recset () {alive = true; life; = 0; Animcount = 0; sprite.setframe (0);} } 5, imagetools.javaPackage fly; import javax.microedition.lcdui. *; / ** *

Title: *

Description: *

Copyright: CopyRight (c) 2004 *

Company: * @Author not attributable * @version 1.0 * /

Public class imagetools {protected imagetools () {}

Public static image getimage (string str) {image img = null; try {img = image.createImage (STR);} catch (exception ex) {system.out.println (ex);} finally {return;}}} 6, mygamecanvas.javaPackage fly;

Import javax.microedition.lcdui.game.gamecanvas; import javax.microedition.lcdui. *; import javax.microedition.lcdui.game. *;

/ ** *

Title: *

Description: *

Copyright: Copyright (c) 2004 *

Company: * @ Author not attributable * @version 1.0 * /

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 long gametimeoffset; long gametime; int bombnum; int [] bombaward; int bombawardtop; GameObject plane; int planedirection; TiledLayer background; Bullets bullets; GameObject explosion; GameObject bomb; Image bomb_ico; Font fontbig, fontsmall; // define your variable endprotected MyGameCanvas () {super (true); g = getGraphics (); running = false; t = null; addCommand (startcmd = new Command ( "start" , Command.ok, 1)); addcommand ("exit", command.exit, 1); setcommandlistener (this); screenwidth = getWidth (); screenheight = getHeight ();

// put your init overce code here image img = imagetools.getimage ("/ pic / myplaneframes.png"); Plane = New GameObject (IMG, 24, 24); pladeIRection = 0; img = imagetools.getimage ("/ PIC /back_water.png "); int backcolumns = screenwidth / img.getWidth () 1; int backrows = screenheight / img.getHeight () 1; background = new TiledLayer (backcolumns, backrows, img, img.getWidth (), IMG.GetHeight ()); int x, y; for (int i = 0; i

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 (running) {st = system.currenttimemillis (); // PUT Your Code Here // Input (); // Gamelogic (); // Your Code End GameInput (); GameMain (); et = system.currenttimemillis (); DIFF = ET-ST; IF (Diff

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); if (bomb.alive) { Bomb.moveto (Plane.Sprite.getx () - 20, Plane.sprite.Gety () - 20); Bomb.Paint (g); Bomb.Ullets.killbullts (Plane.Sprite, 32);} Bullets.Paint (g); Plane.Paint (g); Bullets.Refreshbullts (Plane.Sprite,! GameOver &&! Bomb.Alive); g.drawImage (Bomb_ico, 0, ScreenHeight-1, G.BOTTOM | G.LEFT ); fontbig.drawString (String.valueOf (gametime), screenwidth / 2-15,10); fontsmall.drawString (String.valueOf (bombnum), bomb_ico.getWidth (), screenheight-fontsmall.height); if (gameover) {Explosion.paint (g); explosion.Update (); if (! Expenented.alive) {Plane.alive = false; G.SetColor (255, 255, 255); g.drawstring (StringTools.Timeopinion (Gametime), 5,22, g.d | g.top); g.drawstring ("fly 0.1 ver by favo yang", 2,100, g.left | g.top); g.drawstring ("e-mail: favoyang@yahoo.com", 2,115 , g.LEFT | g.top; g.draws TRING ("Simulate from:", 2,130, g.left | g.top; g.drawstring ("Mr. Tony 'S ", 2, 145, g.LEFT | G.TOP); g. DrawString ("Hello Tony, Just Funny.", 2, 160, G.LEFT | G.TOP);}} else {Gametime = (System.currentTimeMillis () - GametimeOffset / 1000; int awardIndex = (int) Gametime / 20; if (awardindex> bombawardtop) awardindex = bombawardtop; if (bombaward [awardindex] = 0!) {bombnum = bombaward [awardindex]; bombaward [awardindex] = 0;} if (keyevent) {if (key_up) {plane.move ( 0, -3); Plane.Sprite.setFrame (0);}} (key_down) {Plane.move (0, 3);

Plane.SPRITE.SETFRAME (0);} if (key_left) {Plane.move (-3, 0); Plane.Sprite.SetFrame (1);}} (key_right) {Plane.Move (3, 0); Plane .sprite.setframe (2);} if (key_fire) {if (! Bomb.alive && Bombnum> 0) {// bomb isn't actived and there's enough bomb. Bomb.Reset (); bomb.alive = true; Bombnum--;}}} else {plan.sprite.setframe (0);}}

Flushgraphics ();

private void gameInit () {gameover = false; gametime = 0; gametimeoffset = System.currentTimeMillis (); allowinput = true; key_up = key_down = key_left = key_right = key_fire = false; plane.moveto ((screenwidth-plane.sprite.getWidth () / 2 (ScreenHeight-Plane.Sprite.getHeight ()) / 2); Bullets.initBullets (); Plane.Reset (); Explosion.Reset (); Explosion.lifetime = 3; Bomb.Reset () Bomb.lifetime = 6; bomb.alive = false; bombnum = 3; for (int i = 0; i

Public void stop () {if (running) {Running = false;}}

Private void printinfo () {system.out.println ("MyGamecanvas Printinfo () Start:"); System.out.Println ("Width:" getWidth () "Height:" GetHeight ()); java.lang .Runtime RT = java.lang.Runtime.Getruntime (); System.out.println ("Total Memory:" rt.totalmemory ()); System.out.println ("Free Memory:" rt.FreeMemory () System.out.println ("MyGamecanvas Printinfo () end:");

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 (cmdstr.equals ( "exit")) {stop (); Navigate.midlet.destroyApp (false); Navigate.midlet.notifyDestroyed ();}} private void gameinput () {if (allowinput) {keystate = getKeyStates (); KeyEvent = false; = 0) {// up key_up = true; keyevent = true; // deal your unstop job code herentirection = 1; //system.out.println ( "UP Press"); // deal your unstop job code end End} else f ((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 Releas e "); // 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 4; //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 YOONE PRESS-One Job Code End}}

IF (KeyState & Left_PRESSED! = 0) {// Left key_left = true; keyevent = true; // DEAL Your unstop job code here 4 = 3; //system.out.println ("left press "); / / deal your unstop job code end 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 YOONE PRESS-One Job Code End}} f ((KeyState & Right_Pressed)! = 0) {// Right Key_right = true; keyevent = true; ////// DEAL YOUR unstop job code Here flatirection = 4; //system.out.println ("right press "); // deal your unstop job code end} else f ((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 flatirection = 0; //system.out.println ("Fire Press "); / / deal your unstop job code end 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;}

} 7, navigate.javaPackage fly; import javax.microedition.lcdui. *; / ** *

Title: *

description: *

Copyright: CopyRight (c) 2004 *

Company: * @author not attributable * @version 1.0 * / public class Navigate {private static Navigate instance; public static MyGameCanvas mc; public static FlyMidlet midlet; public static Display display;

Protected navigate (flymidlet midlet) {navigate.midlet = midlet; navigate.mc = mygamecanvas.getInstance (); navigate.display = display.getdisplay (midlet);}

SYNCHRONIZED PUBLIC Static Navigate GetInstance (Flymidlet MIDlet) {IF (Instance == Null) {Instance = New NaviGate (MIDlet); System.out.Println ("New NaviGate");} Return Instance;

Public static void cleanjob () {instance = null;}} 8, StringTools.javaPackage fly;

/ ** *

Title: *

Description: *

Copyright: Copyright (c) 2004 *

Company: * @ Author not attributable * @version 1.0 * / public class stringtools {protected stringtools () {}

Public Static String Timeopinion (LONG GAMTIME) {IF (Gametime <10) {Return "Do you play with your foot?"; // Return "I can't Belive, Your a Game Master";} else if (Gametime < 16) {Return "Come Boy, You Can do it!";} Else IF (Gametime <20) {Return "What a pity! Try again."; Else IF (Gametime <25) {return "Very Well, you Are A Real Man. "} else if (Gametime <30) {Return" i know you have talent of this game ";} else if (Gametime <40) {return" i can't Belive, Your Are A Game Master. ";} else {return" oh my god, are you a human? ";}}} 9.Fly.jadmidlet-1: flymidlet, fly.flymidletmidlet-jar-size: 18499midlet-jar-url: fly. JARMIDLET-NAME: my midlet suitemidlet-vendor: my vendormidlet-version: 1.0

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

New Post(0)