J2ME手机游戏设计案例源代码-GameEngine_TiledLayer
- 格式:doc
- 大小:174.00 KB
- 文档页数:20
基于J2ME的手机射击游戏J2ME本设计包括:源程序+可执行程序+论文+答辩稿+软件配制+配置说明摘要手机开发是当今最火爆的开发之一,已其低成本、多用户为优势,在进几年时间里极为流行。
而J2ME又是手机开发平台中运用最为广泛的一种。
本文先介绍了JAVA语言的历史,以及JAVA语言的现状。
然后讨论了一个基本的开发平台的搭建,最后再以一个实际的开发探讨了现在最为流行的开发框架,和制作后期的优化、调试。
关键词: J2ME,MIDP,CLDC,面向对象目录第1章绪论 11.1 题目背景及情况 11.2 国内外研究状况 11.2.1 国外 11.2.2 国内 11.3 题目研究方法 11.4 论文构成及研究内容 2第2章 JAVA语言综述 32.1 JAVA的由来 32.2 JAVA语言的特点 32.3 JAVA的三个版本 42.3.2 J2EE(Java 2 EntERPrise Edition)技术应用 52.3.3 J2ME技术的应用 5第3章移动开发简介 73.1 CLDC和MIDP 73.2移动平台开发特点 83.3 搭建开发环境 83.3.1 工具介绍 83.3.2 工具安装方法 9第4章结构分析 124.1 初步构思 124.1.1 类的划分 124.1.2各个类之间的关系 124.2 基本框架 124.3 算法分析 19第5章后期优化与平台移植 255.1 为什么要优化 255.2 何时不用优化 255.3 从哪里开始优化 265.3 结论 27参考文献 28致谢 29各个类的功能1. Map类地图类的主要功能是负责中地图的卷屏和关卡之间地图的切换。
2. Plane敌机类:敌机类的是为了实现屏幕中敌机的刷新,飞行和射击等主要功能由于Plane敌机类需要派生出BOSS类,所以先要找出BOSS和普通敌机之间的共同之处。
3. BOSS类:继承自Plane,实现BOSS飞行射击,变换子弹等功能。
4. PlayerPlane玩家飞机类:实现了玩家对飞机的控制和飞机移动过程中的动画。
*************GameMID/* @author wizardyx */import javax.microedition.lcdui.Display;import javax.microedition.midlet.*;/* 游戏MIDlet */public class GameMID extends MIDlet {private Display display; //声明Displayprivate StartScreen startscreen; //声明启动画面对象private FlashScreen flashscreen; //声明闪屏画面对象private GameMenu gmenu; //声明菜单画面对象private GameWorld gw; //声明游戏引擎框架private GameMusic gm; //声明音效对象public GameMID(){display = Display.getDisplay(this); //获取Displaygm=new GameMusic();loadFlashScreen(); //加载闪屏画面}public void startApp() {}public void pauseApp() {}public void destroyApp(boolean unconditional) {gmenu=null;flashscreen=null;startscreen=null;gm=null;GameMusic.releases();System.gc(); //释放资源}/* 退出程序*/public void exit(){try{destroyApp(false);}catch(Exception e){}notifyDestroyed();}/* 加载游戏启动画面*/public void loadStartScreen(){flashscreen=null;startscreen=null;startscreen=new StartScreen(this); //创建启动画面display.setCurrent(startscreen); //设置启动画面为当前显示画面}/* 加载闪屏画面*/public void loadFlashScreen(){flashscreen=new FlashScreen(this); //创建闪屏display.setCurrent(flashscreen); //设置闪屏画面为当前显示画面}/* 加载游戏菜单*/public void loadGameMenu(int menuIndex){flashscreen=null;startscreen=null;if(gmenu==null) {gmenu = new GameMenu(this); //创建菜单}gmenu.setMenuIndex(menuIndex); //设置当前菜单项display.setCurrent(gmenu); //设置菜单画面为当前显示画面}/* 加载游戏主界面*/public void loadGameWorld(){gmenu=null;if(gw==null) {gw = new GameWorld(this); //创建游戏引擎画布}display.setCurrent(gw); //设置游戏引擎画布为当前显示画面gw.start();}}*********Bullet/* @author wizardyx */import java.io.IOException;import java.util.Vector;import 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.*//* 玩家子弹类*/public class Bullet {Plane plane; //玩家飞机Sprite sprbullet; //子弹精灵Image imgbullet; //子弹图像private int bulletPower;private int bulletSpeed =4; //子弹速度private int shootTimes; //子弹发射计时private int bulletStay=15; //子弹发射间隔时间public Vector v; //子弹向量public Bullet(Plane plane){try {imgbullet = Image.createImage("/mybullet.png"); //子弹图像} catch (IOException ex) {ex.printStackTrace();}sprbullet=new Sprite(imgbullet,imgbullet.getWidth(),imgbullet.getHeight()); //创建子弹精灵sprbullet.defineReferencePixel(imgbullet.getWidth()/2, imgbullet.getHeight()/2);//设置参考点this.plane=plane; //引用玩家飞机v = new Vector(); //创建存储子弹数据的VectorshootTimes=0; //初始化子弹发射间隔时间}/* 增加新子弹*/public void addNew() {//初始化子弹坐标为飞机坐标int p_x=plane.getX()+plane.getWidth()/2;int p_y=plane.getY();//根据子弹威力级别,添加不同的子弹switch(bulletPower){case 1:{ //前两个参数是子弹的当前坐标,第3个参数是子弹在水平方向上的速度int a[] = {p_x-5,p_y,0}; //一级子弹数据int b[] = {p_x+5,p_y,0};v.addElement(a); //添加子弹数据到v中v.addElement(b);}break;case 2:{int a[] = {p_x-8,p_y+5,0}; //二级子弹数据int b[] = {p_x,p_y,0};int c[] = {p_x+8,p_y+5,0};v.addElement(a);v.addElement(b);v.addElement(c);}break;case 3:{int a[] = {p_x-8,p_y+5,-1}; //三级子弹数据int b[] = {p_x-5,p_y,0};int c[] = {p_x+5,p_y,0};int d[] = {p_x+8,p_y+5,1};v.addElement(a);v.addElement(b);v.addElement(c);v.addElement(d);}break;case 4:{int a[] = {p_x-8,p_y,0}; //四级子弹数据int b[] = {p_x+8,p_y,0};int c[] = {p_x,p_y,0};int d[] = {p_x-10,p_y+8,-1};int e[] = {p_x+10,p_y+8,1};int f[] = {p_x-14,p_y+8,-1};int g[] = {p_x+14,p_y+8,1};v.addElement(a);v.addElement(b);v.addElement(c);v.addElement(d);v.addElement(e);v.addElement(f);v.addElement(g);}break;case 5:{int a[] = {p_x-4,p_y,0}; //五级子弹数据int b[] = {p_x,p_y,0};int c[] = {p_x+4,p_y,0};int d[] = {p_x-6,p_y+6,-1};int e[] = {p_x+6,p_y+6,1};int f[] = {p_x-10,p_y+8,-1};int g[] = {p_x+10,p_y+8,1};int h[] = {p_x-10,p_y+10,-2};int i[] = {p_x+10,p_y+10,2};int j[] = {p_x-14,p_y+12,-2};int k[] = {p_x+14,p_y+12,2};v.addElement(a);v.addElement(b);v.addElement(c);v.addElement(d);v.addElement(e);v.addElement(f);v.addElement(g);v.addElement(h);v.addElement(i);v.addElement(j);v.addElement(k);}break;}}/* 删除指定的子弹*/public void delBullet(int i){if(i<v.size()){v.removeElementAt(i);}}/* 绘制子弹*/public void render(Graphics g){int row[]=null;//循环绘制子弹for(int i=0;i<v.size();i++){row= (int[])v.elementAt(i); //从v中获取子弹数据sprbullet.setRefPixelPosition(row[0],row[1]);//设置子弹精灵的位置sprbullet.paint(g); //绘制子弹}}/* 设置子弹威力级别*/public void setBulletPower(int bulletPower) {this.bulletPower = bulletPower;}/* 获取子弹威力级别*/public int getBulletPower() {return bulletPower;}/* 更新子弹数据*/public void update() {int bulletData[] = null; //用于临时记录子弹数据shootTimes++; //子弹发射时间递增//当玩家飞机生命值大于0时,每隔bulletStay次循环发射一次子弹if(plane.getLifes()>0 && shootTimes%bulletStay==0) {addNew(); //增加子弹shootTimes=0; //重置子弹发射时间}for(int i=v.size()-1;i>=0;i--){bulletData = (int[])v.elementAt(i); //获取子弹数据bulletData[0]=bulletData[0]+2*bulletData[2]; //计算子弹X坐标bulletData[1]-=bulletSpeed; //按子弹速度计算Y坐标//检查子弹是否超出屏幕if(bulletData[1]<0 || bulletData[0]<0 || bulletData[0]>240){delBullet(i); //当子弹超出屏幕范围,删除子弹}else{v.setElementAt(bulletData, i); //更新子弹数据}}}/* 子弹碰撞检测*/public int collidesWith(Sprite spr){int n=0; //初始化碰撞次数int bulletData[] = null; //初始化子弹数据数组for(int i=v.size()-1;i>=0;i--){bulletData = (int[])v.elementAt(i); //获取子弹数据sprbullet.setRefPixelPosition(bulletData[0],bulletData[1]); //设置子弹精灵位置//当子弹发生碰撞,删除子弹,并记入碰撞次数if(sprbullet.collidesWith(spr,false)){delBullet(i);n++;}}return n; //返回碰撞次数}public void Release(){v.removeAllElements();v=null;imgbullet=null;sprbullet=null;}}**********FlashScreenimport java.io.IOException;import java.util.Timer;import java.util.TimerTask;import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Graphics;import javax.microedition.lcdui.Image;/** To change this template, choose Tools | Templates* and open the template in the editor.*//**** @author Wizardyx*//* 闪屏画面*/public class FlashScreen extends Canvas{GameMID mid;Image img;private Timer timer; //声明定时器private TimerTask task; //声明定时器任务/* 构造方法*/public FlashScreen(GameMID mid) {this.mid=mid;try {img = Image.createImage("/logo.png");} catch (IOException ex) {ex.printStackTrace();}FlashTimer(); //闪屏计时repaint(); //绘制闪屏画面}//释放资源public void release() {img=null;}/*按键事件处理,按任意键均加载游戏菜单画面*/ protected void keyPressed(int keyCode) {super.keyPressed(keyCode);timer.cancel();release();mid.loadGameMenu(1);//加载游戏菜单}/*绘制闪屏画面*/protected void paint(Graphics g) {g.drawImage(img,0, 0, Graphics.TOP|Graphics.LEFT);}/* 闪屏画面计时*/private void FlashTimer() {timer = new Timer(); //创建定时器task = new TimerTask(){ //创建定时任务private int times=0; //计时变量public void run() {times++; //task任务计时,每次增加100ms(下面任务启动中定义)if(times>10){ //3000ms后,自动加载游戏启动界面release();mid.loadStartScreen(); //加载游戏启动界面timer.cancel();}}};//启动task任务,在0ms后开始,每隔300ms运行一次timer.schedule(task, 0,300);}}******GameDesign/** To change this template, choose Tools | Templates* and open the template in the editor.*/import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*;import java.io.IOException;/*** @author Administrator*/public class GameDesign {//<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0| private Image bgTiles;private TiledLayer background;private TiledLayer Meteorite;//</editor-fold>//GEN-END:|fields|0|//<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|//</editor-fold>//GEN-END:|methods|0|public Image getBgTiles() throws java.io.IOException {//GEN-BEGIN:|1-getter|0|1-preInit if (bgTiles == null) {//GEN-END:|1-getter|0|1-preInit// write pre-init user code herebgTiles = Image.createImage("/bgTiles.png");//GEN-BEGIN:|1-getter|1|1-postInit }//GEN-END:|1-getter|1|1-postInit// write post-init user code herereturn this.bgTiles;//GEN-BEGIN:|1-getter|2|}//GEN-END:|1-getter|2|public TiledLayer getBackground() throws java.io.IOException {//GEN-BEGIN:|2-getter|0|2-preInitif (background == null) {//GEN-END:|2-getter|0|2-preInit// write pre-init user code herebackground = new TiledLayer(8, 20, getBgTiles(), 32, 32);//GEN-BEGIN:|2-getter|1|2-midInitint[][] tiles = {{ 1, 2, 3, 3, 3, 3, 4, 1 },{ 1, 2, 3, 3, 3, 3, 4, 1 },{ 1, 2, 3, 3, 3, 3, 4, 1 },{ 1, 2, 3, 3, 3, 3, 4, 1 },{ 1, 2, 3, 3, 3, 3, 4, 1 },{ 1, 2, 3, 3, 3, 3, 4, 1 },{ 1, 2, 3, 3, 3, 3, 4, 1 },{ 1, 2, 3, 3, 3, 3, 4, 1 },{ 1, 2, 3, 3, 3, 3, 4, 1 },{ 1, 2, 3, 3, 3, 3, 4, 1 },{ 1, 2, 3, 3, 3, 3, 4, 1 },{ 1, 2, 3, 3, 3, 3, 4, 1 },{ 1, 2, 3, 3, 3, 3, 4, 1 },{ 1, 2, 3, 3, 3, 3, 4, 1 },{ 1, 2, 3, 3, 3, 3, 4, 1 },{ 1, 2, 3, 3, 3, 3, 4, 1 },{ 1, 2, 3, 3, 3, 3, 4, 1 },{ 1, 2, 3, 3, 3, 3, 4, 1 },{ 1, 2, 3, 3, 3, 3, 4, 1 },{ 1, 2, 3, 3, 3, 3, 4, 1 }};//GEN-END:|2-getter|1|2-midInit// write mid-init user code herefor (int row = 0; row < 20; row++) {//GEN-BEGIN:|2-getter|2|2-postInitfor (int col = 0; col < 8; col++) {background.setCell(col, row, tiles[row][col]);}}}//GEN-END:|2-getter|2|2-postInit// write post-init user code herereturn background;//GEN-BEGIN:|2-getter|3|}//GEN-END:|2-getter|3|public TiledLayer getMeteorite() throws java.io.IOException {//GEN-BEGIN:|3-getter|0|3-preInitif (Meteorite == null) {//GEN-END:|3-getter|0|3-preInit// write pre-init user code hereMeteorite = new TiledLayer(4, 20, getBgTiles(), 32, 32);//GEN-BEGIN:|3-getter|1|3-midInitint[][] tiles = {{ 21, 0, 0, 0 },{ 0, 0, 0, 0 },{ 22, 0, 0, 0 },{ 0, 0, 21, 22 },{ 0, 0, 0, 0 },{ 22, 0, 0, 0 },{ 0, 21, 0, 0 },{ 0, 0, 0, 0 },{ 0, 0, 0, 0 },{ 0, 0, 22, 0 },{ 0, 0, 0, 0 },{ 22, 0, 0, 0 },{ 0, 0, 0, 22 },{ 21, 0, 0, 0 },{ 0, 0, 0, 0 },{ 0, 0, 22, 0 },{ 0, 0, 0, 0 },{ 0, 0, 21, 0 },{ 0, 22, 0, 0 },{ 0, 0, 0, 22 }};//GEN-END:|3-getter|1|3-midInit// write mid-init user code herefor (int row = 0; row < 20; row++) {//GEN-BEGIN:|3-getter|2|3-postInitfor (int col = 0; col < 4; col++) {Meteorite.setCell(col, row, tiles[row][col]);}}}//GEN-END:|3-getter|2|3-postInit// write post-init user code herereturn Meteorite;//GEN-BEGIN:|3-getter|3|}//GEN-END:|3-getter|3|public void updateLayerManagerForScene1(LayerManager lm) throws java.io.IOException {//GEN-LINE:|4-updateLayerManager|0|4-preUpdate// write pre-update user code heregetMeteorite().setPosition(57,306);//GEN-BEGIN:|4-updateLayerManager|1|4-postUpdategetMeteorite().setVisible(true);lm.append(getMeteorite());getBackground().setPosition(-8, -7);getBackground().setVisible(true);lm.append(getBackground());//GEN-END:|4-updateLayerManager|1|4-postUpdate// write post-update user code here}//GEN-BEGIN:|4-updateLayerManager|2|//GEN-END:|4-updateLayerManager|2|}***********GameLevelimport java.io.IOException;import javax.microedition.lcdui.Font;import javax.microedition.lcdui.Graphics;import javax.microedition.lcdui.Image;import yerManager;import javax.microedition.lcdui.game.TiledLayer;/** To change this template, choose Tools | Templates* and open the template in the editor.*//**** @author Administrator*/public class GameLevel {public static GameLevel Instance;private boolean levelFinish; //过关标志private int finishTimes; //过关后的动画时间private boolean Live; //玩家飞机是否存活private byte glt=Graphics.LEFT|Graphics.TOP; //锚点Image imgBlood; //生命条背景图像int bloodX,bloodY,bloodW,bloodH; //用于绘制损失的生命条的矩形参数ImgText imgText; //字符图像Image imgStr; //字符图像对应的字符串String strScore; //存储得分GameDesign gd; //场景设计器TiledLayer tlBg;TiledLayer tlMeteorite;LayerManager lm; //场景管理int viewPortX,viewPortY,viewPortW,viewPortH; //场景视口参数Image imgPlane; //玩家飞机图像Plane plane; //玩家飞机精灵//定义字体private Font largeFont=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_LARGE);private int storyY;//游戏关卡完成后的故事描述String story = "历经一场苦战,终于将这批入侵者打退了。
俄罗斯方块手机游戏的设计与实现摘要J2ME(Java 2 Micro Edition) 是近年来随着各种不同设备,尤其是移动通信设备的飞速发展而诞生的一项新的开发技术。
它定位在消费性电子产品的应用上,对设备的智能化、多样化,提供了革命性的解决方案。
随着手机的日益普及、Java功能在移动设备上的实现,Java应用程序产生的手机增值服务逐渐体现出其影响力,对丰富人们的生活内容、提供快捷的资讯起着不可忽视的作用。
本游戏就是针对J2ME平台,开发出能在支持java手机上运行的俄罗斯方块游戏,具有高移植性。
同时它的操作简单,可玩性高,是一款老少皆宜的游戏。
本论文介绍了J2ME的相关技术及本程序的开发流程和具体功能的实现。
关键字JAVA J2ME 手机游戏1.绪论1.1 传统手机游戏的利弊在信息社会中,手机及其他无线设备越来越多的走进普通百姓的工作和生活,随着信息网络化的不断进展,手机及其他无线设备上网络势在必行。
但是传统手机存在以下弊端:1. 传统手机出厂时均由硬件厂商固化程序,程序不能增加、删除,有了错误也不能更新、修改,若要增加新功能必须另换一部手机。
2. 传统手机访问互联网是通过WAP(Wireless Application Protocal),所有网络资源必须接通网络才能在线访问,非常耗时、费用亦很高。
但是Java技术在无线应用方面的优势非常明显:1. 应用程序可按需下载,而不是购买由硬件商提供的套件,可升级空间大。
2. Java技术提供了一个类库,它使的应用开发商可以创建更为直觉、丰富的用户界面。
3. Java技术使网络带宽的应用更为有效,因为应用程序可以下载到器件上,并在本地运行,仅仅是在连接到服务器时才会占用网络带宽。
1.2 目前手机游戏发展状态与技术基于以上分析,Java手机将是手机的发展方向,是业界的热点。
到目前为止,已经有很多手机支持java了,并且还在快速的增长。
现在,J2ME已经发展成为一种比较成熟的技术,对手机提供的最小平台更合理,更实用。
*********Midletimport javax.microedition.lcdui.Display;import javax.microedition.midlet.*;public class Midlet extends MIDlet {private Display display;private KeyActionCanvas canvas; //声明画布对象canvaspublic Midlet(){display = Display.getDisplay(this); //获取Displaycanvas=new KeyActionCanvas(); //创建画布对象canvasdisplay .setCurrent(canvas); //设置canvas为当前显示对象}public void startApp() {}public void pauseApp() {}public void destroyApp(boolean unconditional) {}}**********KeyActionCanvasimport javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Graphics;/** To change this template, choose Tools | Templates* and open the template in the editor.*//**** @author Administrator*/public class KeyActionCanvas extends Canvas {int Action;protected void paint(Graphics g) {}protected void keyPressed(int keyCode) {Action=getGameAction(keyCode); //通过按键获取游戏动作switch(Action){case Canvas.UP:System.out.println("处理游戏动作UP");break;case Canvas.DOWN:System.out.println("处理游戏动作DOWN");break;case Canvas.LEFT:System.out.println("处理游戏动作LEFT");break;case Canvas.RIGHT:System.out.println("处理游戏动作RIGHT");break;case Canvas.FIRE:System.out.println("处理游戏动作FIRE");break;case Canvas.GAME_A:System.out.println("处理游戏动作GAME_A");break;case Canvas.GAME_B:System.out.println("处理游戏动作GAME_B");break;case Canvas.GAME_C:System.out.println("处理游戏动作GAME_C");break;case Canvas.GAME_D:System.out.println("处理游戏动作GAME_D");break;}}}。
基于J2ME的手机游戏开发实例与发展分析摘要:随着java技术的不断更新,j2me技术在手机游戏的应用逐渐成为游戏市场的新亮点。
文章首先介绍了j2me技术的原理和架构,之后通过实例分析了j2me在手机游戏中的应用,最后分析了j2me手机游戏的开发意义。
关键词:j2me;midp;软件应用;软件开发中图分类号:tp311.52 文献标识码:a 文章编号:1007-9599 (2012) 24-0188-02随着java语言的不断发展,已经逐渐成长为一种尖端的程序设计语言。
这个最初用来编写与硬件不相关的嵌入式系统的编程语言,现在更是做到了与厂商不相关、与硬件不相关的服务器端技术,在软件开发应用中展示出了强大的生命力和发展前景。
j2me技术正是基于java技术规范的,高度优化的java运行环境,在消费电子领域占据着强有力的位置,尤其是手机游戏的开发上,更是发挥的淋漓尽致。
当前j2me技术最流行的应用就是在制作手机游戏方面,java手机的出现更为这种技术的发挥提供了宽阔的平台。
1 j2me技术及其架构java2 micro edition (j2me)是java2技术微型版,与j2se、j2ee同样具有美国sun微系统公司提出的java技术规范,主要针对消费电子市场,为机顶盒、移动电话和pda之类嵌入式消费电子设备提供的java语言平台。
j2me的运行环境与目标更加多样化,而每一个产品的用途却变得更加单一,资源限制也更为严格,其构成主要是由概要描述、配置、java虚拟机和操作系统等部分组成的标准架构之上的。
这种架构下模块化设计很好的适应了小型计算设备的应用限制,通过j2me架构甚至可以替代小型计算设备的操作系统。
j2me的架构分为configuration、profile和optional packages,它们的组合取舍形成了具体的运行环境。
第一层configuration的作用是与原计算机设备所带有的系统进行互换,它里面还存在着一个配置层,负责处理profile和jvm之间的交互;第二层的profile层,主要是设备的应用程序编程用的接口集合,它对设备横向分类,针对特定领域细分市场,内容主要包括特定用途的类库和api;第三层的optional packages是独立于前面两者提供附加的、模块化的和更为多样化的功能,比如多媒体应用、蓝牙等。
J2ME手机游戏设计之“打砖块”一、游戏分析:随着现代社会发展的日新月异,人们平常工作学习的竞争也在加大,生活的压力也跟着加重。
所以利用闲暇之余,大家都需要休闲娱乐工具,来放松自己,缓解压力,消除紧张的疲劳状态。
在这样的背景下,打砖块游戏作为一向休闲手机游戏应运而生,它不仅能舒缓我们的身心,而且能够开发我们的动手能力,提高自己的反应敏捷程度,充实自己。
二、游戏规则设计:BeatBrick:打砖块游戏的游戏规则为,每局有三个球,也就是有三条生命。
首先发球,球在击打砖块后弹回。
此时,须用球拍接住弹回的球并令其继续击打砖块。
否则,球会掉出屏幕。
直至球全部使用完或通全关则游戏结束。
游戏开始时,小球会停在挡板正中间,且此时挡板可左右自由移动,当按下空格键后小球会弹出,并在程序设定的区域内不停的碰撞反弹。
当小球碰到墙壁、挡板和砖块时,均会以相反的速度反弹,并且砖块被碰撞后会自动消失。
挡板可左右移动以接住小球,若小球没有被接住,超出指定区域下边缘,则程序会提示游戏结束,点击“确定”后退出程序。
当所有的砖块均消失时,程序会提示你赢了。
三、游戏实现:1、J2ME,即Java 2 Micro Edition,是SUN公司推出的在移动设备上运行的微型版Java平台,常见的移动设备有手机,PDA,电子词典,以及各式各样的信息终端如机顶盒等等。
最重要的移动终端当然是手机了,因此,我们主要讨论手机相关的J2ME规范。
J2ME是Sun公司为嵌入式开发所定义的一个框架,一系列标准的规范所组成。
所以J2ME是一个协议簇,而不是单一的规范。
2、游戏由6个类构成:GameScreen:GameScreen类表示游戏的核心,它包括对象的移动、图像的绘制以读取和响应用户的输人。
本游戏从MIDP LCDUI的低级Canvas类派生一个类来代表游戏的屏幕,后面将对这个类进行详细的说明。
BeatBrick:由MIDIet继承而来的应用程序类来代表这个应用程序。
引言在如今3G时代,手机游戏、移动多媒体无线增值服务,已会成为一个热点。
我国主要的移动运营商正在积极努力致力于发展手机游戏业务。
在中国,手机游戏仍处于探索时期,其发展仍然受很多因素的制约。
面对广阔的市场和无限的潜在客户的基础上,如何真正让手机游戏在正确的轨道上发展和运作是电信运营商和游戏开发商急需解决的问题。
从用户角度看,手机游戏因其随时随地便于携带的基本特征备受用户关注。
因此用户无论在户外或车里,只要是空闲时间,都可以成为手机游戏参与者。
由于游戏节奏很快,不会占用用户大量的时间,游戏情节进度可以暂停等效果,给用户提供方便,并降低了游戏难度的阈值,提高用户的游戏的兴致度及可玩值,提供更适合的人在旅游时的休闲、娱乐功能。
从开发的角度上来看,成熟的手机游戏产品应该有以下几个方面:一个是能够适应庞大的用户群,即可以在高峰时间内集中加载游戏的信息;二是游戏可以适应不同的移动终端的屏幕尺寸;三是一个手机游戏的兼容性,一些智能手机的色彩和声音支持具有良好的兼容性;四是游戏中应当应用合理大小,若手机没有太大内存,用户下载和存储很难;五是游戏过程能中断性要求,结合不同的特点,可以实现手机操作系统的背景。
目前,手机网络游戏有两大发展的基本条件:一是有效的3G网络传输平台,在3 G手机网络时代的传输速率和承载能力有了很大程度的提高,互联网电话打开网页时间基本控制在3秒左右,对移动电话使用3G网络的高速数据传输的实时转播的多媒体内容丰富,打破了数据传输速度的对手机游戏开发限制;二是完美的电话终端的软硬件支持环境,手机硬件生产、开发与手机操作系统的不断成熟,为移动终端平台实现多种插件凝聚力提供依据,手机的娱乐功能越来越易于实现高质量的,有利于各种手机网络游戏需要。
手机网络游戏市场的需求将在几年呈强劲的势头。
首先,是指在2002年左右发展迅速的PC网络游戏。
从中可以看到,中国的宽带互联网业务网络游戏和走进公众生活,占用庞大的用户群体,使中国的游戏厂商也如雨后春笋般迅速崛起一般。
基于J2ME的手机游戏主菜单的设计与实现摘要刚进游戏时看到的菜单叫主菜单,手机游戏主菜单界面是集游戏中主要功能大成的界面,设计的好坏直接影响用户群的大小。
本文在分析菜单界面功能和键位使用方法的基础上,结合实际开发中的经验,阐述了手机游戏菜单的设计原则和设计流程,并给出了基于j2me的编程实现。
关键词主菜单;j2me;手机游戏中图分类号tp311 文献标识码a 文章编号1674-6708(2010)30-0230-02游戏一般有两个菜单,手机游戏也不例外:主菜单(main menu)和暂停菜单(pause menu)。
在刚进游戏时看到的菜单叫主菜单, 在游戏过程中弹出的菜单叫暂停菜单。
游戏需要有专门的代码来绘制菜单和实现菜单的功能。
其中主菜单主要提供给玩家“新游戏”、“继续”、“音乐开关”、“帮助”、“关于”、“退出游戏”等功能,主菜单界面是集游戏中主要功能大成的界面,设计的好坏直接影响用户群的大小。
由于手机内存、屏幕、键盘等的限制,手机游戏对功能的要求更高,主菜单的设计尤为重要。
1 菜单界面功能分析游戏界面作为人机交互的桥梁,其作用无可取代。
游戏玩家对游戏的直观印象,一个来自操作,另一个就是画面。
游戏界面本身就是画面的一部分,其地位举足轻重[1],通常的手机游戏主要涉及以下几个界面:1)启动界面,从程序启动到进入游戏主界面时的画面,一般制作一个简单的开始动画;2)主菜单界面:累似于文章写作中的提纲,可以点击菜单进入到相应的界面中;3)新游戏界面:通常指游戏运行中的主界面,也是新游戏的开始部分;4)继续界面,可以保存游戏进度,也可以用来作为难度选择界面来做;5)帮助界面。
主要介绍游戏规则和按键控制等;6)关于界面,这个界面即申明了版权,又可以适当作些广告;7)退出界面,可以直接退出游戏,或单独作一个界面,询问玩家是否退出游戏。
每一个游戏的菜单都是非常重要的一部分,无论是界面的美观,功能,或者版式。
XXXXXX 本科毕业设计(论文)基于J2ME的手机游戏贪吃蛇的开发学院(系):计算机科学与工程专业:计算机科学与技术学生姓名:×××学号:×××指导教师:XXXX评阅教师:完成日期:2011/6/10摘要J2ME(Java 2 Micro Edition)是近年来随着各种不同设备,尤其是移动通信设备的飞速发展而诞生的一项新的开发技术。
J2ME虚拟机在手机上的普及为手机游戏的发展提供了最适合的土壤,随后MIDP2.0的发布,特别是其中新增的GameAPI使得手机游戏开发者可以更专注于游戏性的增强而不再是繁琐的动画处理与地图设计,让开发过程变得更加方便迅捷。
所以它成为手机游戏发展的重要里程碑。
本论文着眼于J2ME技术的应用,开发一款趣味性手机游戏——贪吃蛇。
本文首先介绍了手机游戏在国内外的发展现状及发展前景。
之后就基于我们这次的毕业课题,针对我们如何在MyEclipse平台应用J2ME进行手机游戏开发进行详细介绍,具体包括对本游戏程序的结构设计、界面设计、具体功能的实现以及游戏测试等。
关键词:J2ME;MIDP;手机游戏;贪吃蛇The Cellphone Game Greedy Snake Based On J2MEAbstractJ2ME(Java 2 Micro Edition) is a kind of fast developing technology implemented on various devices especially mobile communication equipments. Widespread use of KVM on the mobile phone increases the speed of mobile game development. With the release of MIDP 2.0, especially the newly added GameAPI, game developer can concentrate on the enhance of game performance, freeing themselves of animation making and game map design. Game development becomes rapid and more and more convenient. So, MIDP 2.0 becomes the milestone of mobile game development.This dissertation focuses on implementation of J2ME technology and has developed a n interesting game suite running on mobile phones — Greedy Snake. Firstly, This dissertation introduces the developing status quo of the handset game in domestic and foreign, and also the foreground of the handset game. Then, it will make an introduction about how we develop the handset games using J2ME in MyEclipse platform in details, including the designing of this game program’s structure, the designing of the interface, the detailed implementation of the function , game testing, and so on.Key words: J2ME; MIDP; Handset Game; Greedy Snake目录摘要 (I)Abstract (II)1 引言 (1)1.1 研究背景 (1)1.2 手机游戏研究的目的和意义 (1)1.3 手机游戏的国内外现状 (1)1.4 研究重点 (2)1.5 创新之处 (2)2 J2ME技术及开发环境的配置 (3)2.1 J2ME 简介 (3)2.2 J2ME中的虚拟机 (3)2.3 J2ME的架构 (4)2.4 MIDlet状态图 (5)2.5 MIDP应用程序 (6)2.6 J2ME 开发环境搭建 (7)2.6.1 开发工具安装 (7)2.6.2 贪吃蛇程序开发的模拟环境 (7)3 总体设计 (8)3.1 游戏的基本需求 (8)3.2 游戏界面框架 (8)3.3 游戏总体流程 (9)3.4 游戏程序组织结构 (10)3.5 关键技术 (11)3.5.1 高级用户界面 (11)3.5.2 低级用户界面 (11)3.5.3 内存优化技术 (12)4 详细设计 (14)4.1 双蛇模式 (14)4.1.1 双蛇模式的概述 (14)4.1.2 双蛇模式的操作流程 (14)4.1.3 双蛇模式代码结构 (15)4.1.4 关键类的设计 (16)4.2 关键代码的实现 (23)4.2.1 双蛇模式游戏地图代码设计 (23)4.2.2 食物的代码设计 (24)4.2.3 蛇的代码设计 (24)4.2.4 双蛇模式主循环 (26)5 程序的运行测试 (27)结论 (30)1 程序的不足之处..................................... 错误!未定义书签。
摘要随着移动电话的普及率越来越高,手机已经成为人们生活中的必需品。
同时,随着科技的发展,移动电话的功能也越来越强,手机娱乐已经成为一项充满发展前景和商机的产业。
虽然目前手机的处理能力与个人计算机的标准处理能力相比很有限,但是足够运行一个相对小型的游戏,甚至是3D游戏。
本文先从Java手机游戏的背景出发,介绍了Java手机游戏的发展现状,然后从整体上介绍本3D游戏开发的模型、结构分析和具体功能实现,游戏的接口和运作方式。
通过对手机游戏开发,MIDP 2.0,以及JSR-184(J2ME 3D技术)的研究,采用立即模式,运用深度优先遍历算法来随机生成游戏地图,并尽力模拟手机游戏的普遍性形式,实现一个操作简单,并有一定可玩性的完整3D手机迷宫游戏。
关键词:J2ME;MIDP 2.0;M3G(Mobile 3D Graphics--JSR184);立即模式(immediate mode);深度优先遍历。
AbstractWith mobile phone penetration rate increasing, mobile phones have become a necessity in people's lives. At the same time, with the development of technology, the functions of mobile phones more and more strong, mobile entertainment has become a full development prospects and business opportunities in the industry. Although at present the handling capacity of mobile phones and personal computer processing power than the standard is very limited, but enough of a relatively small operation of the game, or even 3D game.This article start with the background of Java mobile phone games based on the Java development of mobile phone games, and then a whole game on the 3D model of development, structural analysis and specific functions to achieve, the game's interface and mode of operation. Through the development of mobile games, MIDP 2.0, and JSR-184 (J2ME 3D technology) research, using the depth-first traversal algorithms to generate random map games, and to try to simulate the universal form of mobile games, to realize a simple operation, and funny 3D maze game on mobile phone.KEY WORDS:J2ME; MIDP 2.0; M3G(Mobile 3D Graphics--JSR184); Immediate mode; The depth-first traversal algorithms目录第一章前言 (1)1.1引言 (1)1.2手机游戏现状 (1)1.3手机游戏应具有的特征 (3)1.4论文内容及安排 (4)第二章开发环境及相关技术 (6)2.1开发环境 (6)2.1.1关于Eclipse及Eclipseme (6)2.1.2关于Wireless Tool Kit (9)2.2 MIDP 2.0简介 (9)2.3 J2ME概述 (11)2.4 J2ME 3D技术简介 (13)2.4.1M3G(JSR-184) (14)2.4.2开发模式 (16)第三章设计作品思想、结构相关 (18)3.13D游戏开发概述 (18)3.2游戏的策划 (19)3.3游戏的准备工作 (20)3.4游戏线程 (21)3.5游戏的流程 (22)3.6深度优先算法生成迷宫 (23)3.6.1深度优先遍历的基本方法 (24)3.6.2深度优先算法的基本思想 (27)第四章3D迷宫游戏的实现 (29)4.1主类Maze3DMIDlet类 (29)4.2平面Plane类的实现 (30)4.2.1定义顶点 (32)4.2.2指定三角带状的顶点索引 (32)4.2.3设置纹理坐标 (32)4.3迷宫地图Maze类的实现 (34)4.4 菜单栏表MenuList类的实现 (39)4.5设备属性Graphics3DProperties类的实现 (41)4.6 错误处理ErrorScreen类的实现 (42)4.7游戏画布MazeCanvas类的实现 (43)4.7.1变量和构造函数 (43)4.7.2创建开始标志 (45)4.7.3创建结束标志 (46)4.7.4创建迷宫的墙壁 (47)4.7.5半透明的墙壁 (49)4.7.6创建地板 (50)4.7.7游戏视角的切换 (51)4.7.8游戏线程 (53)4.7.9 绘制3D场景 (55)4.7.10 绘制2D图像 (56)总结 (58)参考文献 (59)附录 (60)第一章前言1.1 引言最早的手机游戏出现于1997年,经过十年的发展,随着手机终端和移动通信网路的不断进步,手机游戏也正在经历由简单到复杂的进化过程。
********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。Java
import 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; 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 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; //游戏绘图对象 public static int ScreenWidth; //游戏屏幕宽度,设置为静态成员,方便外部访问 public static int ScreenHeight; //游戏屏幕高度 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}; //定义上方向精灵帧序列 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 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 { 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,ScreenWidth,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=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 TiledBg(); //创建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("游戏开始");//在控制台输出开始信息,可屏蔽
/* 游戏主循环 */ while(running){ //检查是否运行
cycleStartTime = System.currentTimeMillis();//记录游戏循环开始时间 // 下面语句用于处理游戏内容,如果游戏设置为跳帧, // 则本次循环不处理游戏内容 if(!bSkip) { GameInput(); //处理输入消息 GameCycle(); //处理游戏逻辑 render(g); //渲染游戏画面 flushGraphics(); //更新画面 }
/* 下面语句用于计算游戏循环时间,并进行相应处理 */ cycleEndTime=System.currentTimeMillis(); //记录游戏循环结束时间 cycleTimes=cycleEndTime-cycleStartTime; //计算循环时间
//如果循环时间间隔小于MS_PER_FRAME,则通过休眠,使其不小于rate, //并能让系统有空闲转去处理其他事务 if(cycleTimesbSkip = false; try { Thread.sleep(MS_PER_FRAME - cycleTimes); //休眠 } catch (InterruptedException ex) { ex.printStackTrace(); } } else{ //如果循环时间大于MS_PER_FRAME,则设置bSkip为true, //让程序跳过一次循环,以主动丢帧的方式换取游戏速度 bSkip = true;