|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
先放我的代码:
import random
'''build a 10x10 site'''
site=[(x,y) for x in range(0,11) for y in range(0,11)]
'''global locations'''
fishlist=list(random.sample(site,10))
tor_loc=list(random.sample(site,1))[0]
tortose_health=100
Round=0
'''build the object'''
class fish:
def __init__(self,location,fish=True):
if fish:
self.movement=[(-1,0),(1,0),(0,-1),(0,1)]
else:
self.movement=[(-1,0),(1,0),(0,-1),(0,1),(-2,0),(2,0),(0,-2),(0,2)]
self.location=location
def update_location(self):
move=list(random.sample(self.movement,1))[0]
new_loc=[]
for i in range(0,2):
new=move[i]+self.location[i]
if new>10 or new<0:
new+= -2*move[i]
else:
pass
new_loc.append(new)
new_loc=tuple(new_loc)
return new_loc
'''health calculate'''
def health_cal(tor_loc,fishlist,tortose_health):
tortose_health=tortose_health-1
for thisfish in fishlist:
if thisfish==tor_loc:
fishlist.remove(thisfish)
tortose_health+=20
if tortose_health>100:
tortose_health=100
else:
pass
return tortose_health, fishlist
'''is alive?'''
def is_alive(fishlist,tortose_health):
if len(fishlist)<1 or tortose_health<1:
return False
else:
return True
'''update one time'''
while is_alive(fishlist,tortose_health):
for cache in fishlist:
thisfish=fish(cache)
new_loc=thisfish.update_location()
fishlist[fishlist.index(cache)]=new_loc
tortose=fish(tor_loc,fish=False)
tor_loc=tortose.update_location()
health_cal(tor_loc,fishlist,tortose_health)
Round+=1
print ('---------------Round%s----------------------'%(int(Round)))
print ('-----------Tortose\'s health%s--------------'%(int(tortose_health)))
print (fishlist)
print ('----------GAME OVER!---------------')
if len(fishlist)<1:
print ('-------------------TORTOSE WIN!-------------------')
print ('tortose health:',tortose_health)
else:
print ('-------------------FICHES WIN!--------------------')
我感觉我在调用healt_cal()这个功能的时候已经在第一行先减了1点体力值额,为什么我打印出来的结果体力值一直是100额。哪位大神救救本宝宝额
|
|