snake游戏分析(排版)

上传人:第*** 文档编号:31080673 上传时间:2018-02-04 格式:DOC 页数:27 大小:496KB
返回 下载 相关 举报
snake游戏分析(排版)_第1页
第1页 / 共27页
snake游戏分析(排版)_第2页
第2页 / 共27页
snake游戏分析(排版)_第3页
第3页 / 共27页
snake游戏分析(排版)_第4页
第4页 / 共27页
snake游戏分析(排版)_第5页
第5页 / 共27页
点击查看更多>>
资源描述

《snake游戏分析(排版)》由会员分享,可在线阅读,更多相关《snake游戏分析(排版)(27页珍藏版)》请在金锄头文库上搜索。

1、Snake 游戏分析Snake也是一个经典游戏了, Nokia 蓝屏机的王牌游戏之一。 Android SDK 1.5就有了它的身影。我们这里就来详细解析一下 Android SDK Sample 中的 Snake工程。本工程基于 SDK 2.3.3 版本中的工程,路径为: %Android_SDK_HOME% /samples/android-10/Snake 一、 Eclipse 工程 通过 File-New Project-Android-Android Project,选择“ Create project from existing sample”创建自己的应用 SnakeAndroi

2、d,如下图: 运行效果如下图: 二、工程结构和类图 其实 Snake 的工程蛮简单的,源文件就三个: Snake.java SnakeView.java TileView.java。 Snake 类是这个游戏的入口点, TitleView 类进行游戏的绘画, SnakeView 类则是对游戏控制操作的处理。 Coordinate, RefreshHandler是 2 个辅助类,也是 SnakeView 类中的内部类。其中, Coordinate是一个点的坐标( x, y), RefreshHandler 将 RefreshHandler对象绑定某个线程并给它发送消息。如下图: 任何游戏都需要有

3、个引擎来推动游戏的运行,最简化的游戏引擎就是:在一个线程中 While 循环,检测用户操作,对用户的操作作出反应,更新游戏的界面,直到用户退出游戏。 在 Snake 这个游戏中,辅助类 RefreshHandler 继承自 Handler,用来把 RefreshHandler与当前线程进行绑定,从而可以直接给线程发送消息并处理消息。注意一点: Handle 对消息的处理都是异步。 RefreshHandler 在 Handler的基础上增加 sleep()接口,用来每隔一个时间段后给当前线程发送一个消息。 handleMessage()方法在接受消息后,根据当前的游戏状态重绘界面,运行机制如下

4、: 运行机制 这比较类似定时器的概念,在特定的时刻发送消息,根据消息处理相应的事件。update()与 sleep()间接的相互调用就构成了一个循环。这里要注意: mRedrawHandle绑定的是 Avtivity 所在的线程,也就是程序的主线程;另外由于 sleep()是个异步函数,所以 update()与 sleep()之间的相互调用才没有构成死循环。 最后分析下游戏数据的保存机制,如下: 这里考虑了 Activity 的生命周期:如果用户在游戏期间离开游戏界面,游戏暂停;或者由于内存比较紧张, Android 关闭游戏释放内存,那么当用户返回游戏界面的时候恢复到上次离开时的界面。 三、

5、源码解析 详细解析下源代码,由于代码量不大,以注释的方式列出如下: 1、 Snake.java Java代码 /* * Title: Snake * Copyright: (C) 2007 The Android Open Source Project. Licensed under the Apache License, Version 2.0 (the License) * author Gavin 标注 */ package com.deaboway.snake; import android.app.Activity; import android.os.Bundle; import

6、android.widget.TextView; /* * Snake: a simple game that everyone can enjoy. * * This is an implementation of the classic Game Snake, in which you control a * serpent roaming around the garden looking for apples. Be careful, though, * because when you catch one, not only will you become longer, but y

7、oull move * faster. Running into yourself or the walls will end the game. * */ / 贪吃蛇: 经典游戏,在一个花园中找苹果吃,吃了苹果会变长,速度变快。碰到自己和墙就挂掉。 public class Snake extends Activity private SnakeView mSnakeView; private static String ICICLE_KEY = snake-view; /* * Called when Activity is first created. Turns off the tit

8、le bar, sets up * the content views, and fires up the SnakeView. * */ / 在 activity 第一次创建时被调用 Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.snake_layout); mSnakeView = (SnakeView) findViewById(R.id.snake); mSnakeView.setTextView(T

9、extView) findViewById(R.id.text); / 检查存贮状态以确定是重新开始还是恢复状态 if (savedInstanceState = null) / 存储状态为空,说明刚启动可以切换到准备状态 mSnakeView.setMode(SnakeView.READY); else / 已经保存过,那么就去恢复原有状态 Bundle map = savedInstanceState.getBundle(ICICLE_KEY); if (map != null) / 恢复状态 mSnakeView.restoreState(map); else / 设置状态为暂停 mSn

10、akeView.setMode(SnakeView.PAUSE); / 暂停事件被触发时 Override protected void onPause() super.onPause(); / Pause the game along with the activity mSnakeView.setMode(SnakeView.PAUSE); / 状态保存 Override public void onSaveInstanceState(Bundle outState) / 存储游戏状态到 View里 outState.putBundle(ICICLE_KEY, mSnakeView.sav

11、eState(); 2、 SnakeView.java Java代码 /* * Title: Snake * Copyright: (C) 2007 The Android Open Source Project. Licensed under the Apache License, Version 2.0 (the License) * author Gavin 标注 */ package com.deaboway.snake; import java.util.ArrayList; import java.util.Random; import android.content.Contex

12、t; import android.content.res.Resources; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.widget.TextView; /* * SnakeView: implementation o

13、f a simple game of Snake * * */ public class SnakeView extends TileView private static final String TAG = Deaboway; /* * Current mode of application: READY to run, RUNNING, or you have already * lost. static final ints are used instead of an enum for performance * reasons. */ / 游戏状态,默认值是准备状态 private

14、 int mMode = READY; / 游戏的四个状态 暂停 准备 运行 和 失败 public static final int PAUSE = 0; public static final int READY = 1; public static final int RUNNING = 2; public static final int LOSE = 3; / 游戏中蛇的前进方向,默认值北方 private int mDirection = NORTH; / 下一步的移动方向,默认值北方 private int mNextDirection = NORTH; / 游戏方向设定 北 南

15、 东 西 private static final int NORTH = 1; private static final int SOUTH = 2; private static final int EAST = 3; private static final int WEST = 4; /* * Labels for the drawables that will be loaded into the TileViewclass */ / 三种游戏元 private static final int RED_STAR = 1; private static final int YELLOW_STAR = 2; private static final int GREEN_STAR = 3; /* * mScore: use

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号