学习android游戏开发的准备


学习android游戏开发的准备
2011年08月10日
  此文为翻译 作为自己的学习资料,可能有些地方翻译得不太准确,大家就抱着看看的态度就可以了。如果能学习东西当然是更好
  原文地址:http://www.rbgrn.net/content/54-getting-started-android-game-development
  如果你喜欢android平台游戏开发,那你需要知道很多基础东西。我是Light Racer,Light Racer 3D,Antigen,Deadly Chambers and Wixel的开发者,如今这些游戏都放置在android market上。在我开发游戏之前,我的第一个android 应用程序是原始的Light Racer 。我也学到很多关于写android游戏的经验,因此很愿意分享给大家。我甚至写了一本关于Light Racer 3D详细开发的在线书籍――《development of Light Racer 3D》。里面介绍了大量的基础知识和关键的代码片段。如果你之前已经有游戏开发的经验,那么跳转到移动开发平台将不会太难。只需要学习android的框架和api就可以了。如果你是刚刚学习游戏开发,那我这里提供一个必须知道的列表让你入门。它包括各种游戏的类型,如行动、策略、模拟和解题。
  Android的环境是以java为基础。因为java是比c++更容易上手的开发语言,也是移动开发的规范,再加上目前我也在用,所以这对新手将是一个很好的开头。Google在这方面也做了很多工作,比如提供文档形式的api和大量的例子。这些例子几乎100%的使用api的函数,因此也被叫做API Demos。如果你对java有一定的熟悉和已经在使用Eclipse了,那么开始第一个例子就会相当的容易。如果以前你从未学过任何的代码,那它将会随着你的前进不断吸引着你,不过不能气馁。如果你有过一些用c++开发的经验以及想用c++开发跨平台或者高质量的android游戏 ,那就看看BatteryTech,目前我正使用它来开发游戏。
   Get the SDK
  第一步启动android平台获取SDK,这SDK里包括核心的元件库,模拟器,工具和例子代码。推荐使用eclipse和android插件。如果你只是做android开发,那Eclipse IDE for Java Developers是个不错的选择。如果这是你第一个java开发的项目,你就要下载java SE JDK。
   Learn the application architecture
            首先理解android 应用框架是很重要。如果你不学习它,你后面学习解决游戏问题时将会困难重重。你应该去理解 Applications, Activities, Intents 和它们之间是如何联系的。Google已经提供了大量好的信息在这框架里。更重要的事情是理解为什么游戏需要多个activity组成和怎么样设计游戏才有好的用户体验。所有的都关联着activity的生命周期。
   Learn the activity lifecycle
  Activity 的生命周期由android系统管理。系统控制创建,恢复,暂停和销毁Activity的状态。想拥有好的操作的应用程序,那正确处理这些事件是非常重要。在设计游戏之前理解这些的工作原理可以省去你调试时间和设计耗费的时间。对于大多数应用,android默认的设置就可以满足大多数应用,但是对于游戏你或许需要去考虑设置单例标志。因为当设置的是默认时,android在适合的时候每次都会创建一个新的activity实例。对于游戏,你或许只需要一个activity实例。这就意味着你需要去管理这些事物的状态,但是对于我则需考虑解决一些资源管理的问题。
   The main loop
            无论你正在写什么类型的游戏,你或许有或许没有主循环。如果你的游戏不是依赖于时间或者只是单纯的回应用户的动作,除了等待用户输入,就不会其他视觉的变化,你可能不需要主循环。但是如果你正在写的游戏包括动画,时间或者其他自动化等等,你应该正视地考虑使用主线程。
  The main loop of a game is the part that "ticks" sub systems in a specific order and usually as many times per second as possible.
  主循环需要在自己的线程里跑动,因为所有更新UI界面的线程都必须在android主 UI线程里更新UI界面,更新界面执行的顺序通常如下:State, Input, AI, Physics, Animation, Sound and Video.
  情景的更新意味着要去管理情景的过渡,譬如游戏结束,人物选择或者下一情景。通常在一个情景你会去等待几秒钟,情景管理将会处理这些延迟和当时间到期时就设置下一情景.
  输入是通过用户操作键盘,滚动和碰触的来触发,在处理物理性质之前必须处理这输入,因为通常输入会影响到物理性质,因此首先处理输入将给游戏带来更有效的响应。在android中,主线程发生输入事件,因此你必须用代码来缓冲你的输入事件,这样主循环才有时间去执行。这不是困难的任务。定义字段,然后通过onKeyPressed 或者onTouchEvent 事件来保存下一个用户的输入。当这个输入是有效的输入时,将改变游戏的情景以及让物理性质处理响应它,来更新输入。
  AI的更新近似用户决定他们去“按”下一步。学习如何取写AI已经超出这文章的范围了,不过一般的想法是AI会像用户做的那样按按钮。这也会引起和响应物理性质的更新。
  物理性质的更新或许是或许不是真的物理。比如行动类的游戏,考虑上次时间的更新是关键点,当前时间会更新用户的输入,AI的输入已经决定所有事物是否发生碰撞。对于一个游戏,你在视觉上抓住一个物件并滑动它们,它将会是滑动的一部分,或者让它掉入其他地方。对于一个益智问答游戏,它将是答案正确与否的一部分。你还可以说出你别的什么东西,对于这篇文章所有的游戏都是这个游戏引擎的一部分。我把它指做物理性质。
  动画并不是放入一个gif动画那么简单。你需要在游戏的正确时间内描绘每一帧。这不像声音处理那样难,保存这些情景的字段比如:isDancing, danceFrame and lastDanceFrameTime 来使动画根据时间决定一下帧来更新。这是所有动画都这样做的。实际上显示动画的变化都是由视频来更新。
  声音的通过处理触发声音,停止声音,改变音量和改变声音的声调来更新。平常当写游戏时,声音的更新实际是通过产生一个bytes流来发送到声音的缓冲区,它是由android管理。因此你们可以选择SoundPool 或者MediaPlayer来使用到游戏里。They are both a little sensitive but know that because of some low level implementation details, small, low bitrate OGGs will yield the best performance results and the best stability.
  视频的更新要考虑到游戏的情景,人物的位置,分数,状态,行为和屏幕的绘画。如果使用主循环,你将要用到SurfaceView来绘画。与其他views对比,view本身会回调绘画的操作以及主循环将不得不做。SurfaceView拥有最高的帧每秒的速度以及在屏幕上最合适给游戏做动画或者移动部件的功能。All the video update should do is take the state of the game and draw it for this instance in time.  Any other automation is better handled by a different update task.
  代码是怎么样的呢,这里有个例子:
  public void run() {
  while (isRunning) {
  while (isPaused && isRunning) {
  sleep(100);
  }
  update();
  }
  }
  private void update() {
  updateState();
  updateInput();
  updateAI();
  updatePhysics();
  updateAnimations();
  updateSound();
  updateVideo();
  }
   3D or 2D?
  在开始你的游戏之前,你需要决定你打算往3D还是2D走。2D游戏有比较低的学习曲线通常更容易得到良好的性能。3D游戏需要更多深入的数学技能并且如果你不小心有可能出现性能的问题。他们还要求有使用建模工具的能力,像3D Studio和Maya。如果你打算要有形状复杂的箱子和圆圈。那使用OpenGL,Android支持OpenGL的3D编程以及在OpenGL里一定能找到很多好的教程去学习。
   Build simple, high quality methods
  在开始时一定要确定自己要避免在不要将方法写得太长。如果你遵照主循环模式,我上面所述,这是相当容易的。每个方法只能完成一个功能并保证没错误。比如:如果你需要洗桌子上的卡,你应该写个方法叫"shuffleCards",就只做洗卡的功能。
  练习代码在游戏开发中是一个很重要的部分。调试在有状态性,真实的系统里是非常困难的。尽量保持自己方法只有一个任务,不要一个方法里完成多个任务。如果你打算编写一个画场景背景的方法,可以叫"drawBackground"。这会让自己以后在复杂的系统里很快速的理解。
   It's all about efficiency!
  Performance is a major issue for any game.  The goal is to make the game as responsive as possible and to also look as smooth as possible.  Certain methods like Canvas.drawLine are going to be slow.  Also drawing an entire screen-sized bitmap onto the main canvas every frame will also be costly.  Balancing things like that is necessary to achieve the best performance.  Make sure to manage your resources well and use tricks to use the least amount of CPU to achieve your task.  Even the best game will not be very fun if it can't perform well.  People in general have little tolerance for choppiness or poor response.
   Tips and Tricks
  
Take a look at the example for LunarLander in the SDK.  It uses a SurfaceView and that would be the appropriate view to use for a game that needs the highest number of frames per second possible.  If you're going 3D, take a look at GLSurfaceView. It takes care of the OpenGL device initialization and provides a mechanism for rendering.  For LightRacer, I had to optimize the way I have everything drawn or else the framerate would be drastically lower.  I drew the background to a Bitmap only once which was when the view is initialized.  The light trails are in their own bitmap which gets updated as the racers move.  Those two bitmaps are drawn to the main canvas every frame with the racers drawn on top and then finally an explosion.  This technique made the game run at a playable rate.
  It's also a good practice to have your bitmaps be the exact size you intend to draw them on screen, if applicable.  This makes it so that no scaling is needed and will save some CPU.
  Use a consistent Bitmap Configuration (like RGBA8888) throughout the game.  This will save the graphics library CPU in having to translate the different formats.
  If you're determined to develop a 3D game but have no 3D knowledge, you will want to pick up a book or two on 3D game programming and study up on linear algebra.  At a bare minimum, you must understand dot products, cross products, vectors, unit vectors, normals, matrixes and translation.  The best book I have come across for this math is called Mathematics for 3D Game Programming and Computer Graphics.
  Keep the sound small and at a low bitrate.  The less there is to load, the faster loading times will be and the less memory the game will use.
  Use OGGs for sound, PNGs for graphics.
  Make sure to release all media players and null out all of your resources when the activity is destroyed.  This will ensure that the garbage collector gets to everything and that you don't have any memory leaks between launches of the game.
  Join the Android Google group and find community support.  There will be people that can help you along the way.
  Above all, spend time testing and retesting and making sure that every little thing works exactly the way you would expect it to.  Polishing the game up is the longest and hardest part of development.  If you rush it out to market, you will probably have a disappointed crowd and you may feel that all your hard work is wasted.  It's not possible to have 100% of people love what you write but you should at least try to put out the highest quality work that you can.

猜你喜欢

转载自yns74yns.iteye.com/blog/1359256