马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这是我在小甲鱼的基础上改的代码import random as f
class Turtle:
def __init__(self):
self.energy=100
self.x=f.randint(0,10)
self.y=f.randint(0,10)
def eat(self):
self.energy+=20
if self.energy>100:
self.energy=100
def move(self):
self.energy-=1
self.x_new=f.choice([1,2,-1,-2])+self.x
self.y_new=f.choice([1,2,-1,-2])+self.y
if self.x_new>10:
self.x=10-(self.x_new-10)
elif self.x_new<0:
self.x=-(self.x_new)
else:
self.x=self.x_new
if self.y_new>10:
self.y=10-(self.y_new-10)
elif self.y_new<0:
self.y=-(self.y_new)
else:
self.y=self.y_new
return (self.x,self.y)
class Fish:
def __init__(self):
self.x=f.randint(0,10)
self.y=f.randint(0,10)
def move(self):
self.x_new=f.choice([1,-1])+self.x
self.y_new=f.choice([1,-1])+self.y
if self.x_new>10:
self.x=10-(self.x_new-10)
elif self.x_new<0:
self.x=-(self.x_new)
else:
self.x=self.x_new
if self.y_new>10:
self.y=10-(self.y_new-10)
elif self.y_new<0:
self.y=-(self.y_new)
else:
self.y=self.y_new
return(self.x,self.y)
turtle=Turtle()
fish=Fish()
all_fish=10
while True:
if turtle.energy==0:
print('乌龟体力用光了,游戏结束')
break
if all_fish==0:
print('鱼被吃光了,游戏结束')
break
if fish.move()==turtle.move():
print('一条鱼被吃了')
all_fish-=1
turtle.eat()
else:
turtle.move()
fish.move()
为什么运行的结果最开始显示了吃鱼,但是多次运行后就只显示乌龟体力用光了呢?求大佬解答
|