马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 lxping 于 2022-11-21 23:04 编辑 from random import *
class Trutle:
def __init__(self,name):
self.name = name
self.power = 100
self.x = randint(0,10)
self.y = randint(0,10)
def position(self):
self.power -= 1
dire = choice(direction)
om = randint(1,2)
new_x = self.x + dire[0]*om
new_y = self.y + dire[1]*om
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
print(f"trutle:{self.x, self.y}")
return (self.x,self.y)
def eatfish(self):
self.power += 20
if self.power > 100:
self.power = 100
class Fish:
def __init__(self,name):
self.name = name
self.x = randint(0,10)
self.y = randint(0,10)
def position(self):
dire = choice(direction)
om = 1
new_x = self.x + dire[0]*om
new_y = self.y + dire[1]*om
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
return (self.x,self.y)
origin = (0, 0) #原点
legal_x = [0,10] #x轴的移动范围
legal_y = [0,10] #y轴的移动范围
direction = [[-1,0], [1,0], [0,1], [0,-1]]
trutle = Trutle("小甲鱼")
for i in range(1,11):
locals()["fish"+ str(i)] = Fish("fish"+str(i))
fish_list = [fish1, fish2, fish3, fish4, fish5, fish6, fish7, fish8, fish9, fish10]
trutle_pos = (trutle.x,trutle.y)
for each in fish_list:
if (each.x,each.y) == trutle_pos:
trutle.eatfish()
fish_list.remove(each)
print(f"还剩{len(fish_list)}条鱼")
while True:
if len(fish_list) and trutle.power:
trutle_pos = trutle.position()
for each in fish_list:
each_pos = each.position()
if each_pos == trutle_pos:
trutle.eatfish()
fish_list.remove(each)
print(f"还剩{len(fish_list)}条鱼")
elif not len(fish_list):
print("没鱼啦~")
break
elif not trutle.power:
print("游不动啦~")
break
print("游戏结束")
|