from random import randint as 随机
class Gui: # 龟
hp = 100
def walk(self, x0):
mov = 随机(1, 2)
f = 随机(1, 4)
self.hp -= 1
if f == 1:
return [x0[0], bian(1, x0[1], mov)]
if f == 2:
return [x0[0], bian(0, x0[1], mov)]
if f == 3:
return [bian(0, x0[0], mov), x0[1]]
if f == 4:
return [bian(1, x0[0], mov), x0[1]]
class Yu:
hp = 1
def walk(self, x0):
if self.hp == 0:
return []
else:
f = 随机(1, 4)
if f == 1:
return [x0[0], bian(1, x0[1], 1)]
if f == 2:
return [x0[0], bian(0, x0[1], 1)]
if f == 3:
return [bian(0, x0[0], 1), x0[1]]
if f == 4:
return [bian(1, x0[0], 1), x0[1]]
# 判断边界
def bian(jt, w, b): # 进退 位置 步长
if jt == 1:
if w == 10:
return w - b
if w == 9:
if b == 2:
return w
else:
return w + b
else:
return w + b
if jt == 0:
if w == 0:
return w + b
if w == 1:
if b == 2:
return w
else:
return w - b
else:
return w - b
def show(al): # 生成图像 并返回死鱼数
dies = 0 # 死鱼数
for l in range(len(al)):
n = al[l]
if n:
a = n[0] + n[1] * 11
if l == 0:
tu2[a] = '* ' # *号代表乌龟
else:
tu2[a] = str(l-1)+' ' # 数字代表鱼
else:
dies += 1
for p in range(10, -1, -1):
for q in range(0, 11):
print(tu2[p * 11 + q], end='')
print(p)
print('0 1 2 3 4 5 6 7 8 9 10 \n吃了%d条 ' % dies, end='')
return dies
wei = [] # 存放所有的位置
g = Gui()
yu = [Yu(), Yu(), Yu(), Yu(), Yu(), Yu(), Yu(), Yu(), Yu(), Yu()]
tu = [] # 底图的列表
tu2 = [] # 实时图的列表
for iii in range(0, 121): # 创建一个121个字符串的列表方便显示
tu.append(' ') # 这里可以替换
for ii in range(11): # 随机生成11个位置,第一个是甲鱼
wei.append([随机(0, 10), 随机(0, 10)])
while g.hp:
tu2 = tu[:] # 刷新显示
wei[0] = g.walk(wei[0])
for i in range(1, 11):
wei[i] = yu[i-1].walk(wei[i])
# print(wei[0],wei[i])
if wei[i] == wei[0]:
# print('chi')
wei[i] = []
yu[i-1].hp = 0
g.hp += 20
die = show(wei)
print('HP =', g.hp)
if die == 10:
print('吃完啦')
break
if g.hp == 0:
print('饿死了')
print(wei)