贪吃蛇Word文档格式.docx

上传人:b****5 文档编号:16428545 上传时间:2022-11-23 格式:DOCX 页数:51 大小:865.91KB
下载 相关 举报
贪吃蛇Word文档格式.docx_第1页
第1页 / 共51页
贪吃蛇Word文档格式.docx_第2页
第2页 / 共51页
贪吃蛇Word文档格式.docx_第3页
第3页 / 共51页
贪吃蛇Word文档格式.docx_第4页
第4页 / 共51页
贪吃蛇Word文档格式.docx_第5页
第5页 / 共51页
点击查看更多>>
下载资源
资源描述

贪吃蛇Word文档格式.docx

《贪吃蛇Word文档格式.docx》由会员分享,可在线阅读,更多相关《贪吃蛇Word文档格式.docx(51页珍藏版)》请在冰豆网上搜索。

贪吃蛇Word文档格式.docx

如果用户在游戏期间离开游戏界面,游戏暂停;

或者由于内存比较紧张,Android关闭游戏释放内存,那么当用户返回游戏界面的时候恢复到上次离开时的界面。

三、源码解析

详细解析下源代码,由于代码量不大,以注释的方式列出如下:

1、Snake.java

viewplaincopytoclipboardprint?

1./**

2.*<

p>

Title:

Snake<

/p>

3.*<

Copyright:

(C)2007TheAndroidOpenSourceProject.LicensedundertheApacheLicense,Version2.0(the"

License"

)<

4.*@authorGavin标注

5.*/

6.packagecom.deaboway.snake;

7.importandroid.app.Activity;

8.importandroid.os.Bundle;

9.importandroid.widget.TextView;

10./**

11.*Snake:

asimplegamethateveryonecanenjoy.

12.*

13.*ThisisanimplementationoftheclassicGame"

Snake"

inwhichyoucontrola

14.*serpentroamingaroundthegardenlookingforapples.Becareful,though,

15.*becausewhenyoucatchone,notonlywillyoubecomelonger,butyou'

llmove

16.*faster.Runningintoyourselforthewallswillendthegame.

17.*

18.*/

19.//贪吃蛇:

经典游戏,在一个花园中找苹果吃,吃了苹果会变长,速度变快。

碰到自己和墙就挂掉。

20.publicclassSnakeextendsActivity{

21.privateSnakeViewmSnakeView;

22.privatestaticStringICICLE_KEY="

snake-view"

;

23./**

24.*CalledwhenActivityisfirstcreated.Turnsoffthetitlebar,setsup

25.*thecontentviews,andfiresuptheSnakeView.

26.*

27.*/

28.//在activity第一次创建时被调用

29.@Override

30.publicvoidonCreate(BundlesavedInstanceState){

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==null){

37.//存储状态为空,说明刚启动可以切换到准备状态

38.mSnakeView.setMode(SnakeView.READY);

39.}else{

40.//已经保存过,那么就去恢复原有状态

41.Bundlemap=savedInstanceState.getBundle(ICICLE_KEY);

42.if(map!

=null){

43.//恢复状态

44.mSnakeView.restoreState(map);

45.}else{

46.//设置状态为暂停

47.mSnakeView.setMode(SnakeView.PAUSE);

48.}

49.}

50.}

51.//暂停事件被触发时

52.@Override

53.protectedvoidonPause(){

54.super.onPause();

55.//Pausethegamealongwiththeactivity

56.mSnakeView.setMode(SnakeView.PAUSE);

57.}

58.//状态保存

59.@Override

60.publicvoidonSaveInstanceState(BundleoutState){

61.//存储游戏状态到View里

62.outState.putBundle(ICICLE_KEY,mSnakeView.saveState());

63.}

64.}

2、SnakeView.java

7.importjava.util.ArrayList;

8.importjava.util.Random;

9.importandroid.content.Context;

10.importandroid.content.res.Resources;

11.importandroid.os.Handler;

12.importandroid.os.Message;

13.importandroid.util.AttributeSet;

14.importandroid.os.Bundle;

15.importandroid.util.Log;

16.importandroid.view.KeyEvent;

17.importandroid.view.View;

18.importandroid.widget.TextView;

19./**

20.*SnakeView:

implementationofasimplegameofSnake

21.*

22.*

23.*/

24.publicclassSnakeViewextendsTileView{

25.privatestaticfinalStringTAG="

Deaboway"

26./**

27.*Currentmodeofapplication:

READYtorun,RUNNING,oryouhavealready

28.*lost.staticfinalintsareusedinsteadofanenumforperformance

29.*reasons.

30.*/

31.//游戏状态,默认值是准备状态

32.privateintmMode=READY;

33.//游戏的四个状态暂停准备运行和失败

34.publicstaticfinalintPAUSE=0;

35.publicstaticfinalintREADY=1;

36.publicstaticfinalintRUNNING=2;

37.publicstaticfinalintLOSE=3;

38.//游戏中蛇的前进方向,默认值北方

39.privateintmDirection=NORTH;

40.//下一步的移动方向,默认值北方

41.privateintmNextDirection=NORTH;

42.//游戏方向设定北南东西

43.privatestaticfinalintNORTH=1;

44.privatestaticfinalintSOUTH=2;

45.privatestaticfinalintEAST=3;

46.privatestaticfinalintWEST=4;

47./**

48.*LabelsforthedrawablesthatwillbeloadedintotheTileViewclass

49.*/

50.//三种游戏元

51.privatestaticfinalintRED_STAR=1;

52.privatestaticfinalintYELLOW_STAR=2;

53.privatestaticfinalintGREEN_STAR=3;

54./**

55.*mScore:

usedtotrackthenumberofapplescapturedmMoveDelay:

numberof

56.*millisecondsbetweensnakemovements.Thiswilldecreaseasapplesare

57.*captured.

58.*/

59.//游戏得分

60.privatelongmScore=0;

61.//移动延迟

62.privatelongmMoveDelay=600;

63./**

64.*mLastMove:

trackstheabsolutetimewhenthesnakelastmoved,andis

65.*usedtodetermineifamoveshouldbemadebasedonmMoveDelay.

66.*/

67.//最后一次移动时的毫秒时刻

68.privatelongmLastMove;

69./**

70.*mStatusText:

textshowstotheuserinsomerunstates

71.*/

72.//显示游戏状态的文本组件

73.privateTextViewmStatusText;

74./**

75.*mSnakeTrail:

alistofCoordinatesthatmakeupthesnake'

sbody

76.*mAppleList:

thesecretlocationofthejuicyapplesthesnakecraves.

77.*/

78.//蛇身数组(数组以坐标对象为元素)

79.privateArrayList<

Coordinate>

mSnakeTrail=newArrayList<

();

80.//苹果数组(数组以坐标对象为元素)

81.privateArrayList<

mAppleList=newArrayList<

82./**

83.*Everyoneneedsalittlerandomnessintheirlife

84.*/

85.//随机数

86.privatestaticfinalRandomRNG=newRandom();

87./**

88.*Createasimplehandlerthatwecanusetocauseanimationtohappen.We

89.*setourselvesasatargetandwecanusethesleep()functiontocausean

90.*update/invalidatetooccuratalaterdate.

91.*/

92.//创建一个RefreshHandler来产生动画:

通过sleep()来实现

93.privateRefreshHandlermRedrawHandler=newRefreshHandler();

94.//一个Handler

95.classRefreshHandlerextendsHandler{

96.//处理消息队列

97.@Override

98.publicvoidhandleMessage(Messagemsg){

99.//更新View对象

100.SnakeView.this.update();

101.//强制重绘

102.SnakeView.this.invalidate();

103.}

104.//延迟发送消息

105.publicvoidsleep(longdelayMillis){

106.this.removeMessages(0);

107.sendMessageDelayed(obtainMessage(0),delayMillis);

108.}

109.};

110./**

111.*ConstructsaSnakeViewbasedoninflationfromXML

112.*

113.*@paramcontext

114.*@paramattrs

115.*/

116.//构造函数

117.publicSnakeView(Contextcontext,AttributeSetattrs){

118.super(context,attrs);

119.//构造时初始化

120.initSnakeView();

121.}

122.publicSnakeView(Contextcontext,AttributeSetattrs,intdefStyle){

123.super(context,attrs,defStyle);

124.initSnakeView();

125.}

126.//初始化

127.privatevoidinitSnakeView(){

128.//可选焦点

129.setFocusable(true);

130.Resourcesr=this.getContext().getResources();

131.//设置贴片图片数组

132.resetTiles(4);

133.//把三种图片存到Bitmap对象数组

134.loadTile(RED_STAR,r.getDrawable(R.drawable.redstar));

135.loadTile(YELLOW_STAR,r.getDrawable(R.drawable.yellowstar));

136.loadTile(GREEN_STAR,r.getDrawable(R.drawable.greenstar));

137.}

138.//开始新的游戏——初始化

139.privatevoidinitNewGame(){

140.//清空ArrayList列表

141.mSnakeTrail.clear();

142.mAppleList.clear();

143.//Fornowwe'

rejustgoingtoloadupashortdefaulteastboundsnake

144.//that'

sjustturnednorth

145.//创建蛇身

146.mSnakeTrail.add(newCoordinate(7,7));

147.mSnakeTrail.add(newCoordinate(6,7));

148.mSnakeTrail.add(newCoordinate(5,7));

149.mSnakeTrail.add(newCoordinate(4,7));

150.mSnakeTrail.add(newCoordinate(3,7));

151.mSnakeTrail.add(newCoordinate(2,7));

152.//新的方向:

北方

153.mNextDirection=NORTH;

154.//2个随机位置的苹果

155.addRandomApple();

156.addRandomApple();

157.//移动延迟

158.mMoveDelay=600;

159.//初始得分0

160.mScore=0;

161.}

3、TileView.java

7.importandroid.content.Context;

8.importandroid.content.res.TypedArray;

9.importandroid.graphics.Bitmap;

10.importandroid.graphics.Canvas;

11.importandroid.graphics.Paint;

12.importandroid.graphics.drawable.Drawable;

14.importandroid.view.View;

15./**

16.*TileView:

aView-variantdesignedforhandlingarraysof"

icons"

orother

17.*drawables.

18.*

19.*/

20.//View变种,用来处理一组贴片——“icons”或其它可绘制的对象

21.publicclassTileViewextendsView{

22./**

23.*Parameterscontrollingthesizeofthetilesandtheirrangewithinview.

24.*Width/Heightareinpixels,andDrawableswillbescaledtofittothese

25.*dimensions.X/YTileCountsarethe

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育 > 数学

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

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