ImageVerifierCode 换一换
格式:DOCX , 页数:51 ,大小:865.91KB ,
资源ID:16428545      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/16428545.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(贪吃蛇Word文档格式.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

贪吃蛇Word文档格式.docx

1、如果用户在游戏期间离开游戏界面,游戏暂停;或者由于内存比较紧张,Android关闭游戏释放内存,那么当用户返回游戏界面的时候恢复到上次离开时的界面。三、源码解析详细解析下源代码,由于代码量不大,以注释的方式列出如下:1、Snake.javaview plaincopy to clipboardprint?1. /* 2. * Title: Snake 3. * Copyright: (C) 2007 The Android Open Source Project. Licensed under the Apache License, Version 2.0 (the License)4. *

2、author Gavin 标注 5. */ 6. package com.deaboway.snake;7. import android.app.Activity;8. import android.os.Bundle;9. import android.widget.TextView;10. /* 11. * Snake: a simple game that everyone can enjoy. 12. * 13. * This is an implementation of the classic Game Snake, in which you control a 14. * se

3、rpent roaming around the garden looking for apples. Be careful, though, 15. * because when you catch one, not only will you become longer, but youll move 16. * faster. Running into yourself or the walls will end the game. 17. * 18. */ 19. / 贪吃蛇: 经典游戏,在一个花园中找苹果吃,吃了苹果会变长,速度变快。碰到自己和墙就挂掉。20. public clas

4、s Snake extends Activity 21. private SnakeView mSnakeView;22. private static String ICICLE_KEY = snake-view;23. /* 24. * Called when Activity is first created. Turns off the title bar, sets up 25. * the content views, and fires up the SnakeView. 26. * 27. */ 28. / 在 activity 第一次创建时被调用 29. Override 3

5、0. public void onCreate(Bundle savedInstanceState) 31. super.onCreate(savedInstanceState);32. setContentView(R.layout.snake_layout);33. mSnakeView = (SnakeView) findViewById(R.id.snake);34. mSnakeView.setTextView(TextView) findViewById(R.id.text);35. / 检查存贮状态以确定是重新开始还是恢复状态 36. if (savedInstanceState

6、 = null) 37. / 存储状态为空,说明刚启动可以切换到准备状态 38. mSnakeView.setMode(SnakeView.READY);39. else 40. / 已经保存过,那么就去恢复原有状态 41. Bundle map = savedInstanceState.getBundle(ICICLE_KEY);42. if (map != null) 43. / 恢复状态 44. mSnakeView.restoreState(map);45. else 46. / 设置状态为暂停 47. mSnakeView.setMode(SnakeView.PAUSE);48. 4

7、9. 50. 51. / 暂停事件被触发时 52. Override 53. protected void onPause() 54. super.onPause();55. / Pause the game along with the activity 56. mSnakeView.setMode(SnakeView.PAUSE);57. 58. / 状态保存 59. Override 60. public void onSaveInstanceState(Bundle outState) 61. / 存储游戏状态到View里 62. outState.putBundle(ICICLE_K

8、EY, mSnakeView.saveState();63. 64. 2、SnakeView.java7. import java.util.ArrayList;8. import java.util.Random;9. import android.content.Context;10. import android.content.res.Resources;11. import android.os.Handler;12. import android.os.Message;13. import android.util.AttributeSet;14. import android.o

9、s.Bundle;15. import android.util.Log;16. import android.view.KeyEvent;17. import android.view.View;18. import android.widget.TextView;19. /* 20. * SnakeView: implementation of a simple game of Snake 21. * 22. * 23. */ 24. public class SnakeView extends TileView 25. private static final String TAG =

10、Deaboway26. /* 27. * Current mode of application: READY to run, RUNNING, or you have already 28. * lost. static final ints are used instead of an enum for performance 29. * reasons. 30. */ 31. / 游戏状态,默认值是准备状态 32. private int mMode = READY;33. / 游戏的四个状态 暂停 准备 运行 和 失败 34. public static final int PAUSE

11、 = 0;35. public static final int READY = 1;36. public static final int RUNNING = 2;37. public static final int LOSE = 3;38. / 游戏中蛇的前进方向,默认值北方 39. private int mDirection = NORTH;40. / 下一步的移动方向,默认值北方 41. private int mNextDirection = NORTH;42. / 游戏方向设定 北 南 东 西 43. private static final int NORTH = 1;44.

12、 private static final int SOUTH = 2;45. private static final int EAST = 3;46. private static final int WEST = 4;47. /* 48. * Labels for the drawables that will be loaded into the TileView class 49. */ 50. / 三种游戏元 51. private static final int RED_STAR = 1;52. private static final int YELLOW_STAR = 2;

13、53. private static final int GREEN_STAR = 3;54. /* 55. * mScore: used to track the number of apples captured mMoveDelay: number of 56. * milliseconds between snake movements. This will decrease as apples are 57. * captured. 58. */ 59. / 游戏得分 60. private long mScore = 0;61. / 移动延迟 62. private long mM

14、oveDelay = 600;63. /* 64. * mLastMove: tracks the absolute time when the snake last moved, and is 65. * used to determine if a move should be made based on mMoveDelay. 66. */ 67. / 最后一次移动时的毫秒时刻 68. private long mLastMove;69. /* 70. * mStatusText: text shows to the user in some run states 71. */ 72.

15、/ 显示游戏状态的文本组件 73. private TextView mStatusText;74. /* 75. * mSnakeTrail: a list of Coordinates that make up the snakes body 76. * mAppleList: the secret location of the juicy apples the snake craves. 77. */ 78. / 蛇身数组(数组以坐标对象为元素) 79. private ArrayList mSnakeTrail = new ArrayList();80. / 苹果数组(数组以坐标对象

16、为元素) 81. private ArrayList mAppleList = new ArrayList82. /* 83. * Everyone needs a little randomness in their life 84. */ 85. / 随机数 86. private static final Random RNG = new Random();87. /* 88. * Create a simple handler that we can use to cause animation to happen. We 89. * set ourselves as a target

17、 and we can use the sleep() function to cause an 90. * update/invalidate to occur at a later date. 91. */ 92. / 创建一个Refresh Handler来产生动画: 通过sleep()来实现 93. private RefreshHandler mRedrawHandler = new RefreshHandler();94. / 一个Handler 95. class RefreshHandler extends Handler 96. / 处理消息队列 97. Override 9

18、8. public void handleMessage(Message msg) 99. / 更新View对象 100. SnakeView.this.update();101. / 强制重绘 102. SnakeView.this.invalidate();103. 104. / 延迟发送消息 105. public void sleep(long delayMillis) 106. this.removeMessages(0);107. sendMessageDelayed(obtainMessage(0), delayMillis);108. 109. ;110. /* 111. *

19、Constructs a SnakeView based on inflation from XML 112. * 113. * param context 114. * param attrs 115. */ 116. / 构造函数 117. public SnakeView(Context context, AttributeSet attrs) 118. super(context, attrs);119. / 构造时初始化 120. initSnakeView();121. 122. public SnakeView(Context context, AttributeSet attr

20、s, int defStyle) 123. super(context, attrs, defStyle);124. initSnakeView();125. 126. / 初始化 127. private void initSnakeView() 128. / 可选焦点 129. setFocusable(true);130. Resources r = this.getContext().getResources();131. / 设置贴片图片数组 132. resetTiles(4);133. / 把三种图片存到Bitmap对象数组 134. loadTile(RED_STAR, r.g

21、etDrawable(R.drawable.redstar);135. loadTile(YELLOW_STAR, r.getDrawable(R.drawable.yellowstar);136. loadTile(GREEN_STAR, r.getDrawable(R.drawable.greenstar);137. 138. / 开始新的游戏初始化 139. private void initNewGame() 140. / 清空ArrayList列表 141. mSnakeTrail.clear();142. mAppleList.clear();143. / For now were

22、 just going to load up a short default eastbound snake 144. / thats just turned north 145. / 创建蛇身 146. mSnakeTrail.add(new Coordinate(7, 7);147. mSnakeTrail.add(new Coordinate(6, 7);148. mSnakeTrail.add(new Coordinate(5, 7);149. mSnakeTrail.add(new Coordinate(4, 7);150. mSnakeTrail.add(new Coordinat

23、e(3, 7);151. mSnakeTrail.add(new Coordinate(2, 7);152. / 新的方向 :北方 153. mNextDirection = NORTH;154. / 2个随机位置的苹果 155. addRandomApple();156. addRandomApple();157. / 移动延迟 158. mMoveDelay = 600;159. / 初始得分0 160. mScore = 0;161. 3、TileView.java7. import android.content.Context;8. import android.content.re

24、s.TypedArray;9. import android.graphics.Bitmap;10. import android.graphics.Canvas;11. import android.graphics.Paint;12. import android.graphics.drawable.Drawable;14. import android.view.View;15. /* 16. * TileView: a View-variant designed for handling arrays of icons or other 17. * drawables. 18. * 19. */ 20. / View 变种,用来处理 一组 贴片 “icons”或其它可绘制的对象 21. public class TileView extends View 22. /* 23. * Parameters controlling the size of the tiles and their range within view. 24. * Width/Height are in pixels, and Drawables will be scaled to fit to these 25. * dimensions. X/Y Tile Counts are the

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1