版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、2009.01,CSIS0329 Computer Game Design and Programming,Tutorial 2 Simple J2ME game programming,Outline,Basic game loop functions Try/catch statements General screen configurations LayerManager Adding a background color Loading an image Making an image move Creating random objects starting at random p
2、ositions Making an image visible and invisible Collision detection Getting current time Drawing texts Sample game,Basic game loop functions,protected void startApp () Contains statements that will be executed when the application is started E.g. initialize some variables public void pauseApp () What
3、 should be done when the application is paused? E.g. display some messages public void destroyApp (boolean unconditional) What should be done when the application is stopped? destroyApp(true) destroy all threads created,Basic game loop functions,public void commandAction(Command c, Displayable s) Ha
4、ndles key commands E.g. public void commandAction(Command c, Displayable s) if (c.getCommandType( ) = Command.EXIT) destroyApp(true); ,Outline,Basic game loop functions Try/catch statements General screen configurations LayerManager Adding a background colour Loading an image Making an image move Cr
5、eating random objects starting at random positions Making an image visible and invisible Collision detection Getting current time Drawing texts Sample game,Try/catch statements,The try/catch statement encloses some code and is used to handle errors and exceptions that might occur in that code. Gener
6、al syntax of the try/catch statement: try body-code catch (exception-classname variable-name) handler-code Four parts: The body-code contains code that might throw the exception that we want to handle. The exception-classname is the class name of the exception we want to handle. The variable-name sp
7、ecifies a name for a variable that will hold the exception object if the exception occurs. The handler-code contains the code to execute if the exception occurs.,Try/catch statements,Example: try hdCanvas = new HardDriveCanvas(this, /car.png, /obstacle.png); catch (IOException ioe) System.err.printl
8、n(Problem loading image +ioe); ,Outline,Basic game loop functions Try/catch statements General screen configurations LayerManager Adding a background colour Loading an image Making an image move Creating random objects starting at random positions Making an image visible and invisible Collision dete
9、ction Getting current time Drawing texts Sample game,General screen configurations LayerManager,You need to have a LayerManager to contain everything. private LayerManager layerManager; layerManager = new LayerManager(); You need to define the size of the visible window. int width = getWidth(); / wi
10、dth of the phone screen int height = getHeight(); / height of the phone screen layerManager.setViewWindow(1,1,width-2, height-2) / left upper corner at (1,1) and right lower corner at (width-2, height-2). i.e. having 1 unit margin on each side,Adding a background color,You need to first create a Gra
11、phics object: Graphics g = getGaphics(); Then set its colour and size: g.setColor(255, 255, 255); / a white color g.fillRect(0, 0, getWidth()-3, getHeight()-3); Then draw it into the LayerManager: layerManager.paint(g, 0, 0);,Loading an image,You need to create an Image object and load your image fi
12、le to it. String carImageName = “/car.png”; / remember / means in the “res” folder Image carImage = Image.createImage(carImageName); You need to use a Sprite object to contain the Image object. Sprite carSprite; carSprite = new Sprite(carImage);,Loading an image,You can set the position of the sprit
13、e using the “setPosition()” method. carSprite.setPosition(width/2, height-30); Finally, you need to “append” your image to the LayerManager. layerManager.append(carSprite);,Making an image move,Getting the key states using getKeyStates() method: int keyStates = getKeyStates(); Getting the current po
14、sition of a sprite using getY() and getX() methods: int CurrentY = carSprite.getY(); int CurrentX = carSprite.getX();,Making an image move,Checking which key is pressed and setting the position of a sprite: if (keyStates / Math.max(a, b) returns the greater number among a and b / Math.min(a, b) retu
15、rns the smaller number among a and b,Making an image move,Math.PI Math.E Math.LN10 Math.LN2 Math.LOG10E Math.LOG2E Math.SQRT1_2 square root of 1/2 Math.SQRT2 square root of 2,Math.abs() Math.round() Math.ceil() Math.floor() Math.max() Math.min() Math.power() Math.random(),Please refer to .hk/jstutor
16、/ch15/ch15.htm for details.,Making an image move,Finally, you need to refresh the screen so that your motion can take effect. flushGraphics();,Creating random objects starting at random positions,You can have an array of sprite objects which all use the same image: String obsImageName = “/obstacle.p
17、ng”; Image obsImage = Image.createImage(obsImageName); obstacles = new Sprite10; for (int i=0; i 10; i+) obstaclesi = new Sprite(obsImage); layerManager.append(obstaclesi); In Java, you can also use the length property to get the number of elements in an array. E.g. obstacles.length is equivalent to
18、 10.,Creating random objects starting at random positions,Then set the position of each sprite randomly: java.util.Random random = new java.util.Random(); for (int i=0; I 1) % 170; int xPos = (random.nextInt()1) % 170; obstaclesi = setPosition(xPos, -yPos); ,Bitwise operators in Java,Making an image
19、 visible and invisible,You can make your sprites visible and invisible at different time to make your game more interesting obstaclesi.setVisible(true); / visible obstaclesi.setVisible(false); / invisible,Collision detection,You can make use of the “collidesWith()” method to detect whether two sprit
20、es collide with each other. for (int i = 0; i obstacles.length; i+) if (carSprite.collidesWith(obstaclesi.true) / some operations ,Getting current time,You can make use of the “System.currentTimeMillis()” method to get the current time. Its type is “long” and so you have to use a “long” variable to
21、store it. long CurrTime = System.currentTimeMillis();,Drawing texts,Besides text boxes, you can draw some simple texts on a screen using methods in the Graphics class. int width = getWidth(); int height = getHeight(); g.setColor(255,100,0); g.fillRect(0, 0, width - 3, height - 3); g.setFont(Font.get
22、Font (Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE); int centerX = width / 2; int centerY = height / 2;,Drawing texts,int fontHeight = g.getFont().getHeight(); int textHeight = 3 * fontHeight; int topY = centerY - textHeight / 2; g.drawString(GAME OVER, centerX, topY, Graphics.HCENTER |
23、Graphics.TOP); g.drawString(Time: + time + s, centerX, topY + fontHeight, Graphics.HCENTER | Graphics.TOP); g.drawString(Score: + score), centerX, topY + 2 * fontHeight, Graphics.HCENTER | Graphics.TOP);,Sample game,A left/right moving car There are some obstacles Can avoid hitting an obstacle, 1 mark will be given Hit an obstacle, game over Print out time spent and score when game is over,Sample screens,Sample game,4 classes HardDriveMIDlet.java Main game loop HardDriveCanvas.java Control of the moving car ObstacleManager.java Control of the obstacle
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB/T 47565-2026农用挂车和农用牵引车许用机械连接组件
- 护理职业健康保护
- 2025年监管科技的NLP合规条款解析
- 2025年家庭服务器网络带宽管理 合理分配流量的技巧
- 护理礼仪中的团队合作精神
- 选剥混茧工班组建设测试考核试卷含答案
- 洗衣粉制造工岗前全能考核试卷含答案
- 普通研磨工安全素养竞赛考核试卷含答案
- 采油平台水手创新应用水平考核试卷含答案
- 打字员变革管理知识考核试卷含答案
- 2025年神农架林区公安局招聘辅警真题
- 胸痹患者中医护理评估与干预
- 2026年4月福建厦门市思明区部分单位联合招聘非在编人员4人笔试模拟试题及答案解析
- 江苏苏豪控股集团秋招面笔试题及答案
- 24J113-1 内隔墙-轻质条板(一)
- 律师事务所内部惩戒制度
- 高中英语课堂形成性评价与听力理解能力提升教学研究课题报告
- 校园校园环境智能监测系统方案
- (2025年)资阳市安岳县辅警考试公安基础知识考试真题库及参考答案
- 职业健康卫生知识培训内容-职业健康培训内容
- 2022年济宁医学院附属医院医护人员招聘考试笔试题库及答案解析
评论
0/150
提交评论