版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年文物保护工程责任监理师从业资格考试监理通论知识模拟试卷及答案(共七套)
- 学校实验室仪器使用标准化操作手册
- 2026年内蒙古高职单招文化课综合试题及参考要点含答案
- 林芝地区中级统计师资格考试(统计基础理论及相关知识)能力提高训练试题库及答案(2026年)
- 胆囊腺肌症诊疗共识(2024版)
- 旅游导游服务评价与满意度绩效考评表
- 房产销售顾问市场拓展与业绩达成绩效考核表
- 服装企业服装清洗与保养流程手册
- 2026中国食品连锁加盟行业市场分析与实践与品牌运营及模式创新报告
- IT运维工程师数据中心网络延迟处理操作指南
- 成人术中外周静脉输液技术操作及管理指南
- 2026年艺术史考试指南:中外艺术史重难点及全题型预测试题
- 2026粉末冶金开发高速钢刀片脱模剂适用性分析报告
- 水泥毯的施工方案(3篇)
- 三角函数压轴题专项训练-2026年高考数学 含答案
- 中医执业医师考试《针灸学》巩固题及答案
- 2026年辽宁省公安厅招聘警务辅助人员笔试试题(含答案)
- 2025年国家烟草专卖局、中国烟草总公司招录笔试题库附答案解析
- 医院收款室安全生产制度
- 金波童话阅读试题及答案
- 线描画课件完整版
评论
0/150
提交评论