|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
没什么别的意思,就想看乌龟赢一次,写了如下代码import random as r
legal_x=[0,10]
legal_y=[0,10]
class Turtle :
def __init__(self):
self.power=100
self.x=r.randint(0,10)
self.y=r.randint(0,10)
def move(self):
new_x=self.x+r.choice([1,2,-1,-2])
new_y=self.y+r.choice([1,2,-1,-2])
if new_x>=legal_x[1]:
new_x=10-(new_x-10)
if new_x<=legal_x[0]:
new_x=0-new_x
self.x=new_x
self.y=new_y
self.power-=1
return(self.x,self.y)
def eat(self):
self.power+=20
class Fish():
def __init__(self):
self.hp=1
self.x=r.randint(0,10)
self.y=r.randint(0,10)
def move(self):
if self.hp==0:
self.x=-1
self.y=-1
return(self.x,self.y)
new_x=self.x+r.choice([1,-1])
new_y=self.y+r.choice([1,-1])
if new_x>=legal_x[1]:
new_x=10-(new_x-10)
if new_x<=legal_x[0]:
new_x=0-new_x
self.x=new_x
self.y=new_y
return(self.x,self.y)
def beeat(self):
self.hp-=1
turtle=Turtle()
fish_list=[]
for i in range(0,10):
new_fish=Fish()
fish_list.append(new_fish)
fish_eat=0
while 1:
if turtle.power==0:
print(fish_eat)
print('turtle die')
break
if len(fish_list)==0:
print('turtle became more powerful')
break
turtle.move()
for v in range(0,len(fish_list)):
fish_list[v].move()
if fish_list[v].x==turtle.x and fish_list[v].y ==turtle.y:
fish_list[v].beeat()
turtle.eat()
print('turtle eat')
for i in range(0,len(fish_list)):
if fish_list[i].x==-1 and fish_list[i].y==-1:
fish_eat+=1
if fish_eat==len(fish_list):
print(fish_eat)
print('turtle became more powerful')
break
我个人认为问题出现在fish_eat上,或者前面的fish.move中的if hp==0里
这个乌龟吃鱼游戏本身对乌龟就不友好哈,你将乌龟生命值调高些即可:
部分修改,参考代码:
import random as r
legal_x=[0,10]
legal_y=[0,10]
class Turtle :
def __init__(self):
self.power= 1000
self.x=r.randint(0,10)
self.y=r.randint(0,10)
def move(self):
new_x=self.x+r.choice([1,2,-1,-2])
new_y=self.y+r.choice([1,2,-1,-2])
if new_x>=legal_x[1]:
new_x=10-(new_x-10)
if new_x<=legal_x[0]:
new_x=0-new_x
self.x=new_x
self.y=new_y
self.power-=1
return(self.x,self.y)
def eat(self):
self.power+=1000
class Fish():
def __init__(self):
self.hp=1
self.x=r.randint(0,10)
self.y=r.randint(0,10)
def move(self):
if self.hp==0:
self.x=-1
self.y=-1
return(self.x,self.y)
new_x=self.x+r.choice([1,-1])
new_y=self.y+r.choice([1,-1])
if new_x>=legal_x[1]:
new_x=10-(new_x-10)
if new_x<=legal_x[0]:
new_x=0-new_x
self.x=new_x
self.y=new_y
return(self.x,self.y)
def beeat(self):
self.hp-=1
turtle=Turtle()
fish_list=[]
for i in range(0,10):
new_fish=Fish()
fish_list.append(new_fish)
fish_eat=0
while 1:
if turtle.power==0:
print('The turtle ate %d fish.'%fish_eat)
print('turtle die')
break
if len(fish_list)==0:
print('turtle became more powerful')
break
turtle.move()
for v in range(0,len(fish_list)):
fish_list[v].move()
if fish_list[v].x==turtle.x and fish_list[v].y ==turtle.y:
fish_list[v].beeat()
turtle.eat()
print('turtle ate a fish~')
fish_eat+=1
if fish_eat == len(fish_list):
print('The turtle ate up all the fish.')
print('turtle victory!')
break
|
|