J2ME手机游戏设计案例源代码GameEngineRecord_第1页
J2ME手机游戏设计案例源代码GameEngineRecord_第2页
J2ME手机游戏设计案例源代码GameEngineRecord_第3页
J2ME手机游戏设计案例源代码GameEngineRecord_第4页
J2ME手机游戏设计案例源代码GameEngineRecord_第5页
已阅读5页,还剩34页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、*gamemid。java/* * to change this template, choose tools | templates * and open the template in the editor. */import javax.microedition.lcdui.display;import javax.microedition.midlet.*;/* * author administrator */public class gamemid extends midlet private display display; private gameworld gw=null;

2、public gamemid() display = display.getdisplay(this); /获取display gw=new gameworld(this); /创建游戏画布 display.setcurrent(gw); /设置游戏画布为当前显示画面 public void startapp() if(gw!=null) gw.start(); /游戏开始执行 public void pauseapp() if(gw!=null)gw.setpaused(true); /游戏暂停执行 public void destroyapp(boolean unconditional)

3、if(gw!=null)gw.stop(); /停止游戏循环 /* 退出程序 */ public void exit() try destroyapp(false); catch(exception e) notifydestroyed(); *gamedesign。java/* * to change this template, choose tools | templates * and open the template in the editor. */import javax.microedition.midlet.*;import javax.microedition.lcdui

4、.*;import javax.microedition.lcdui.game.*;import java.io.ioexception;/* * author administrator */public class gamedesign /gen-begin:|fields|0| private image tiledbg2; private image tiledbg; public int fireseq001delay = 200; public int fireseq001 = 34, 35, 36, 35; private tiledlayer background; publi

5、c int firebackground; private tiledlayer build; /gen-end:|fields|0| /gen-begin:|methods|0| /gen-end:|methods|0| public image gettiledbg() throws java.io.ioexception /gen-begin:|1-getter|0|1-preinit if (tiledbg = null) /gen-end:|1-getter|0|1-preinit / write pre-init user code here tiledbg = image.cre

6、ateimage(/tiled.png);/gen-begin:|1-getter|1|1-postinit /gen-end:|1-getter|1|1-postinit / write post-init user code here return this.tiledbg;/gen-begin:|1-getter|2| /gen-end:|1-getter|2| public tiledlayer getbackground() throws java.io.ioexception /gen-begin:|2-getter|0|2-preinit if (background = nul

7、l) /gen-end:|2-getter|0|2-preinit / write pre-init user code here background = new tiledlayer(20, 20, gettiledbg(), 32, 32);/gen-begin:|2-getter|1|2-midinit firebackground = background.createanimatedtile(fireseq0010); int tiles = 2, 2, 1, 2, 2, 2, 2, 17, firebackground, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,

8、 2 , 2, 2, 2, 2, 2, 2, 2, 26, 11, 11, 2, 1, 6, 2, 2, 2, 2, 2, 2, 2 , 10, 10, 10, 12, 7, 2, 2, 17, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 , 2, 2, firebackground, 17, 2, 2, 2, 17, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 , 6, 2, 2, 17, 2, 2, 2, 17, 2, 7, 3, 2, 2, 2, 2, 2, 3, 2, 2, 2 , 2, 2, 2, 17, 2, 2, 2, 17,

9、2, 2, 2, 2, 2, 2, 2, 2, 17, 2, 2, 2 , 2, 2, 2, 19, 11, 11, 11, 18, 11, 10, 12, 2, 2, 2, 2, 2, 17, 2, 2, 2 , 2, 2, 2, 2, 2, 2, 2, 2, 2, firebackground, 17, 2, 2, 2, 2, 2, 17, 2, 2, 2 , 1, 2, 2, 2, 2, 2, 2, 6, 2, 2, 17, 2, 2, 2, 2, 2, 17, 2, 6, 2 , 3, 3, 3, 3, 7, 3, 3, 3, 3, 3, 17, 3, 1, 3, 3, 3, 17,

10、3, 3, 3 , 2, 6, firebackground, 2, 2, 2, 4, 4, 4, 4, 17, 4, 3, 3, 3, 3, 17, 3, 3, 3 , 2, 3, 9, 10, 10, 10, 10, 10, 10, 10, 18, 10, 10, 27, 10, 10, 18, 10, 10, 10 , 2, 2, 17, 2, 2, 2, 6, 4, 4, 4, 4, 4, 3, 17, 3, 3, 3, 3, 3, firebackground , 2, 2, 17, 2, 2, 2, 4, 4, 4, 4, 4, 4, 3, 17, 3, 1, 3, 3, 3, 3

11、 , 2, 2, 17, 2, 2, 2, 4, 4, 4, 4, 4, 4, 3, 26, 11, 11, 11, 3, 3, 3 , 2, 2, 17, 2, 2, 2, 4, 4, 4, 4, 4, 4, 3, 17, 3, 3, 3, 3, 3, 3 , 2, 2, 17, 2, 1, 2, 4, 4, 4, 4, 17, 4, 3, 17, 3, 3, 3, 3, 3, 3 , 2, 2, 17, firebackground, 2, 2, 4, 4, 4, 4, 19, 10, 10, 20, 3, 3, 3, 3, 3, 3 , 2, 2, 17, 2, 2, 2, 4, 7,

12、4, 4, 4, 3, 3, 3, 3, 6, 3, 3, 1, 3 , 6, 2, 17, 2, 2, 2, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3 ;/gen-end:|2-getter|1|2-midinit / write mid-init user code here for (int row = 0; row 20; row+) /gen-begin:|2-getter|2|2-postinit for (int col = 0; col 20; col+) background.setcell(col, row, tilesrowcol)

13、; /gen-end:|2-getter|2|2-postinit / write post-init user code here return background;/gen-begin:|2-getter|3| /gen-end:|2-getter|3| public tiledlayer getbuild() throws java.io.ioexception /gen-begin:|6-getter|0|6-preinit if (build = null) /gen-end:|6-getter|0|6-preinit / write pre-init user code here

14、 build = new tiledlayer(20, 20, gettiledbg(), 32, 32);/gen-begin:|6-getter|1|6-midinit int tiles = 0, 0, 0, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 30, 13, 0, 0, 69, 70, 71, 72, 0, 0 , 0, 43,

15、0, 0, 0, 31, 32, 0, 0, 0, 0, 0, 0, 0, 77, 78, 79, 80, 0, 0 , 0, 51, 0, 0, 0, 39, 40, 0, 0, 0, 0, 0, 0, 0, 85, 86, 87, 88, 13, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 93, 94, 95, 96, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 15,

16、0, 0, 0, 0, 0, 0 , 0, 37, 38, 0, 0, 0, 0, 44, 45, 46, 0, 0, 0, 31, 32, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 52, 53, 54, 0, 0, 0, 39, 40, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 60, 61, 62, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0

17、, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 65, 66, 67, 68, 0, 0, 0, 0, 0, 0, 43, 0, 0 , 13, 0, 0, 0, 0, 0, 0, 73, 74, 75, 76, 0, 0, 0, 0, 0, 0, 51, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 81, 82, 83, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 89, 90, 91, 92, 0, 0, 0, 0, 0, 0, 0, 0, 1

18、3 , 0, 16, 0, 0, 0, 0, 0, 14, 15, 0, 0, 0, 0, 0, 0, 0, 41, 42, 0, 0 , 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 49, 50, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ;/gen-end:|6-getter|1|6-midinit / write mid-init user code here for (int row = 0; row 20; row+) /gen-begin:

19、|6-getter|2|6-postinit for (int col = 0; col 20; col+) build.setcell(col, row, tilesrowcol); /gen-end:|6-getter|2|6-postinit / write post-init user code here return build;/gen-begin:|6-getter|3| /gen-end:|6-getter|3| public void updatelayermanagerforscene(layermanager lm) throws java.io.ioexception

20、/gen-line:|7-updatelayermanager|0|7-preupdate / write pre-update user code here getbuild().setposition(0, 0);/gen-begin:|7-updatelayermanager|1|7-postupdate getbuild().setvisible(true); lm.append(getbuild(); getbackground().setposition(0, 0); getbackground().setvisible(true); lm.append(getbackground

21、();/gen-end:|7-updatelayermanager|1|7-postupdate / write post-update user code here /gen-begin:|7-updatelayermanager|2|/gen-end:|7-updatelayermanager|2| public image gettiledbg2() throws java.io.ioexception /gen-begin:|17-getter|0|17-preinit if (tiledbg2 = null) /gen-end:|17-getter|0|17-preinit / wr

22、ite pre-init user code here tiledbg2 = image.createimage(/tiled.png);/gen-begin:|17-getter|1|17-postinit /gen-end:|17-getter|1|17-postinit / write post-init user code here return this.tiledbg2;/gen-begin:|17-getter|2| /gen-end:|17-getter|2| *gameworld。javaimport java.io.ioexception;import java.io.in

23、putstream;import java.util.date;import javax.microedition.lcdui.font;import javax.microedition.lcdui.graphics;import javax.microedition.lcdui.image;import javax.microedition.lcdui.game.gamecanvas;import javax.microedition.lcdui.game.layermanager;import javax.microedition.lcdui.game.sprite;import jav

24、ax.microedition.lcdui.game.tiledlayer;import javax.microedition.media.manager;import javax.microedition.media.mediaexception;import javax.microedition.media.player;import javax.microedition.media.control.volumecontrol;/* * to change this template, choose tools | templates * and open the template in

25、the editor. */* * * author administrator */public class gameworld extends gamecanvas implements runnable gamemid mid; /游戏midlet thread mainthread; /游戏主线程 private final int ms_per_frame=30; /设置一次游戏循环所需时间,单位为毫秒(ms),每秒游戏帧数约为1000/ms_per_frame private int cps; /游戏帧速 private boolean ispaused; /游戏暂停状态 priv

26、ate boolean running; /游戏运行状态 graphics g; /游戏绘图对象 private int screenwidth; /游戏屏幕宽度 private int screenheight; /游戏屏幕高度 public static int gamestate; /游戏状态,1为暂停,2为游戏进行中,3为游戏失败 public static int keystate; /按键状态 private image imgenemy,imgboy;/精灵图像 private sprite sprenemy; /定义敌人精灵 private mysprite sprboy; /

27、用扩展的精灵类来定义玩家精灵 /定义三个不同方向的玩家精灵帧序列,右方向可由左方向镜像变换得到 int lsequ=3,4,3,5; /定义左方向精灵帧序列 int usequ=6,7,6,8; /定义上方向精灵帧序列 int dsequ=0,1,0,2; /定义下方向精灵帧序列 int enemyx,enemyy; /敌人精灵坐标 int enemyspeedx; /敌人精灵速度,这里设置为只能水平移动 boolean iscoll; /碰撞标志 long colltexttime; /碰撞文本显示时间 gamedesign gd; /游戏设计器 layermanager lm; /图层管理

28、器 tiledlayer tlbg; /背景图层 tiledlayer tlbuild; /障碍物层 int fireindex; /动态贴图索引 int firetimes; /动态贴图延时 int viewportx, viewporty; /视口坐标 private final int min_buffer=30; /人物与贴图边缘的距离 private player pmusic,pcoll; /声明播放器 private inputstream inmenu,incoll; /声明输入流 private volumecontrol control; /声明音量控制器 private

29、int rsflag; /记录暂停时的动作/定义字体private font largefont=font.getfont(font.face_system,font.style_bold, font.size_large); private font mediumfont=font.getfont(font.face_system,font.style_bold, font.size_medium); public gameworld(gamemid midlet) super(true); this.mid=midlet; /获取midlet this.setfullscreenmode(

30、true); /设置为全屏模式 screenwidth=getwidth(); /获取屏幕大小 screenheight=getheight(); g=this.getgraphics(); /获取绘图对象 running=false; /设置游戏运行状态 ispaused=false; gameinit(); /游戏初始化 /* 游戏初始化 */ private void gameinit() gd=new gamedesign(); lm=new layermanager(); try tlbg = gd.getbackground(); tlbuild=gd.getbuild(); gd

31、.updatelayermanagerforscene(lm); catch (ioexception ex) ex.printstacktrace(); try imgboy = image.createimage(/boy.png); imgenemy=image.createimage(/enemy.png); catch (ioexception ex) ex.printstacktrace(); / 使用image对象,帧宽度、帧高度,速度,限制范围矩形左上角的x坐标、y坐标、高度和宽度来构造自定义精灵 sprboy=new mysprite(imgboy,32,48,3,min_b

32、uffer,min_buffer,screenwidth-min_buffer*2,screenheight-min_buffer*2); sprboy.setframesequence(lsequ); /设置初始化时的精灵帧序列和方向 sprboy.setdir( mysprite.left); /设置初始化时精灵方向 sprboy.definereferencepixel(sprboy.getwidth()/2, sprboy.getheight()/2); /设置参考点 sprboy.setrefpixelposition(100, 100); /通过参考点定位精灵 int sw=spr

33、boy.getwidth(); int sh=sprboy.getheight(); sprboy.definecollisionrectangle(sw/10, sh/10, sw*8/10,sh*8/10); /重设精灵的碰撞矩形范围为原来的80 /创建敌人精灵 sprenemy=new sprite(imgenemy,imgenemy.getwidth()/3,imgenemy.getheight()/3); sprenemy.setframesequence(lsequ); /设置精灵循环序列 enemyx=50; /设置敌人精灵的坐标 enemyy=200; enemyspeedx=

34、-2; /设置敌人精灵速度 gd=new gamedesign(); /创建游戏设计器 lm=new layermanager(); /创建层管理器,管理场景 try tlbg = gd.getbackground(); /创建背景贴图层 tlbuild=gd.getbuild(); /创建障碍物贴图层 gd.updatelayermanagerforscene(lm); /设置图层管理器 catch (ioexception ex) ex.printstacktrace(); lm.insert(sprboy,1); /添加精灵到场景中 lm.insert(sprenemy,2); view

35、portx=0; /初始化视口坐标 viewporty=0; /设置视口 lm.setviewwindow(viewportx, viewporty, screenwidth,screenheight); fireindex=0; /初始化动态贴图参数 firetimes=0; try inmenu = getclass().getresourceasstream(/music.mid); /创建音频流 pmusic = manager.createplayer(inmenu, audio/midi); /创建midi播放器 incoll = getclass().getresourceass

36、tream(/coll.wav); /创建音频流 pcoll = manager.createplayer(incoll, audio/x-wav); /创建wav播放器 catch (ioexception ex) ex.printstacktrace(); catch (mediaexception ex) ex.printstacktrace(); try pmusic.setloopcount(-1);/设置为循环播放pmusic.start(); /播放音频catch(exception e)e.printstacktrace(); /获得音量控制接口 control=(volume

37、control)pmusic.getcontrol(volumecontrol); control.setlevel(50); /设置音量大小 loadsence(); /加载场景数据 /* 游戏运行 */ public void run() int cyclesthissecond=0; /当前1秒内的循环次数 long cyclestarttime; /循环开始时间 long lastcpstime=0; /上次计算帧速的时间 long cycleendtime=0; /循环结束时间 long cycletimes=0; /一次游戏循环热所花的时间 boolean bskip = fals

38、e; /游戏是否跳帧 cps=0; system.out.println(游戏开始);/在控制台输出开始信息,可屏蔽 /* 游戏主循环 */ while(running) /检查是否运行 cyclestarttime = system.currenttimemillis();/记录游戏循环开始时间 / 下面语句用于处理游戏内容,如果游戏设置为跳帧, / 则本次循环不处理游戏内容 if(!bskip) gameinput(); /处理输入消息 gamecycle(); /处理游戏逻辑 render(g); /渲染游戏画面 flushgraphics(); /更新画面 /* 下面语句用于计算游戏循

39、环时间,并进行相应处理 */ cycleendtime=system.currenttimemillis(); /记录游戏循环结束时间 cycletimes=cycleendtime-cyclestarttime; /计算循环时间 /如果循环时间间隔小于ms_per_frame,则通过休眠,使其不小于rate, /并能让系统有空闲转去处理其他事务 if(cycletimes 1000) /检查距上次计算帧数的/时间是否经过1000ms lastcpstime=system.currenttimemillis(); /设定lastcps为当前时间 cps = cyclesthissecond;

40、/ system.out.println(cps:+cps); /输出每秒的帧数 cyclesthissecond = 0; /重置帧数 else cyclesthissecond+; /帧数递增 system.out.println(游戏结束!); release(); /释放资源 exit(); /退出游戏 /* 启动游戏进程 */ public void start() /检查游戏循环是否处于运行状态,如果未运行,则创建线程并启动 /如果游戏处于运行状态,则表示是暂停后的继续执行,仅设置暂停状态即可 if(!running) running=true; mainthread=new th

41、read(this); mainthread.start(); gamestate=2; else gamestate=2; setpaused(false); /退出游戏 private void exit() mid.exit(); /退出游戏 /* 停止游戏 */ public void stop() running=false; /终止游戏循环 /* 释放游戏资源 */ private void release() /此处为释放游戏资源的代码 try pmusic.stop();/停止音乐 pmusic.deallocate();/释放资源 pmusic.close();/关闭播放器 pcoll.stop();/停止音乐 pcoll.deallocate();/释放资源 pcoll.clo

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论