自己编程小游戏
#第038讲作业,一个作品。相比37讲做了以下调整1)调整了池塘大小,使得结果有赢有输;2)增加龟吃鱼时候的坐标输出import random as r
legal_x=
legal_y=
class Turtle():
def __init__(self):
self.x=r.randint(legal_x,legal_x)
self.y=r.randint(legal_y,legal_y)
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:
self.x = legal_x-(new_x-legal_x)
elif new_x > legal_x:
self.x = legal_x-(new_x-legal_x)
else:
self.x = new_x
if new_y < legal_y:
self.y = legal_y-(new_y-legal_y)
elif new_y > legal_y:
self.y = legal_y-(new_y-legal_y)
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,legal_x)
self.y=r.randint(legal_y,legal_y)
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:
self.x = legal_x-(new_x-legal_x)
elif new_x > legal_x:
self.x = legal_x-(new_x-legal_x)
else:
self.x = new_x
if new_y < legal_y:
self.y = legal_y-(new_y-legal_y)
elif new_y > legal_y:
self.y = legal_y-(new_y-legal_y)
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
厉害(其实我想说::腻害) {:5_108:}
页:
[1]