J2ME小游戏源代码

时间:2015-12-24 11:20:44
【文件属性】:

文件名称:J2ME小游戏源代码

文件大小:221KB

文件格式:ZIP

更新时间:2015-12-24 11:20:44

剑士

小剑士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=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; } } }


【文件预览】:
Tales
----tmpclasses()
--------MonsterSprite.class(21KB)
--------ASprite.class(553B)
--------HeroSprite.class(9KB)
--------MapTiled.class(29KB)
--------Tales.class(2KB)
--------NPCSprite.class(1KB)
--------SFCanvas.class(23KB)
----bin()
--------MANIFEST.MF(170B)
--------Tales.jad(211B)
----src()
--------ASprite.java(436B)
--------MapTiled.java(23KB)
--------Tales.java(913B)
--------MonsterSprite.java(30KB)
--------HeroSprite.java(11KB)
--------NPCSprite.java(841B)
--------SFCanvas.java(35KB)
----tmplib()
----project.properties(47B)
----res()
--------blood.png(2KB)
--------box.png(1013B)
--------bar.png(306B)
--------npc1.png(741B)
--------load1.png(2KB)
--------mainch.png(35KB)
--------load2.png(1KB)
--------menumain.png(1KB)
--------house_01.png(6KB)
--------npc.png(1KB)
--------1212.png(9KB)
--------monster.png(32KB)
--------item.png(4KB)
--------bloodti.png(243B)
--------gold.png(727B)
--------menu.png(3KB)
--------shop.png(7KB)
--------house_02.png(3KB)
--------map2.png(13KB)
--------title.png(11KB)
----lib()
----classes()
--------MonsterSprite.class(23KB)
--------ASprite.class(553B)
--------HeroSprite.class(9KB)
--------MapTiled.class(29KB)
--------Tales.class(2KB)
--------NPCSprite.class(1KB)
--------SFCanvas.class(25KB)

网友评论

  • 多学习很有帮助,谢谢
  • 很好 学到很多东西了
  • 谢谢了,正在我的环境上重新导入运行。
  • 用编译好的每啥问题,自己编译就不成功..........
  • 谢谢了,正在我的环境上重新导入运行。
  • 谢谢,我就是要看看代码,学习学习