J2ME手机游戏设计案例源代码GameEngineRecord

上传人:无*** 文档编号:79080690 上传时间:2022-04-22 格式:DOC 页数:39 大小:270.01KB
收藏 版权申诉 举报 下载
J2ME手机游戏设计案例源代码GameEngineRecord_第1页
第1页 / 共39页
J2ME手机游戏设计案例源代码GameEngineRecord_第2页
第2页 / 共39页
J2ME手机游戏设计案例源代码GameEngineRecord_第3页
第3页 / 共39页
资源描述:

《J2ME手机游戏设计案例源代码GameEngineRecord》由会员分享,可在线阅读,更多相关《J2ME手机游戏设计案例源代码GameEngineRecord(39页珍藏版)》请在装配图网上搜索。

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,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|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();/关闭播放器

42、pColl.stop();/停止音乐 pColl.deallocate();/释放资源 pColl.close();/关闭播放器 catch (MediaException ex) ex.printStackTrace(); imgBoy=null; sprBoy=null; sprEnemy=null; System.gc(); /*获取暂停状态 */ public boolean getPaused() return isPaused; /* 设置暂停状态 */ public void setPaused(boolean isPaused) this.isPaused = isPaused

43、; /* 处理游戏输入信息 */ private void GameInput() int keyStates = this.getKeyStates(); /获取按键状态 /* 下面是对按键事件的具体处理,应根据游戏需要进行修改*/ KeyState=0; /Fire if(keyStates & FIRE_PRESSED )!=0) KeyState=FIRE_PRESSED; / Left if (keyStates & LEFT_PRESSED) != 0) KeyState=LEFT_PRESSED; / Right if (keyStates & RIGHT_PRESSED) !=

44、0 ) KeyState=RIGHT_PRESSED; / Up if (keyStates & UP_PRESSED) != 0) KeyState=UP_PRESSED; / Down if (keyStates & DOWN_PRESSED) !=0) KeyState=DOWN_PRESSED; /Game_A if(keyStates & GAME_A_PRESSED)!=0) KeyState=GAME_A_PRESSED; /Game_B if(keyStates & GAME_B_PRESSED)!=0) KeyState=GAME_B_PRESSED; /Game_C if(

45、keyStates & GAME_C_PRESSED)!=0) KeyState=GAME_C_PRESSED; /Game_D if(keyStates & GAME_D_PRESSED)!=0) KeyState=GAME_D_PRESSED; /* 渲染游戏画面 */ private void render(Graphics g) /* 填充背景 */ g.setColor(0xffffff); g.fillRect(0, 0, ScreenWidth,ScreenHeight); /* 下面是对游戏渲染的具体处理,应根据游戏需要进行修改*/ lm.paint(g, 0, 0); /当敌人精灵和玩家角色相碰撞时,绘制下面的内容 if(isColl) g.setColor(0xccbbcc); g.fillRect(50, 0, 120, 20); g.setColor(0xffffff); g.setFont(largeFont); g.drawString(被敌人撞上啦!,60,0, Graphics.LEFT| Graphics.TOP); /游戏暂停时的渲染 if(GameState = 1) String str=; s

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

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!