第37课一屏一刷新,穿越时空的鱼C
本帖最后由 咖啡的旅游记 于 2018-5-21 22:49 编辑importrandom as r
#定义动物类,赋予移动的能力
class Animals:
def __init__(self):
self.mx=r.randint(1,10)
self.my=r.randint(1,10)
def move_xy(self,step_num=1):
#定义随机移动(一mx)
if r.randint(0,1):
self.mx -= step_num
else:
self.mx += step_num
#定义随机移动(一my)
if r.randint(0,1):
self.my -= step_num
else:
self.my += step_num
return
#定义乌龟类
class Tortoise(Animals):
def __init__(self):
super().__init__()
self.blood_volume=100
def eating(self):
return self.move_xy(2)
#定义鱼类
class Fish(Animals):
pass
#定义水池(边界限制)
class Pool:
def __init__(self):
self.x=10
self.y=10
self.tortoise=Tortoise()
self.fish=Fish()
self.fish_num=10
def moving(self):
n = 0
boo = True
fish_dict={}
to = self.tortoise
fs = self.fish
while boo:
to_XY = to.eating()
if to_XY < 0: to_XY = 0
if to_XY > self.x: to_XY = self.x
if to_XY < 0: to_XY = 1
if to_XY >self.y: to_XY = self.y
to.blood_volume -= 1
n += 1
for m in range(0,self.fish_num):
fs_XY = fs.move_xy()
if fs_XY < 0: fs_XY = 0
if fs_XY > self.x: fs_XY = self.x
if fs_XY < 0: fs_XY = 0
if fs_XY > self.y: fs_XY = self.y
fish_dict['鱼'+str(m)]=fs_XY
while self.fish_num
for each_xy in fish_dict.keys():
if to_XY == fish_dict:
to.blood_volume += 20
fish_dict.pop(each_xy)
print('%s在这个%s位置被乌龟吃掉了哦!呜呜!!!'%(each_xy,to_XY))
self.fish_num-=1
break
else:
break
#我的鱼会穿越量子时空
fish_dict.clear()
if to.blood_volume == 0 or self.fish_num == 0:
print("乌龟总计走了%s步,当前体力是%s,还剩余%s条鱼." % (n, to.blood_volume, self.fish_num))
print("游戏结束!!!")
boo = False
p=Pool()
p.moving()
。。。。。。
页:
[1]