全球旧事资料 分类
通过for循环来检查第二种情况。代码如下:
privateboolCheckDead检查游戏是否结束s
akeBlockheads
akeBlockthis_blocks0超出画布
范围ifheadPoi
tX10headPoi
tY10
headPoi
tXthis_width1headPoi
tYthis_height1retur
true
fori
ti1ithis_blocksCou
ti
s
akeBlockbs
akeBlockthis_blocksi是否头尾相接
ifheadPoi
tXbPoi
tXheadPoi
tYbPoi
tY
this_isGameOvertrueretur
true
this_isGameOverfalseretur
false
d、GetFood函数用于生成下一个食物,其实就是一个蛇块,生成的规则就是,坐标要在画布范围内,并且食物的坐标不能和贪吃蛇的坐标重合,具体代码如下通过for循环检查食物坐标是否和贪吃蛇的蛇块列表ArrayList里的蛇块有冲突。代码如下:
fprivates
akeBlockGetFood生成下一个食物,也就是下一节蛇块s
akeBlockfood
ullRa
domr
ewRa
domboolredofalsewhiletrueredofalsei
txrNextthis_widthi
tyrNextthis_heightfori
ti0ithis_blocksCou
tis
akeBlockbs
akeBlockthis_blocksiifbPoi
tXxbPoi
tYyredotrueifredofalse
food
ews
akeBlockColorGoldthis_size
ewPoi
txy
break
retur
food


e、Move函数用于更新整个蛇块的坐标,我们前面通过将蛇块信息放到ArrayList里来表示贪吃蛇的整个信息,其中根据下标从0到Cou
t1依次表示各个蛇块的信息,实际代码中增加了吃食物的情况判断,也就是说如果新蛇头的坐标和食物的坐标一致,那么就不做删除原蛇尾的操作了,这样产生的效果就是蛇块列表的长度增加了一个,达到吃食物变长的效果。代码如下:
privatevoidMove
蛇体移动

Poi
tp
ewPoi
t下一个位置
s
akeBlockheads
akeBlockthis_blocks0
ifthis_directio
Directio
Up
p
ewPoi
theadPoi
tXheadPoi
tY1
elseifthis_directio
Directio
Dow

p
ewPoi
theadPoi
tXheadPoi
tY1
felseifthis_directio
Directio
Left
p
ewPoi
theadPoi
tX1headPoi
tY
else
p
ewPoi
theadPoi
tX1headPoi
tY

生成新坐标,将成为蛇头
s
akeBlockb
ews
akeBlockColorRedthis_sizep
如果下一个坐标不是当前食物坐标,那么从蛇块信息列表中删除最后一个蛇块
ifthis_foodPoi
tp
this_blocksRemoveAtthis_blocksCou
t1
如果下一个坐标和食物坐标重合,那么就生成一个新食物
else

this_foodthisGetFood把下一个坐标插入到蛇
块信息列表的第一个,使其成为蛇头
fe
5

this_blocksI
sert0b
将元素插入指
定索引处
thisPai
tPalettethis_gpPalette
更新画板

f、Pai
tPalette函数需要一个r
好听全球资料 返回顶部