|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import time
import random
#创建类
maxRange=10#正方形一边格子数
#乌龟类
class Tortoise:
name=""#名字
bloodValue=100#血量
movdistance=1#移动距离
direct=0#移动方向0向上,1向右,2向下,3向左
x=0#x坐标
y=0#y坐标
def __init__(self,name):
self.x=random.randint(0,maxRange-1)#随机生成初始坐标
self.y=random.randint(0,maxRange-1)
self.name=name
def run(self):
if self.bloodValue<=0:
return
self.direct=random.randint(0,3)#随机生产移动方向
self.movdistance=random.randint(1,2)#随机产生移动距离
if self.direct==0:
if self.y-self.movdistance<0:
self.direct=2#碰壁反弹,朝反方向走
elif self.direct==1:
if self.x+self.movdistance>maxRange-1:
self.direct=3#碰壁反弹,朝反方向走
elif self.direct==2:
if self.y+self.movdistance>maxRange-1:
self.direct=0#碰壁反弹,朝反方向走
elif self.direct==3:
if self.x-self.movdistance<0:
self.direct=1#碰壁反弹,朝反方向走
temp=random.randint(0,2)#随机决定要不要移动temp=0时不移动
if temp:
self.mov()#确定可以移动的方向后移动
def mov(self):#移动坐标
if self.bloodValue<=0:
return
if self.direct==0:
self.y-=self.movdistance
elif self.direct==1:
self.x+=self.movdistance
elif self.direct==2:
self.y+=self.movdistance
elif self.direct==3:
self.x-=self.movdistance
self.bloodValue-=1#移动后体力消耗1
def getxy(self):#获取乌龟坐标
return [self.x,self.y]
def getFish(self):#吃掉一条鱼
if self.bloodValue<=0:
return
self.bloodValue+=20
#鱼类
class Fish:
name=""#名字
bloodValue=100#血量
movdistance=1#移动距离
direct=0#移动方向0向上,1向右,2向下,3向左
x=0#x坐标
y=0#y坐标
def __init__(self,name):
self.x=random.randint(0,maxRange-1)#随机生成初始坐标
self.y=random.randint(0,maxRange-1)
self.name=name
def run(self):
if self.bloodValue<=0:
return
self.direct=random.randint(0,3)#随机生产移动方向
self.movdistance=random.randint(1,2)#随机产生移动距离
if self.direct==0:
if self.y-self.movdistance<0:
self.direct=2#碰壁反弹,朝反方向走
elif self.direct==1:
if self.x+self.movdistance>maxRange-1:
self.direct=3#碰壁反弹,朝反方向走
elif self.direct==2:
if self.y+self.movdistance>maxRange-1:
self.direct=0#碰壁反弹,朝反方向走
elif self.direct==3:
if self.x-self.movdistance<0:
self.direct=1#碰壁反弹,朝反方向走
temp=random.randint(0,2)#随机决定要不要移动temp=0时不移动
if temp:
self.mov()#确定可以移动的方向后移动
def mov(self):#移动坐标
if self.bloodValue<=0:
return
if self.direct==0:
self.y-=self.movdistance
elif self.direct==1:
self.x+=self.movdistance
elif self.direct==2:
self.y+=self.movdistance
elif self.direct==3:
self.x-=self.movdistance
def getxy(self):#获取鱼坐标
return [self.x,self.y]
def suicide(self):#鱼被吃
self.bloodValue=0
#打印每个坐标的乌龟和鱼
def printAllData(listAllData):
chessList=[]#棋盘数据
isAnmal=False
#print(listAllData)
for eachx in range(maxRange):
print("\n",end="")
for eachy in range(maxRange):
strTemp=""
isAnmal=False
for each in listAllData:
if [eachy,eachx]==each[1]:
strTemp+=each[0]+" "
isAnmal=True
if isAnmal==False:
strTemp+="[ ]\t"
else:
strTemp+="\t"
print(strTemp,end="")
print("\n")
#开始游戏
tor1=Tortoise("龟")#生成一只乌龟
fishList=[]
fishcount=10#生成鱼的数量
for i in range(fishcount):#生成fishcount个鱼
fishList.append(Fish(str(i+1)))
#游戏开跑
while True:
if tor1.bloodValue<=0 or len(fishList)<=0:#鱼吃完或者乌龟体力为0时结束游戏
break
tor1.run()#乌龟运动
for each in fishList:#鱼运动
each.run()
#判断坐标,吃鱼判断
for each in fishList:
if each.getxy()==tor1.getxy():
each.suicide()#鱼生命值为0
tor1.getFish()#乌龟生命值+20
#收集所有生物坐标
listAllData=[]
listAllData.append((tor1.name,tor1.getxy()))
for each in fishList:
listAllData.append((each.name,each.getxy()))
printAllData(listAllData)#打印每个坐标的乌龟和鱼
for each in fishList:#删除嗝屁的鱼
if each.bloodValue<=0:
fishList.remove(each)
print("乌龟生命值:",str(tor1.bloodValue),"坐标:",str(tor1.x),":",str(tor1.y))
print("还剩"+str(len(fishList))+"条鱼")
#for each in fishList:
#print(each.name+"号鱼生命值:",str(each.bloodValue),"坐标:",str(each.x),":",str(each.y))
print("\n")
time.sleep(2)#等待0.5秒
if tor1.bloodValue>0:
print("乌龟成功生存!")
else:
print("乌龟生存失败!")
name=input("游戏结束!") |
|