import random
class Turtle: # 乌龟
def __init__(self):
self.hp = 100
self.x = random.randint(1, 10)
self.y = random.randint(1, 10)
self.pos = (self.x, self.y)
def move(self):
self.hp -= 1
self.x = (self.x + random.choice(range(-2, 3)))%10
self.y = (self.y + random.choice(range(-2, 3)))%10
self.pos = (self.x, self.y)
class Fish: # 一条鱼
def __init__(self):
self.x = random.randint(1, 10)
self.y = random.randint(1, 10)
self.pos = (self.x, self.y)
def move(self):
self.x = (self.x + random.choice(range(-2, 3)))%10
self.y = (self.y + random.choice(range(-2, 3)))%10
self.pos = (self.x, self.y)
class GroupFish: # 一群鱼
def __init__(self):
self.fish = [Fish() for _ in range(10)]
def move(self):
for fish in self.fish:
fish.move()
def eaten(self, index):
self.fish.pop(index)
def main():
turtle = Turtle()
fish = GroupFish()
while True:
if not turtle.hp: # 当乌龟体力耗尽
print(f"坐标:{turtle.pos} 乌龟体力耗尽")
break
elif not len(fish.fish): # 当鱼群被吃完
print("鱼被吃完了")
break
else:
for n, f in enumerate(fish.fish): # 检查全部鱼的坐标是否和乌龟相遇?
if f.pos == turtle.pos:
print(f"坐标:{f.pos} 鱼被乌龟吃掉了")
fish.eaten(n)
turtle.hp += 20
if turtle.hp > 100: # 预设乌龟体力上限为 100
turtle.hp = 100
turtle.move()
fish.move()
main()
坐标:(0, 1) 鱼被乌龟吃掉了
坐标:(8, 0) 鱼被乌龟吃掉了
坐标:(1, 6) 鱼被乌龟吃掉了
坐标:(0, 0) 鱼被乌龟吃掉了
坐标:(9, 0) 鱼被乌龟吃掉了
坐标:(5, 7) 鱼被乌龟吃掉了
坐标:(2, 3) 鱼被乌龟吃掉了
坐标:(8, 7) 鱼被乌龟吃掉了
坐标:(6, 8) 鱼被乌龟吃掉了
坐标:(4, 3) 鱼被乌龟吃掉了
鱼被吃完了
坐标:(3, 1) 鱼被乌龟吃掉了
坐标:(6, 1) 鱼被乌龟吃掉了
坐标:(8, 6) 鱼被乌龟吃掉了
坐标:(6, 3) 鱼被乌龟吃掉了
坐标:(4, 0) 鱼被乌龟吃掉了
坐标:(3, 4) 鱼被乌龟吃掉了
坐标:(5, 4) 鱼被乌龟吃掉了
坐标:(3, 6) 鱼被乌龟吃掉了
坐标:(9, 5) 鱼被乌龟吃掉了
坐标:(0, 7) 乌龟体力耗尽
|