|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#第038讲作业,一个作品。相比37讲做了以下调整1)调整了池塘大小,使得结果有赢有输;2)增加龟吃鱼时候的坐标输出
import random as r
legal_x=[0,7]
legal_y=[0,7]
class Turtle():
def __init__(self):
self.x=r.randint(legal_x[0],legal_x[1])
self.y=r.randint(legal_y[0],legal_y[1])
self.power=100
def gx(self):
return self.x
def gy(self):
return self.y
def gp(self):
return self.power
def move(self):
new_x=self.x+r.choice([-2,-1,1,2])
new_y=self.y+r.choice([-2,-1,1,2])
if new_x < legal_x[0]:
self.x = legal_x[0]-(new_x-legal_x[0])
elif new_x > legal_x[1]:
self.x = legal_x[1]-(new_x-legal_x[1])
else:
self.x = new_x
if new_y < legal_y[0]:
self.y = legal_y[0]-(new_y-legal_y[0])
elif new_y > legal_y[1]:
self.y = legal_y[1]-(new_y-legal_y[1])
else:
self.y = new_y
self.power -= 1
def eat(self):
self.power += 20
if self.power >100:
self.power = 100
class Fish():
def __init__(self):
self.x=r.randint(legal_x[0],legal_x[1])
self.y=r.randint(legal_y[0],legal_y[1])
def gx(self):
return self.x
def gy(self):
return self.y
def move(self):
new_x=self.x+r.choice([-1,1])
new_y=self.y+r.choice([-1,1])
if new_x < legal_x[0]:
self.x = legal_x[0]-(new_x-legal_x[0])
elif new_x > legal_x[1]:
self.x = legal_x[1]-(new_x-legal_x[1])
else:
self.x = new_x
if new_y < legal_y[0]:
self.y = legal_y[0]-(new_y-legal_y[0])
elif new_y > legal_y[1]:
self.y = legal_y[1]-(new_y-legal_y[1])
else:
self.y = new_y
t=Turtle()
f=[]
for i in range(10):
newfish=Fish()
f.append(newfish)
while 1:
#吃鱼
for each in f:
if each.gx()==t.gx() and each.gy()==t.gy():
print('坐标(%d,%d)发现小鱼,乌龟开始进食' % (each.gx(),each.gy()))
t.eat()
print('吃饱啦,现有能量:%d点' % t.gp())
f.remove(each)
#龟鱼移动
for each in f:
each.move()
t.move()
#判断结束标准
if len(f)==0:
print('鱼被吃光啦,游戏结束')
break
if t.gp()==0:
print('乌龟饿死啦,游戏结束')
break
|
|