|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import random as r
class Turtle():
def __init__(self):
self.power=int(100)
self.x=r.randint(0,10)
self.y=r.randint(0,10)
self.old_x=self.x
self.old_y=self.y
def Powernew(self,fish=False):
if self.old_x!=self.x or self.old_y!=self.y:
self.power=self.power-1
else:
if fish==True and self.power<100:
self.power=self.power+20
if self.power>100:
self.power=100
def Tmove(self):
move_xstep=r.randint(-2,2)
move_ystep=r.randint(-2,2)
if self.power >0:-------------------------------------------------------------------------------------------------------------------------提示此处有错误
if (self.x+move_xstep)<0:
self.x=self.x-move_xstep
else:
if (self.x+move_xstep)>10:
self.x=self.x-move_xstep
else:
self.x=self.x+move_xstep
if (self.y+move_ystep)<0:
self.y=self.y-move_ystep
else:
if (self.y+move_ystep)>10:
self.y=self.y-move_ystep
else:
self.y=self.y+move_ystep
self.power=self.Powernew( )
self.old_x=self.x
self.old_y=self.y
else:
print("此乌龟的体力值为0,已死亡")
return False
def Getposition(self):
print("此乌龟现在的位置是:(%d ,%d) "%(self.x,self.y))
def Getpower(self):
print("此乌龟现在的体力值是:%d"%self.power)
class Fish():
def __init__(self):
self.x=r.randint(0,10)
self.y=r.randint(0,10)
def Fmove(self):
move_xstep=r.randint(-1,1)
move_ystep=r.randint(-1,1)
if (self.x+move_xstep)<0 or (self.x+move_xstep)>10:
self.x=self.x-move_xstep
else:
self.x=self.x+move_xstep
if (self.y+move_ystep)<0 or (self.y+move_ystep)>10:
self.y=self.y-move_ystep
else:
self.y=self.y+move_ystep
def Getposition(self):
print("此鱼现在的位置是:(%d ,%d) "%(self.x,self.y))
print("现在开始我们的小游戏load.......")
print("""游戏规则如下:\n•游戏生成1只乌龟和10条鱼
•它们的移动方向均随机
•乌龟的最大移动能力是2(Ta可以随机选择1还是2移动),鱼儿的最大移动能力是1
•当移动到场景边缘,自动向反方向移动
•乌龟初始化体力为100(上限)
•乌龟每移动一次,体力消耗1
•当乌龟和鱼坐标重叠,乌龟吃掉鱼,乌龟体力增加20
•鱼暂不计算体力
•当乌龟体力值为0(挂掉)或者鱼儿的数量为0游戏结束
""")
turtle=Turtle()
fish=[Fish(),Fish(),Fish(),Fish(),Fish(),Fish(),Fish(),Fish(),Fish(),Fish()]
tcount=1
fcount=10
while tcount and fcount:
for i in range(10):
fish[i].Fmove()
order=input("是否选择乌龟移动去抓小鱼yes/no:")
if order=="yes":
turtle.Tmove()
if turtle.power==0:
tcount-=1
break
for i in range(10):
if fish[i].x==turtle.x and fish[i].y==turtle.y:
fcount-=1
turtle.Powernew(fish=True)
if tcount==0:
print("乌龟死了,游戏结束")
else:
print("鱼都被乌龟吃完了,游戏结束")
下面是运行错误提示:
RESTART: C:\Users\Administrator\AppData\Local\Programs\Python\Python37\game1.py
现在开始我们的小游戏load.......
游戏规则如下:
•游戏生成1只乌龟和10条鱼
•它们的移动方向均随机
•乌龟的最大移动能力是2(Ta可以随机选择1还是2移动),鱼儿的最大移动能力是1
•当移动到场景边缘,自动向反方向移动
•乌龟初始化体力为100(上限)
•乌龟每移动一次,体力消耗1
•当乌龟和鱼坐标重叠,乌龟吃掉鱼,乌龟体力增加20
•鱼暂不计算体力
•当乌龟体力值为0(挂掉)或者鱼儿的数量为0游戏结束
是否选择乌龟移动去抓小鱼yes/no:yes
是否选择乌龟移动去抓小鱼yes/no:yes
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\game1.py", line 97, in <module>
turtle.Tmove()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\game1.py", line 23, in Tmove
if self.power >0:
TypeError: '>' not supported between instances of 'NoneType' and 'int'
>>> sff
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
sff
NameError: name 'sff' is not defined
本帖最后由 hrp 于 2019-12-19 18:20 编辑
Powernew没有返回值,导致self.power = self.Powernew()为空
|
|