J2ME手机游戏设计案例源代码-GameEngine_TiledLayer.doc
《J2ME手机游戏设计案例源代码-GameEngine_TiledLayer.doc》由会员分享,可在线阅读,更多相关《J2ME手机游戏设计案例源代码-GameEngine_TiledLayer.doc(20页珍藏版)》请在三一文库上搜索。
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、/* 退出程序 */ public void exit() try destroyApp(false); catch(Exception e) notifyDestroyed(); *GameWorld。Javaimport java.io.IOException;import javax.microedition.lcdui.Font;import javax.microedition.lcdui.Graphics;import javax.microedition.lcdui.Image;import javax.microedition.lcdui.game.GameCanvas;imp
4、ort javax.microedition.lcdui.game.Sprite;import javax.microedition.lcdui.game.TiledLayer;/* * To change this template, choose Tools | Templates * and open the template in the editor. */* * * author Administrator */public class GameWorld extends GameCanvas implements Runnable GameMID mid; /游戏MIDlet T
5、hread MainThread; /游戏主线程 private final int MS_PER_FRAME=30; /设置一次游戏循环所需时间,单位为毫秒(ms),每秒游戏帧数约为1000/MS_PER_FRAME private int cps; /游戏帧速 private boolean isPaused; /游戏暂停状态 private boolean running; /游戏运行状态 Graphics g; /游戏绘图对象 public static int ScreenWidth; /游戏屏幕宽度,设置为静态成员,方便外部访问 public static int ScreenHe
6、ight; /游戏屏幕高度 public static int GameState; /游戏状态,1为暂停,2为游戏进行中,3为游戏失败 public static int KeyState; /按键状态 private Image imgEnemy,imgBoy;/精灵图像 private Sprite sprEnemy; /定义敌人精灵 private MySprite sprBoy; /用扩展的精灵类来定义玩家精灵 /定义三个不同方向的玩家精灵帧序列,右方向可由左方向镜像变换得到 int LSequ=3,4,3,5; /定义左方向精灵帧序列 int USequ=6,7,6,8; /定义上
7、方向精灵帧序列 int DSequ=0,1,0,2; /定义下方向精灵帧序列 int enemyX,enemyY; /敌人精灵坐标 int enemySpeedX; /敌人精灵速度,这里设置为只能水平移动 boolean isColl; /碰撞标志 long CollTextTime; /碰撞文本显示时间 TiledBg tbg; /创建贴图层应用类 TiledLayer tl; /创建贴图层类/定义字体private Font largeFont=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD, Font.SIZE_LARGE); private
8、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(true); /设置为全屏模式 ScreenWidth=getWidth(); /获取屏幕大小 ScreenHeight=getHeight(); g=this.getGraphics(); /获取绘图对象 running=false; /设置
9、游戏运行状态 isPaused=false; GameInit(); /游戏初始化 /* 游戏初始化 */ private void GameInit() 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,0,0,S
10、creenWidth,ScreenHeight); sprBoy.setFrameSequence(LSequ); /设置初始化时的精灵帧序列和方向 sprBoy.setDir( MySprite.LEFT); /设置初始化时精灵方向 sprBoy.defineReferencePixel(sprBoy.getWidth()/2, sprBoy.getHeight()/2); /设置参考点 sprBoy.setRefPixelPosition(ScreenWidth/2, ScreenHeight/2); /通过参考点定位精灵 int sw=sprBoy.getWidth(); int sh=
11、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=-2; /设置敌人精灵速度 tbg=new T
12、iledBg(); /创建TiledBg对象 tl=tbg.getTiledbg(); /获取贴图层 /* 游戏运行 */ public void run() int cyclesThisSecond=0; /当前1秒内的循环次数 long cycleStartTime; /循环开始时间 long lastCPSTime=0; /上次计算帧速的时间 long cycleEndTime=0; /循环结束时间 long cycleTimes=0; /一次游戏循环热所花的时间 boolean bSkip = false; /游戏是否跳帧 cps=0; System.out.println(游戏开始)
13、;/在控制台输出开始信息,可屏蔽 /* 游戏主循环 */ while(running) /检查是否运行 cycleStartTime = System.currentTimeMillis();/记录游戏循环开始时间 / 下面语句用于处理游戏内容,如果游戏设置为跳帧, / 则本次循环不处理游戏内容 if(!bSkip) GameInput(); /处理输入消息 GameCycle(); /处理游戏逻辑 render(g); /渲染游戏画面 flushGraphics(); /更新画面 /* 下面语句用于计算游戏循环时间,并进行相应处理 */ cycleEndTime=System.current
14、TimeMillis(); /记录游戏循环结束时间 cycleTimes=cycleEndTime-cycleStartTime; /计算循环时间 /如果循环时间间隔小于MS_PER_FRAME,则通过休眠,使其不小于rate, /并能让系统有空闲转去处理其他事务 if(cycleTimes 1000) /检查距上次计算帧数的/时间是否经过1000ms lastCPSTime=System.currentTimeMillis(); /设定lastCPS为当前时间 cps = cyclesThisSecond; / System.out.println(cps:+cps); /输出每秒的帧数 c
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- J2ME 手机 游戏 设计 案例 源代码 GameEngine_TiledLayer
链接地址:https://www.31doc.com/p-3312911.html