import random
def cjyd(temp): #判断是否超出边界,确定在<10,>0的范围内
if temp > 10:
temp = 20 - temp
return temp
elif temp < 0:
temp = 0 - temp
return temp
else:
return temp
class Wugui: #乌龟出生点初始化和移动上下左右的判断和输出
tili = 100
def chusheng(self):
self.wugui_x = random.randint(0, 10)
self.wugui_y = random.randint(0, 10)
print("乌龟出生在【%d】,【%d】" % (self.wugui_x, self.wugui_y))
def yidong(self,x,y):
self.tili -= 1
self.zhou = random.choice(["x","y"])
self.buchang = random.randint(1,2)
if self.zhou == "x":
self.heng = random.choice(["左","右"])
if self.heng == "左":
x = cjyd(x - self.buchang)
else:
x = cjyd(x + self.buchang)
else:
self.shu = random.choice(["上","下"])
if self.shu == "下":
y = cjyd(y - self.buchang)
else:
y = cjyd(y + self.buchang)
return x, y
class Yu: #鱼的出生点位和移动判断和输出
def chusheng(self):
self.yu_x = random.randint(0, 10)
self.yu_y = random.randint(0, 10)
def yidong(self,x,y):
self.zhou = random.choice(["x", "y"])
if self.zhou == "x":
self.heng = random.choice(["左","右"])
if self.heng == "左":
x = cjyd(x - 1)
else:
x = cjyd(x + 1)
else:
self.shu = random.choice(["上","下"])
if self.shu == "下":
y = cjyd(y - 1)
else:
y = cjyd(y + 1)
return x,y
#生成鱼和乌龟并放入字典
wugui = Wugui()
wugui.chusheng()
wuguiz = {}
wuguiz.setdefault("乌龟",(wugui.wugui_x,wugui.wugui_y))
yu = {}
yuzu = Yu()
for i in range(1,11):
yu.setdefault("鱼%d"%i)
for i in yu:
yuzu.chusheng()
yu[i] = (yuzu.yu_x,yuzu.yu_y)
print("%s出生在【%d】,【%d】" % (i, yu[i][0], yu[i][1]))
#判断移动,体力消耗,乌龟吃鱼
jishu = 0
while 1:
jishu += 1
if wugui.tili == 0 or len(yu) == 0 : #判断结束,鱼等于0或者乌龟体力为0
break
print("\n\n第【%d】次移动" % jishu)
input("按回车进入下一次移动")
wuguiz["乌龟"] = wugui.yidong(wuguiz["乌龟"][0],wuguiz["乌龟"][1]) #乌龟移动
if wugui.zhou == "x":
print("乌龟向%s移动了%d步,位置到达【%d】,【%d】,体力还剩【%d】点" % (wugui.heng, wugui.buchang,wuguiz["乌龟"][0],wuguiz["乌龟"][1], wugui.tili))
else:
print("乌龟向%s移动了%d步,位置到达【%d】,【%d】,体力还剩【%d】点" % (wugui.shu, wugui.buchang,wuguiz["乌龟"][0],wuguiz["乌龟"][1], wugui.tili))
for i in yu: #鱼移动
yu[i] = yuzu.yidong(yu[i][0],yu[i][1])
if yuzu.zhou == "x":
print("%s向%s移动了1步,位置到达【%d】,【%d】" % (i, yuzu.heng,yu[i][0],yu[i][1]))
else:
print("%s向%s移动了1步,位置到达【%d】,【%d】" % (i, yuzu.shu,yu[i][0],yu[i][1]))
for i in list(yu.keys()): #判断吃
if wuguiz["乌龟"] == yu[i]:
wugui.tili += 20
if wugui.tili > 100: #判断体力
wugui.tili = 100
print("%s被吃了,乌龟加20点体力,现有体力%d"%(i,wugui.tili))
del yu[i]
if wugui.tili == 0:
print("乌龟没有体力,游戏结束")
if len(yu) == 0:
print("鱼被乌龟吃完了,游戏结束")