Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
J2ME小游戏-fly

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

1、FlyMidlet.java

package fly;

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

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @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.java

package fly;
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import java.util.Random;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @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 < bullets.length; i++) {
      initBullet(i);
    }
  }

  private void initBullet(int i) {
    bullets[i][0] = (rnd.nextInt() & 0x7fffffff) % 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() & 0x7fffffff) % width;
        bullets[i][2] = height + 5;
        bullets[i][3] = (rnd.nextInt()) % 3; //vx
        bullets[i][4] = ( (rnd.nextInt() & 0x7fffffff) % 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 < bullets.length; i++) {
      if(bullets[i][5]==0){
        continue;
      }
      sprite.setPosition(bullets[i][1],bullets[i][2]);
      sprite.paint(g);
    }
  }

  public void refreshBullets(Sprite planesprite, boolean needcollision){
    for (int i = 0; i < bullets.length; i++) {
      if(bullets[i][5]==0){
        continue;
      }
      if(needcollision){
        //System.out.println("needcollision ");
        if (isCollision(planesprite, i, 10)) {
          //System.out.println("collision ");
          Navigate.mc.gameover = true;
          Navigate.mc.explosion.sprite.setPosition(bullets[i][1] - 16,
              bullets[i][2] - 16);
          bullets[i][5] = 0;
          continue;
        }
      }
      updata(i);
    }
  }

  private boolean isCollision(Sprite sprite,int i,int range){
    //updataspritepos(i);
    //return sprite.collidesWith(this.sprite,true);
    boolean result=false;
    int planeXCenter=sprite.getX()+12;
    int planeYCenter=sprite.getY()+12;
    int bulletXCenter=bullets[i][1]+3;
    int bulletYCenter=bullets[i][2]+3;
    if(Math.abs(planeXCenter-bulletXCenter) < range){
      if (Math.abs(planeYCenter - bulletYCenter )< range) {
        result = true;
      }
    }
    return result;
  }

  private void updataspritepos(int i){
    sprite.setPosition(bullets[i][1],bullets[i][2]);
  }

/* no use now
  public void resetDeadBullet(){
    for (int i = 0; i < bullets.length; i++) {
      if(bullets[i][5]==0){//dead bullet
        initBullet(i);
      }
    }
  }
  */

  public void killbullets(Sprite planesprite,int range){
    for (int i = 0; i < bullets.length; i++) {
      if(bullets[i][5]!=0){//alive bullets
        if(isCollision(planesprite, i, range)){
          bullets[i][5]=0;
          initBullet(i);
        }
      }
    }
  }
}
3、Font.java
package fly;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class Font {
  Sprite sprite;
  int width,height;
  int[] charhash;
  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 < charhash.length; i++) {
      charhash[i]=-1;
    }
    Character c;
    for (int i = 0; i < chars.length; i++) {
      c=new Character(chars[i]);
      charhash[c.hashCode()]=i;
    }
  }

  public void drawChar(char ch, int x, int y){
    Character c=new Character(ch);
    int hashcode=c.hashCode();
    sprite.setPosition(x,y);
    if(hashcode>=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 < length; i++) {
      drawChar(str.charAt(i),x+width*i,y);
    }
  }
}
4、GameObject.java
package fly;
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class GameObject {
  public Sprite sprite;
  public boolean alive;
  private int lifecount=0;
  public int lifetime=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 reset(){
    alive=true;
    lifecount=0;
    animcount=0;
    sprite.setFrame(0);
  }
}
5、ImageTools.java
package fly;
import javax.microedition.lcdui.*;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @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 img;
    }
  }
}
6、MyGameCanvas.java
package fly;

import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

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
  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 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
    Image img=ImageTools.getImage("/pic/MyPlaneFrames.png");
    plane=new GameObject(img,24,24);
    planedirection=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 < backcolumns*backrows; i++) {
      x=i%backcolumns;
      y=i/backcolumns;
      System.out.println("x="+x+" y="+y);
      background.setCell(x,y,1);
    }
    img=ImageTools.getImage("/pic/bullet.png");
    bullets=new Bullets(img,img.getWidth(),img.getHeight(),20,screenwidth,screenheight);
    img=ImageTools.getImage("/pic/explosion.png");
    explosion=new GameObject(img,32,32);
    bomb_ico=ImageTools.getImage("/pic/bomb_icon.png");
    img=ImageTools.getImage("/pic/b_number.png");
    fontbig=new Font(g,img,10,15,new char[]{'0','1','2','3','4','5','6','7','8','9'});
    img=ImageTools.getImage("/pic/s_number.png");
    fontsmall=new Font(g,img,5,7,new char[]{'0','1','2','3','4','5','6','7','8','9'});
    img=ImageTools.getImage("/pic/bomb.png");
    bomb=new GameObject(img,65,65);
    bombaward=new int[]{0,1,1,1,1,1};
    bombawardtop=bombaward.length-1;
    //put your init once code end
  }
  //private void InitInstance(){ }

  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();
      //put your code here
      //input();
      //gameLogic();
      //your code end
      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);
    if(bomb.alive){
      bomb.moveto(plane.sprite.getX()-20,plane.sprite.getY()-20);
      bomb.paint(g);
      bomb.update();
      bullets.killbullets(plane.sprite,32);
    }
    bullets.paint(g);
    plane.paint(g);
    bullets.refreshBullets(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(!explosion.alive){
        plane.alive=false;
        g.setColor(255,255,255);
        g.drawString(StringTools.timeOpinion(gametime),5,22,g.LEFT|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.drawString("simulate from:",2,130,g.LEFT|g.TOP);
        g.drawString("Mr.tony 's <hold on 20sec 1.20> ",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);
        }
        if(key_down){
          plane.move(0, 3);
            plane.sprite.setFrame(0);
        }
        if(key_left){
          plane.move( -3, 0);
          plane.sprite.setFrame(1);
        }
        if(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 {
        plane.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 < bombaward.length; i++) {
      bombaward[i]=1;
    }
    bombaward[0]=0;
    printInfo();
  }

  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();
       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
        planedirection=1;
        //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
        planedirection=2;
        //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
        planedirection=3;
        //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
        planedirection=4;
        //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
        planedirection=0;
        //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;
  }


}
7、Navigate.java
package fly;
import javax.microedition.lcdui.*;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @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.java
package fly;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class StringTools {
  protected StringTools() {
  }

  public static String timeOpinion(long gametime){
    if(gametime<10){
      return "Do you play with your foot?";
      //return "i can't belive,your are 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.jad
MIDlet-1: FlyMidlet, , fly.FlyMidlet
MIDlet-Jar-Size: 18499
MIDlet-Jar-URL: fly.jar
MIDlet-Name: My MIDlet Suite
MIDlet-Vendor: My Vendor
MIDlet-Version: 1.0




相关文章

相关软件