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

下载本文档

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

文档简介

*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; 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) /* 退出程序 */ 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;import javax.microedition.lcdui.game.Sprite;/* * 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 Thread MainThread; /游戏主线程 private final int MS_PER_FRAME=30; /设置一次游戏循环所需时间,单位为毫秒(ms),每秒游戏帧数约为1000/MS_PER_FRAME private int cps; /游戏帧速 private boolean isPaused; /游戏暂停状态 private boolean running; /游戏运行状态 Graphics g; /游戏绘图对象 private int ScreenWidth; /游戏屏幕宽度 private int ScreenHeight; /游戏屏幕高度 public static int GameState; /游戏状态,1为暂停,2为游戏进行中,3为游戏失败 public static int KeyState; /按键状态 private Image img;/图像 private int currentX;/图像坐标 private int currentY; private Image imgBoy; /精灵图像 private Sprite sprBoy; /定义精灵 private mySprite spr;/定义字体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(true); /设置为全屏模式 ScreenWidth=getWidth(); /获取屏幕大小 ScreenHeight=getHeight(); g=this.getGraphics(); /获取绘图对象 running=false; /设置游戏运行状态 isPaused=false; GameInit(); /游戏初始化 /* 游戏初始化 */ private void GameInit() try img=Image.createImage(/fox.png); imgBoy = Image.createImage(/Boy.png); /创建图像 catch (IOException ex) ex.printStackTrace(); / sprBoy=new Sprite(imgBoy,imgBoy.getWidth()/3,imgBoy.getHeight()/3); sprBoy=new Sprite(imgBoy,32,48); int LeftSequ=3,4,3,5; sprBoy.setFrameSequence(LeftSequ); sprBoy.setFrame(3); int sw=sprBoy.getWidth(); int sh=sprBoy.getHeight(); sprBoy.defineCollisionRectangle(sw/10, sh/10, sw*8/10,sh*8/10); sprBoy.defineReferencePixel(sprBoy.getWidth()/2, sprBoy.getHeight()/2); System.out.println(sprBoy.getFrameSequenceLength();System.out.println(sprBoy.getRawFrameCount(); currentX =ScreenWidth / 2; /初始化图像坐标 currentY = ScreenHeight / 2; spr=new mySprite(imgBoy,32,48); /* 游戏运行 */ 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(游戏开始);/在控制台输出开始信息,可屏蔽 /* 游戏主循环 */ while(running) /检查是否运行 cycleStartTime = System.currentTimeMillis();/记录游戏循环开始时间 / 下面语句用于处理游戏内容,如果游戏设置为跳帧, / 则本次循环不处理游戏内容 if(!bSkip) GameInput(); /处理输入消息 GameCycle(); /处理游戏逻辑 render(g); /渲染游戏画面 flushGraphics(); /更新画面 /* 下面语句用于计算游戏循环时间,并进行相应处理 */ cycleEndTime=System.currentTimeMillis(); /记录游戏循环结束时间 cycleTimes=cycleEndTime-cycleStartTime; /计算循环时间 /如果循环时间间隔小于MS_PER_FRAME,则通过休眠,使其不小于rate, /并能让系统有空闲转去处理其他事务 if(cycleTimes 1000) /检查距上次计算帧数的/时间是否经过1000ms lastCPSTime=System.currentTimeMillis(); /设定lastCPS为当前时间 cps = cyclesThisSecond; System.out.println(cps:+cps); /输出每秒的帧数 cyclesThisSecond = 0; /重置帧数 else cyclesThisSecond+; /帧数递增 System.out.println(游戏结束!); release(); /释放资源 exit(); /退出游戏 /* 启动游戏进程 */ public void start() /检查游戏循环是否处于运行状态,如果未运行,则创建线程并启动 /如果游戏处于运行状态,则表示是暂停后的继续执行,仅设置暂停状态即可 if(!running) running=true; MainThread=new Thread(this); MainThread.start(); GameState=2; else GameState=2; setPaused(false); /退出游戏 private void exit() mid.exit(); /退出游戏 /* 停止游戏 */ private void stop() running=false; /终止游戏循环 /* 释放游戏资源 */ private void release() /此处为释放游戏资源的代码 img=null; System.gc(); /*获取暂停状态 */ public boolean getPaused() return isPaused; /* 设置暂停状态 */ public void setPaused(boolean isPaused) this.isPaused = isPaused; /* 处理游戏输入信息 */ 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) !=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; /* 渲染游戏画面 */ private void render(Graphics g) /* 填充背景 */ g.setColor(0xffffff); g.fillRect(0, 0, ScreenWidth,ScreenHeight); g.drawImage(img,100, 150, Graphics.LEFT|Graphics.TOP); /* 下面是对游戏渲染的具体处理,应根据游戏需要进行修改*/ /绘制图像,图像位置随按键移动 / g.drawImage(img,currentX, currentY, Graphics.HCENTER|Graphics.VCENTER); / sprBoy.setPosition(currentX, currentY); / sprBoy.setPosition(80, 100); / sprBoy.setRefPixelPosition(80, 100); sprBoy.setTransform(Sprite.TRANS_MIRROR); if(sprBoy.collidesWith(img, 100, 150, false) System.out.println(dddd); sprBoy.move(1, 1); / sprBoy.setTransform(Sprite.TRANS_ROT90); sprBoy.paint(g); spr.setFrame(9); spr.render(g); /下面语句用于绘制屏幕尺寸和游戏帧速 /定义字符串 String strWH=Width:+String.valueOf(ScreenWidth)+ Height:+String.valueOf(ScreenHeight); String strCPS=CPS:+String.valueOf(cps); /绘制背景矩形 g.setColor(0xccbb00); g.fillRect(15, 8, 60, 20); g.fillRect(115, 8, 120, 20); /绘制字符串 g.setColor(0xffffff); g.setFont(mediumFont); /设置字体 g.drawString(strCPS,20,10, Graphics.LEFT| Graphics.TOP); g.drawString(strWH,120,10, Graphics.LEFT| Graphics.TOP); /当处于游戏暂停状态时,绘制下面的内容 if(getPaused() g.setColor(0xccbbcc); g.fillRect(70, 100, 100, 20); g.setColor(0xffffff); g.setFont(largeFont); g.drawString(游 戏 暂 停,85,100, Graphics.LEFT| Graphics.TOP); /* 游戏主函数,处理游戏逻辑*/ private void GameCycle() switch(GameState) case 1: /游戏暂停状态时的处理 /根据按键状态执行相应功能 switch(KeyState) case GAME_A_PRESSED: /按下GAME_A键时的处理 GameState=4; /设置游戏状态会结束程序 break; case FIRE_PRESSED: /按下FIRE键时的处理 if(getPaused() GameState=2; setPaused(false); break; break; case 2: /游戏处于运行状态时的处理 /根据按键状态执行相应功能 switch(KeyState) case FIRE_PRESSED: /按下Fire键时 GameState=1; /设置游戏状态为暂停 setPaused(true); /设置暂停 break; case LEFT_PRESSED: /按下Left键时 currentX = Math.max(0, currentX - 2); /改变水平坐标 sprBoy.nextFrame(); break; case RIGHT_PRESSED: /按下Right键时 currentX = Math.min(ScreenWidth - 2, currentX + 2);/改变水平坐标 break; case UP_PRESSED: /按下Up键时 currentY = Math.max(0, currentY - 2); /改变垂直坐标 break; case DOWN_PRESSED: /按下Right键时 currentY = Math.min(ScreenHeight - 2, currentY + 2); /改变垂直坐标 break; case GAME_A_PRESSED: /按下Game_A键时 GameState=4; /设置游戏状态为退出 break; case GAME_B_PRESSED: /按下Game_B键时 sprBoy.setFrame(2); break; break; case 3: /游戏失败的处理 break; case 4: /游戏退出时的处理 stop(); /停止运行 break; *mySprite。Javaimport javax.microedition.lcdui.Graphics;import javax.microedition.lcdui.Image;import javax.microedition.lcdui.game.Sprite;/* * To change this template, choose Tools | Templates * and open the template in the editor. */* * * author Administrator */public class mySprite private Image imgSrc; /精灵源图像 private Image imgSpr; /精灵当前帧图像 private int positionX; /精灵坐标 private int positionY; private int width; /精灵宽度 private int height; /精灵高度 private int r; /圆形包围框的半径 private int countFrames; /精灵帧总数 private int rows; /精灵帧行数 private int cols; /精灵帧列数 private int currentFrame; /当前帧序号 private boolean isVisible; /是否可见 private int speed; /速度 private int dir; /方向 /* 使用image,帧宽度和帧高度构造精灵*/ public mySprite(Image image, int frameWidth, int frameHeight) this.imgSrc = image; this.width = frameWidth; this.height = frameHeight; cols=image.getWidth()/frameWidth; /计算精灵帧列数 rows=image.getHeight()/frameHeight; /计算精灵帧行数 this.countFrames = cols*rows; /计算精灵帧总数 isVisible=true; /设置精灵为可见 positionY=0; /设置精灵初始坐标 positionX=0; setFrame(0); /设置精灵当前帧序号 r=(frameWidthframeWidth?frameWidth:frameWidth)/2; /默认以矩形较短的边的一半作为圆形包围框的半径 /* 获取X坐标 */ public int getX() return this.positionX; /* 设置X坐标 */ public void setX(int positionX) this.positionX = positionX; /* 获取Y坐标 */ public int getY() return this.positionY; /* 设置Y坐标 */ public void setY(int positionY) this.positionY = positionY; /* 设置当前帧为指定帧 */ public void setFrame(int index) if(currentFrame=0) currentFrame=index; imgSpr=Image.createImage(imgSrc, index%cols*getWidth(),index/cols*getHeight() ,getWidth(),getHeight(), Sprite.TRANS_NONE); /* 设置精灵当前帧到下一帧 */ public void nextFrame() currentFrame+; if(currentFrame=countFrames) currentFrame=0; setFrame(getFrame(); /* 渲染精灵 */ public void render(Graphics g) if(isVisible) g.drawImage(imgSpr, positionX, positionY, Graphics.LEFT|Graphics.TOP); /* 获取当前帧序号 */ public int getFrame() return currentFrame; /* 通过矩形框进行精灵碰撞检测 */ public boolean collideRectwith(mySprite spr) if(spr.getY() + spr.getHeight() this.getY() + this.getHeight() | / spr的顶边在当前精灵的底边之下, spr.getX() + spr.getWidth() this.getX() + this.getWidth() ) / spr的右边在当前精灵的左边之左,则表示spr与当前精灵没有碰撞 return false; return true; /* 通过圆形包围框进行精灵碰撞检测*/ public boolean collideRound(mySprite spr) int x1,y1,x2,y2; x1=getX()+getWidth()/2; y1=getY()+getHeight()/2; x2=spr.getX()+spr.getWidth()/2; y2=spr.getY()+spr.getHeight()/2; /计算两个圆心之间的距离是否小于两个圆的半径和,如果小于则发生碰撞 if(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)= getX() & px = getY() & py = (getY() + getHeight() ) return true; return false; /*

温馨提示

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

评论

0/150

提交评论