|
|
发表于 2020-11-25 20:43:17
|
显示全部楼层
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部分删去就行了 |
|