J2ME小游戏-fly

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
小剑士import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; import java.util.Random; import javax.microedition.lcdui.*; import javax.microedition.rms.RecordStore; public class HeroSprite extends ASprite{ public final int LISTENER_LEFT=4; public final int LISTENER_RIGHT=32; public final int LISTENER_UP=2; public final int LISTENER_DOWN=64; public final int LISTENER_FIRE=256; public int dir,hp,maxhp,lv,exp,nextexp,prevexp,itemlv,gold,at,df,mapdir=0; public String MyItem; public boolean isAlive,isAttach,isStop,isBattle,iisBattle,iiisBattle,isStopb,isPause=false;//是否生存以及是否攻击两个判定量 //Sprite HSprite=new Sprite(HeroImage,60,61); public int x,y,luX,luY,ruX,ruY,ldX,ldY,rdX,rdY,stopTime=5,time=0; public Random random=new Random(); public int numEmpty=0; //代表各个方向的常量 public final int DIR_LEFT=2; public final int DIR_DOWN=1; public final int DIR_RIGHT=4; public final int DIR_UP=3; public final int DIR_LU=5; public final int DIR_RU=6; public final int DIR_LD=7; public final int DIR_RD=8; public String DataName; public char itemcode[]={'0','0','0','0','0','0','0','0','0','0','0','0','0'}; //各个方向的图象序列数组 private int up_seq[]={1,2,3,4,5,6,7,8}; private int lu_seq[]={14,15,16,17,18,19,20,21}; private int ru_seq[]={40,41,42,43,44,45,46,47}; private int ld_seq[]={66,67,68,69,70,71,72,73}; private int rd_seq[]={79,80,81,82,83,84,85,86}; private int left_seq[]={92,93,94,95,96,97,98,99}; private int down_seq[]={53,54,55,56,57,58,59,60}; private int right_seq[]={27,28,29,30,31,32,33,34}; public int itemat[]={0,30,70,120,200}; public int itemdf[]={0,20,60,110,190}; private int up[]={0}; private int lu[]={13}; private int ru[]={39}; private int ld[]={65}; private int rd[]={78}; private int left[]={91}; private int down[]={52}; private int right[]={26}; //打斗时候的图象序列数组 public int downb_seq[]={9,10,11,12}; public int upb_seq[]={35,36,37,38}; public int leftb_seq[]={48,49,50,51}; public int rightb_seq[]={61,62,63,64}; private int lub_seq[]={87,88,89,90}; private int rub_seq[]={74,75,76,77}; private int ldb_seq[]={22,23,24,25}; private int rdb_seq[]={100,101,102,103}; public boolean[][] isWalkable; //构造函数 public HeroSprite(Image hImage,int height,int width){ super(hImage,height,width); x=180; y=180; luX=(int)(x/16)+1; luY=(int)(y/16); lv=1; maxhp=160+(lv-1)*40; itemlv=0; gold=0; MyItem="000000000000"; /*for(int i=0;i<12;i++){ itemcode[i]='0'; }*/ //itemcode={'0','0','0','0','0','0','0','0','0','0','0','0','0'}; exp=210; hp=maxhp; at=30; df=20; prevexp=lv*lv*60-(lv-1)*(lv-1)*60+150; nextexp=prevexp+(lv-1)*(lv-1)*60-(lv-2)*(lv-2)*60+150; System.out.print("已装载英雄类"); this.setFrameSequence(down); isAlive=true; System.out.print("英雄类装载完毕"); } public byte[] chgTorms(boolean isFirst) throws IOException{ ByteArrayOutputStream baos=new ByteArrayOutputStream(); DataOutputStream dos=new DataOutputStream(baos); if(itemcode!=null){ MyItem=String.valueOf(itemcode); } dos.writeUTF(String.valueOf(lv));//将具体数据写入流 dos.writeUTF(String.valueOf(itemlv)); dos.writeUTF(String.valueOf(gold)); dos.writeUTF(String.valueOf(MyItem)); dos.writeUTF(String.valueOf(exp)); if(isFirst==false){//如果不是第一次读入数据则计算攻防 at=itemat[itemlv]+(lv-1)*8+30; df=itemdf[itemlv]+(lv-1)*10+20; } baos.close(); dos.close(); numEmpty=0; if(isFirst==false){ for(int i=0;i<itemcode.length;i++){//如果不是第一次读入数据则计算空格数 if(itemcode[i]=='0'){ numEmpty++; } } } return baos.toByteArray(); } public void doMonster(int myhp,int deadlv,RecordStore rs,int recordid){ this.hp=myhp; if(lv<=6){ prevexp=lv*lv*60-(lv-1)*(lv-1)*60+150;//经验值公式 nextexp=prevexp+(lv-1)*(lv-1)*60-(lv-2)*(lv-2)*60+150; }else{ prevexp=lv*(lv-5)*(lv-5)-40*(lv-4)*(lv-4)+150; nextexp=prevexp+50*(lv+1)*(lv-4)*(lv-4)-40*(lv-3)*(lv-3)+150; } if(deadlv!=0){ exp=exp+deadlv*15+random.nextInt()%5;//怪物死亡时候等级和经验值的换算公式,nextexp下一等级需要的经验,deadlv被杀死怪物的等级 if(exp>=nextexp){//目前设定的最高等级为11级 if(lv<12){ lv++; } at=itemat[itemlv]+(lv-1)*8+30; df=itemdf[itemlv]+(lv-1)*10+20; maxhp=160+(lv-1)*40; hp=maxhp; } try{ rs.setRecord(recordid,chgTorms(false),0,chgTorms(false).length); }catch(Exception e){System.out.println(e);} isBattle=false; } } private void getNowHanglie(){//获得主角的行列数值 luX=(int)(x/16)+1; luY=(int)(y/16); ruX=(int)((x+40)/16)+1; ruY=luY; ldX=luX; ldY=(int)((y+40)/16); rdX=ldX; rdY=ldY; } public void chgMapbool(boolean[][] newbool){ this.isWalkable=newbool; } public void doMove(int keyState){ //System.out.println(ldX+","+ldY); if(hp<=0){ this.isAlive=false; } if(this.isAlive){ getNowHanglie(); if(isPause==false){ switch(keyState){ case LISTENER_UP: if(dir!=DIR_UP){//如果按下UP键时之前的状态不是朝UP的话,则改变状态 dir=DIR_UP; this.setFrameSequence(up_seq); } if(isWalkable[ldX][ldY-1] && isWalkable[rdX][rdY-1]){//对下一步的判断 if(isBattle==true){//按下UP键时候如果是打斗状态则转化为UP奔跑帧 this.setFrameSequence(up_seq); isBattle=false; } if(isStop==true){//如果是停止状态也转化为UP奔跑帧 this.setFrameSequence(up_seq); isStop=false; } y=y-5;//坐标变化 this.nextFrame();//帧变化 } break; case LISTENER_DOWN: if(dir!=DIR_DOWN){ dir=DIR_DOWN; this.setFrameSequence(down_seq); } if(isBattle==true){ this.setFrameSequence(down_seq); isBattle=false; } if(isStop==true){ this.setFrameSequence(down_seq); isStop=false; } if(isWalkable[ldX][ldY+1] && isWalkable[rdX][rdY+1]){ y=y+5; this.nextFrame(); } break; case LISTENER_LEFT: if(dir!=DIR_LEFT){ dir=DIR_LEFT; this.setFrameSequence(left_seq); } if(isBattle==true){ this.setFrameSequence(left_seq); isBattle=false; } if(isStop==true){ this.setFrameSequence(left_seq); isStop=false; } if(isWalkable[ldX-1][ldY]){ x=x-5; this.nextFrame(); } break; case LISTENER_RIGHT: if(dir!=DIR_RIGHT){ dir=DIR_RIGHT; this.setFrameSequence(right_seq); } if(isBattle==true){ this.setFrameSequence(right_seq); isBattle=false; } if(isStop==true){ this.setFrameSequence(right_seq); isStop=false; } if(isWalkable[rdX+1][rdY]){ x=x+5; this.nextFrame(); } break; case LISTENER_LEFT+LISTENER_UP: if(dir!=DIR_LU){ dir=DIR_LU; this.setFrameSequence(lu_seq); } if(isBattle==true){ this.setFrameSequence(lu_seq); isBattle=false; } if(isStop==true){ this.setFrameSequence(lu_seq); isStop=false; } if(isWalkable[ldX-1][ldY] && isWalkable[ldX][ldY-1] && isWalkable[rdX][rdY-1]){ x=x-3; y=y-3; this.nextFrame(); } break; case LISTENER_RIGHT+LISTENER_UP: if(dir!=DIR_RU){ dir=DIR_RU; this.setFrameSequence(ru_seq); } if(isBattle==true){ this.setFrameSequence(ru_seq); isBattle=false; } if(isStop==true){ this.setFrameSequence(ru_seq); isStop=false; } if(isWalkable[ldX][ldY-1] && isWalkable[rdX][rdY-1] && isWalkable[rdX+1][rdY]){ x=x+3; y=y-3; this.nextFrame(); } break; case LISTENER_LEFT+LISTENER_DOWN: if(dir!=DIR_LD){ dir=DIR_LD; this.setFrameSequence(ld_seq); } if(isBattle==true){ this.setFrameSequence(ld_seq); isBattle=false; } if(isStop==true){ this.setFrameSequence(ld_seq); isStop=false; } if(isWalkable[ldX-1][ldY] && isWalkable[ldX][ldY+1] && isWalkable[rdX][rdY+1]){ x=x-3; y=y+3; this.nextFrame(); } break; case LISTENER_RIGHT+LISTENER_DOWN: if(dir!=DIR_RD){ dir=DIR_RD; this.setFrameSequence(rd_seq); } if(isBattle==true){ this.setFrameSequence(rd_seq); isBattle=false; } if(isStop==true){ this.setFrameSequence(rd_seq); isStop=false; } if(isWalkable[rdX+1][rdY] && isWalkable[ldX][ldY+1] && isWalkable[rdX][rdY+1]){ x=x+3; y=y+3; this.nextFrame(); } break; case 0: if(isBattle==false){ isStop=true; switch(dir){ case DIR_DOWN: this.setFrameSequence(down); break; case DIR_LEFT: this.setFrameSequence(left); break; case DIR_UP: this.setFrameSequence(up); break; case DIR_RIGHT: this.setFrameSequence(right); break; case DIR_LU: this.setFrameSequence(lu); break; case DIR_RU: this.setFrameSequence(ru); break; case DIR_RD: this.setFrameSequence(rd); break; case DIR_LD: this.setFrameSequence(ld); break; } } break; case LISTENER_FIRE: if(mapdir==1){ switch(dir){ case 1: if(isBattle==false){ this.setFrameSequence(downb_seq); } break; case 2: if(isBattle==false){ this.setFrameSequence(leftb_seq); } break; case 3: if(isBattle==false){ this.setFrameSequence(upb_seq); } break; case 4: if(isBattle==false){ this.setFrameSequence(rightb_seq); } break; case DIR_LU: if(isBattle==false){ this.setFrameSequence(lub_seq); } break; case DIR_LD: if(isBattle==false){ this.setFrameSequence(ldb_seq); } break; case DIR_RU: if(isBattle==false){ this.setFrameSequence(rub_seq); } break; case DIR_RD: if(isBattle==false){ this.setFrameSequence(rdb_seq); } break; } isBattle=true; break; } } if(isBattle==true){ //如果是打斗状态 if(this.getFrame()==2){//如果是打斗状态的第二帧 iiisBattle=true;//打中状态置于真 }else{ iiisBattle=false; } this.nextFrame(); } if(iiisBattle && isBattle){ iisBattle=true; }else{ iisBattle=false; } dx=x; dy=y; } }else{ if(this.exp>150){ this.exp=this.exp-150; }else this.exp=0; } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值