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

上传人:laozhun 文档编号:2393682 上传时间:2023-02-17 格式:DOC 页数:39 大小:191.50KB
返回 下载 相关 举报
J2ME手机游戏设计案例源代码GameEngineRecord.doc_第1页
第1页 / 共39页
J2ME手机游戏设计案例源代码GameEngineRecord.doc_第2页
第2页 / 共39页
J2ME手机游戏设计案例源代码GameEngineRecord.doc_第3页
第3页 / 共39页
J2ME手机游戏设计案例源代码GameEngineRecord.doc_第4页
第4页 / 共39页
J2ME手机游戏设计案例源代码GameEngineRecord.doc_第5页
第5页 / 共39页
点击查看更多>>
资源描述

《J2ME手机游戏设计案例源代码GameEngineRecord.doc》由会员分享,可在线阅读,更多相关《J2ME手机游戏设计案例源代码GameEngineRecord.doc(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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, 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=(VolumeControl)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;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号