Android SDK实例之Snake游戏深入解析(一)

Snake是Android SDK例子程序中的一个小游戏,工程源代码:android-sdk-windows-1.5_r2\platforms\android-1.1\samples\Snake。这个小游戏在手机、游戏机、电脑中都出现过,游戏具体的玩法想必大家都知道,我主要是从Android开发应用程序的角度来分析这个游戏的框架、结构等。

首先将工程添加到Eclipes中,看源代码,分析类之间的关系,如下:
Snake-Class

整个工程主体也就3个类:Snake,SnakeView,TitleView。Activity,View,Handler是Android SDK提供的基础类,Coordinate,RefreshHandler是2个辅助类,也是SnakeView类中的内部类。其中,Coordinate就相当于是一个点的坐标(x,y),RefreshHandler的主要功能是将RefreshHandler对象绑定某个线程并与给它发送消息。Snake类是这个游戏的入口点,TitleView类负责游戏的绘画,SnakeView类负责对游戏控制操作的处理。

类的结构在上图中已经清晰说明,主要接口解析如下:

 

TitleView  extends  View

继承的接口

onSizeChanged()

在视图大小改变的时候调用,比如说手机由垂直旋转为水平

onDraw()

在视图需要重画的时候调用,比如说使用invalidate刷新界面上的某个矩形区域

Snake  extends  TitleView

继承的接口

onKeyDown()

监听用户键盘操作,并处理这些操作

新增加的接口

restoreState()/saveState()

恢复/保存游戏的数据

update()

重新计算当前游戏的数据

 

然后具体分析整个游戏的框架,任何一个游戏都需要有个引擎来推动游戏的运行,最简化的游戏引擎就是:在一个线程中While循环,检测用户操作,对用户的操作作出反应,更新游戏的界面,直到用户退出游戏。

 

先解析下RefreshHandler这个辅助类的作用,RefreshHandler继承至Handler,其主要作用就是把RefreshHandler与当前线程进行绑定,然后就可以直接给线程发送消息并处理消息,有一点需要强调的是:Handle对消息的处理都是异步。RefreshHandler在Handler的基础上增加sleep()接口,其主要作用就是每隔一个时间段后给当前线程发送一个消息;而handleMessage()则就是在接受消息后,让界面对当前的游戏状态作出处理,运行机制如下:

Snake-Core

实际上以上就相当于一个定时器,在特定的时刻发送消息,根据消息处理相应的事件。Update()与Sleep()这2个函数间接的相互调用就构成了一个循环。

需要注意的是:mRedrawHandle绑定的是Avtivity所在的线程,也就是程序的主线程;另外由于sleep()是个异步函数,所以Update()与Sleep()之间的相互调用才没有构成死循环。

最后分析下游戏数据的保存机制,如下:
Snake-SaveData

以上主要考虑了Activity的生命周期:如果用户在游戏期间离开游戏界面,游戏暂停;如果由于内存比较紧张,Android关闭游戏释放内存,那么当用户返回游戏界面的时候恢复到上次离开时的界面。

Snake源代码的内部细节,就不额外说明,在了解了框架后,再阅读代码就轻松多了。

总结说明

通过学习Snake这个简单的游戏,初步学习了如何开发游戏的界面、如何处理游戏的交互、如何保存游戏的数据等,这为我们以后开发更加复杂的游戏打好坚实的基础。

参考网址:http://www.moandroid.com/?p=474

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
List of Sample Apps The list below provides a summary of the sample applications that are available with the Android SDK. Using the links on this page, you can view the source files of the sample applications in your browser. You can also download the source of these samples into your SDK, then modify and reuse it as you need. For more information, see Getting the Samples. API Demos A variety of small applications that demonstrate an extensive collection of framework topics. Backup and Restore A simple example that illustrates a few different ways for an application to implement support for the Android data backup and restore mechanism. Bluetooth Chat An application for two-way text messaging over Bluetooth. BusinessCard An application that demonstrates how to launch the built-in contact picker from within an activity. This sample also uses reflection to ensure that the correct version of the contacts API is used, depending on which API level the application is running under. Contact Manager An application that demonstrates how to query the system contacts provider using the ContactsContract API, as well as insert contacts into a specific account. Home A home screen replacement application. JetBoy A game that demonstrates the SONiVOX JET interactive music technology, with JetPlayer. Live Wallpaper An application that demonstrates how to create a live wallpaper and bundle it in an application that users can install on their devices. Lunar Lander A classic Lunar Lander game. Multiple Resolutions A sample application that shows how to use resource directory qualifiers to provide different resources for different screen configurations. Note Pad An application for saving notes. Similar (but not identical) to the Notepad tutorial. SampleSyncAdapter Demonstrates how an application can communicate with a cloud-based service and synchronize its data with data stored locally in a content provider. The sample uses two related parts of the Android framework — the account manager and the synchronization manager (through a sync adapter). Searchable Dictionary A sample application that demonstrates Android's search framework, including how to provide search suggestions for Quick Search Box. Snake An implementation of the classic game "Snake." Soft Keyboard An example of writing an input method for a software keyboard. Spinner A simple application that serves as an application-under-test for the SpinnerTest sample application. SpinnerTest An example test application that contains test cases run against the Spinner sample application. To learn more about the application and how to run it, please read the Activity Testing tutorial. TicTacToeLib An example of an Android library project that provides a game-play Activity to any dependent application project. For an example of how an application can use the code and resources in an Android library project, see the TicTacToeMain sample application. TicTacToeMain An example of an Android application that makes use of code and resources provided in an Android library project. Specifically, this application uses code and resources provided in the TicTacToeLib library project. Wiktionary An example of creating interactive widgets for display on the Android home screen. Wiktionary (Simplified) A simple Android home screen widgets example.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鹿鸣天涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值