|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
############################
#乌龟吃鱼小游戏
#
############################
import random as rd
#边缘检测函数
def check_boundary(x,y):
if x>10:
x=10-x%10
elif y>10:
y=10-y%10
elif x<0:
x=-x
elif y<0:
y=-y
else:
pass
return (x,y)
class Tortoise:
x=rd.randint(0,10)
y=rd.randint(0,10)
direct='h'
step=0
tortoise_energy=100 #乌龟能量
def move(self):
#选择移动方向
self.direct = rd.choice('xy')
#选择移动步长
self.step = rd.choice((-2,-1,1,2))
#移动
if 'x' == self.direct:
self.x+=self.step
else:
self.y+=self.step
#边缘检测
(self.x,self.y)=check_boundary(self.x,self.y)
#体力减1
self.tortoise_energy-=1
print('乌龟的位置为%d,%d'%(self.x,self.y))
return (self.x,self.y)
def eat(self):
self.tortoise_energy+=20
print('乌龟吃了一条小鱼 T_T')
class Fish:
x=rd.randint(0,10)
y=rd.randint(0,10)
direct='h'
step=0
def move(self):
#选择移动方向
self.direct = rd.choice('xy')
#选择移动步长
self.step = rd.choice((-1,1))
#移动
if 'x' == self.direct:
self.x+=self.step
else:
self.y+=self.step
#边缘检测
(self.x,self.y)=check_boundary(self.x,self.y)
print('鱼的位置为%d,%d'%(self.x,self.y))
return (self.x,self.y)
tortoise = Tortoise()
fish=[]
for i in range(10):
new_fish=Fish()
fish.append(new_fish)
while True:
if 0==len(fish):
print('鱼被吃光了')
break
if 0>=tortoise.tortoise_energy:
print('乌龟没体力了')
break
pos = tortoise.move()
for i in fish[:]:
if i.move()==pos:
fish.remove(i)
tortoise.eat()
|
|