J2ME 2D小游戏入门之实现爆炸

 

J2ME 2D 小游戏入门之实现爆炸
 大多数游戏都有着丰富的效果类,在精灵移动类游戏中曾一度以此为一个重要的卖点。光光是一些丰富的特效是不能够产生一个好的游戏的,但是一个好的游戏是万万不能缺少好的效果的。

  很多人认为游戏的效果层有时和跟游戏逻辑本身并没有太大的关系,往往就是在最终屏幕上再画上一层效果层。但是游戏逻辑和效果层之间的通信是很重要的。这种通信往往体现在延时与等待上。比如飞机爆炸时,不接受任何用户输入,并且爆炸效果还要继续跟随飞机坠落,甚至爆炸的范围会影响周围的物体,要等待爆炸结果结束了才继续进行游戏。游戏逻辑和效果层之间的通信是很复杂的问题。在这里我突然有了罪恶感,我们没有对游戏进行任何的分析就起步了,游戏完全是基于硬编码的,我想到那儿,大家跟着看到那儿。飞机类仅仅是一个 sprite ,没有设计成一个状态机,这也就使得我们的效果层和逻辑层的通信有些卡通了。也许本文给了你编写自己第一个游戏的喜悦,也带给了你对游戏扩展性与复杂性的一丝担忧。或许这比便一个硬编码的游戏更有意义呢?谁说得好呢,现还是以为那些扩展性良好的游戏是伟大游戏构架师的杰作吧,相信你有了一两个好的想法后会重新设计这个游戏的,使之稍微有一些像个 系统 。然而好的技术不一定产生好的游戏。

  有扯远了,会到现实吧, boys and girls goon.

  描述一下我们的爆炸效果,在子弹击中飞机后,子弹要迅速消失,飞机图像保持不变,此时将爆炸效果至于飞机图像之上,然后开始显示 boom 动画,在此期间,飞机不接受任何移动指示,因为他 lose control 。在爆炸效果后飞机消失。

  我们的爆炸效果类:
GameObject explosion;//

  初始化 once
img=ImageTools.getImage("/pic/explosion.png");//
explosion=new GameObject(img,32,32);

  初始化:
explosion.reset();
explosion.lifetime=3;//
生命周期定位三桢

  逻辑处理:
if (gameover) {// 如果游戏结束,显示效果类
  explosion.paint(g);
  explosion.update();
  if(!explosion.alive){// 当生命周期结束了
   plane.alive=false;// 关闭 plane
   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);
  }
}

  现在你看我是如何解决效果层与逻辑层之间的通信的,我使用的是全局变量 gameover ,在简单游戏中使用大量的全局状态变量也是一种常见的方法 , 可以避免动脑劲。不过缺点明显,游戏硬编码,结构既不清晰也不漂亮,几乎没有扩展性。所以说最好还是将飞机基于状态机设计,并将效果类设计成含有回调函数的抽象类,然后继承效果类实现回调函数来实现通信。至于总体层次上可以用堆栈将绘画单元串起来。还有分层处理等等 给你个思考的起点

  导弹的是实现,是不是你已经有个想法了呢,其实就是利用 Bullets.killbullets

  逻辑处理
if(bomb.alive){
  bomb.moveto(plane.sprite.getX()-20,plane.sprite.getY()-20);
  bomb.paint(g);
  bomb.update();
  bullets.killbullets(plane.sprite,32);
}

  在这里我不得不提一句,将生命概念封装在 GameObject 中是很好的(其实我们只是将其用作显示关键字),但将生命周期安排在 GameObject 中有欠妥当,生命周期也不一定就是基于桢的,有时基于时间,有时还有别的什么。我是说她足够复杂到交给另一个独立类处理,在这里实际需要的是一个足够强大的显示方法,其支持以桢数为参数显示罢了。

  

  • 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、付费专栏及课程。

余额充值