把while循环拿出来,不然鱼还没动乌龟就爬结束了import random
class Turtle():
def __init__(self):
self.x=random.randint(0,11)
self.y=random.randint(0,11)
self.power=100
def move(self):
x_weiyi=random.choice([-1,1,2,-2])
self.x+=x_weiyi
if self.x >= 0 and self.x <= 10:
self.x+=0
elif self.x<0:
self.x=0
elif self.x >10:
self.x=10
y_weiyi = random.choice([-1, 1, 2, -2])
self.y += y_weiyi
if self.y >= 0 and self.y <= 10:
self.y += 0
elif self.y < 0:
self.y = 0
elif self.y > 10:
self.y = 10
self.power-=1
return(self.x,self.y)
class Fish():
def __init__(self):
self.x=random.randint(0,11)
self.y=random.randint(0,11)
def move(self):
x_weiyi=random.choice([-1,1])
self.x+=x_weiyi
if self.x >= 0 and self.x <= 10:
self.x+=0
elif self.x<0:
self.x=0
elif self.x >10:
self.x=10
y_weiyi = random.choice([-1, 1])
self.y += y_weiyi
if self.y >= 0 and self.y <= 10:
self.y += 0
elif self.y < 0:
self.y = 0
elif self.y > 10:
self.y = 10
return (self.x, self.y)
fishes=[]
for i in range(0,10):
fish=Fish()
fishes.append(fish)
wugui=Turtle()
while wugui.power:
position = wugui.move()
for fish in fishes:
if fish.move()==position:
wugui.power+=20
fishes.remove(fish)
print('有一条鱼被吃掉了,乌龟的能量加20点。')
if len(fishes)==0:
print(f'鱼都给吃光了,游戏结束。乌龟能量为{wugui.power}')
break
if len(fishes) > 0:
print('乌龟都给累死啦!游戏结束。')
|