37讲 if turtlr_location in fish_location_save:报错(86行)
本帖最后由 猪猪虾 于 2020-4-9 14:32 编辑类里面的函数之间,变量的相互调用,是self.吗
#游戏编程:按以下要求定义一个乌龟类和鱼类并尝试编写游戏。
#假设游戏场景为范围(x, y)为0<=x<=10,0<=y<=10
#游戏生成1只乌龟和10条鱼
#它们的移动方向均随机
#乌龟的最大移动能力是2(Ta可以随机选择1还是2移动),鱼儿的最大移动能力是1
#当移动到场景边缘,自动向反方向移动
#乌龟初始化体力为100(上限)
#乌龟每移动一次,体力消耗5
#当乌龟和鱼坐标重叠,乌龟吃掉鱼,乌龟体力增加20
#鱼暂不计算体力
#当乌龟体力值为0(挂掉)或者鱼儿的数量为0游戏结束
import easygui as g
import random
location_x=
location_y=
class Fish:
def _init_(self): #定义鱼的初始情况
self.fish_location_save=[] #存放乌龟和鱼重合位置的类似于(1,2)形式的坐标
self.fish_x_location=[] #单独存放乌龟和鱼重合位置的X的坐标
self.fish_y_location=[] #单独存放乌龟和鱼重合位置的y的坐标
#一次性随即设定10个位置,即生成10条鱼
while len(self.fish_location_save)!=10 :
self.fish_now_x=random.randint(1,10)
self.fish_now_y=random.randint(1,10)
fish_location=(self.fish_now_x,self.fish_now_y) #生成元祖,类似于(x,y)形式,然后存于self.fish_location_save列表
if fish_location not in self.fish_location_save:
self.fish_location_save.append(fish_location)
self.fish_x_location.append(self.fish_now_x)
self.fish_y_location.append(self.fish_now_y)
def fish_move(self):
#10条鱼,他们都可动可不动
new_fish_location_save=[] #存放移动后新的乌龟和鱼重合位置的类似于(1,2)形式的坐标
new_fish_x_location=[] #单独移动后新的乌龟和鱼重合位置的X的坐标
new_fish_y_location=[] #单独移动后新的乌龟和鱼重合位置的y的坐标
#移动10条鱼的位置
while len(new_fish_location_save) != 10:
for i inlen(self.fish_x):
#鱼的最大移动为1,在初始坐标的基础上,x,y+1,或者不动,所以有三种情况
new_fish_x=random.choice(,self.fish_x_location+1,self.fish_x_location-1])
new_fish_y=random.choice(,self.fish_y_location+1,self.fish_y_location-1])
#判断是否超出边界
if new_fish_x not in location_x:
new_fish_x-=1
if new_fish_y not in location_y:
new_fish_x-=1
me=(new_fish_x,new_fish_y)
if me not in new_fish_location_save:
new_fish_location_save.append(me)
new_fish_x_location.append(new_fish_x)
new_fish_y_location.append(new_fish_y)
turtle=Turtle()
turtlr_location=turtle.turtle_move()
if turtlr_location in new_fish_location_save:
#乌龟和鱼的位置重合,删掉重合位置的鱼的坐标
for i in len(self.fish_location_save):
if fish_location == self.fish_location_save:
me=i
break
self.fish_location_save.pop(me)
class Turtle:
def _init_(self):#定义乌龟的初始情况
#初始体力
self.turtle_begin=100
#随机设置初始位置
self.now_x=random.randint(1,10)
self.now_y=random.randint(1,10)
def turtle_move(self):
turtle_now_x=random.choice()
turtle_now_y=random.choice()
#判断是否超出边界
if turtle_now_x not in location_x:
#反向返回
turtle_now_x-=1
if turtle_now_y not in location_y:
turtle_now_y-=1
turtlr_location=(turtle_now_x,turtle_now_y)
fish=Fish()
fish_location_save=fish._init_()
if turtlr_location in fish_location_save:
turtle_begin += 20
print("一条鱼已经被吃掉,还剩%d条鱼"%len(fish_location_save))
returnturtlr_location #返回重合位置的坐标
else:
return (0,0) #不重合的时候返回一个不在范围的坐标,避免鱼在盗用的时候报错
turtle_begin -= 5
if turtle_begin == 0:
g.msgbox("game over")
return turtle_begin
fish_use=Fish()
turtlr_use=Turtle()
fish_use._init_ ()
turtlr_use._init_()
turtlr_use.turtle_move()
fish_use.fish_move()
while turtle_begin !=0 or len(fish_location_save) != 0 :
turtlr_use.turtle_move()
fish_use.fish_move()
报什么错 魔法方法是双下划线,不是单个
报错信息是? qiuyouzhi 发表于 2020-4-9 14:31
魔法方法是双下划线,不是单个
报错信息是?
TypeError: argument of type 'NoneType' is not iterable 猪猪虾 发表于 2020-4-9 14:33
TypeError: argument of type 'NoneType' is not iterable
__init__方法只能返回None,你把那个fish_location_save赋值为Fish的__init__,当然会报错 qiuyouzhi 发表于 2020-4-9 14:39
__init__方法只能返回None,你把那个fish_location_save赋值为Fish的__init__,当然会报错
噢,原来是这样,类之间函数变量的调用,是self.吗,我用得对不对呢 猪猪虾 发表于 2020-4-9 14:46
噢,原来是这样,类之间函数变量的调用,是self.吗,我用得对不对呢
那样调用的是实例的变量,而且__init__是在实例化时就调用了,不需要手动调用。 qiuyouzhi 发表于 2020-4-9 14:49
那样调用的是实例的变量,而且__init__是在实例化时就调用了,不需要手动调用。
是这个意思吗:
class Fish:
me=0
def __ init__(self):
name=random(1,5)
def fish(self):
me+=name
可以这么直接用? 猪猪虾 发表于 2020-4-9 14:52
是这个意思吗:
class Fish:
me=0
是的,如果不加self的话 本帖最后由 猪猪虾 于 2020-4-9 15:06 编辑
qiuyouzhi 发表于 2020-4-9 14:57
是的,如果不加self的话
可是我把self全删掉之后,def turtle_move(self):turtle_now_x=random.choice(),显示now_x未定义(63行)
import easygui as g
import random
location_x=
location_y=
class Fish:
def __init__(self): #定义鱼的初始情况,__init__返回值为空
fish_location_save=[] #存放乌龟和鱼重合位置的类似于(1,2)形式的坐标
fish_x_location=[] #单独存放乌龟和鱼重合位置的X的坐标
fish_y_location=[] #单独存放乌龟和鱼重合位置的y的坐标
#一次性随即设定10个位置,即生成10条鱼
while len(fish_location_save)!=10 :
fish_now_x=random.randint(1,10)
fish_now_y=random.randint(1,10)
fish_location=(fish_now_x,fish_now_y) #生成元祖,类似于(x,y)形式,然后存于self.fish_location_save列表
if fish_location not in fish_location_save:
fish_location_save.append(fish_location)
fish_x_location.append(fish_now_x)
fish_y_location.append(fish_now_y)
def fish_move(self):
#10条鱼,他们都可动可不动
new_fish_location_save=[] #存放移动后新的乌龟和鱼重合位置的类似于(1,2)形式的坐标
new_fish_x_location=[] #单独移动后新的乌龟和鱼重合位置的X的坐标
new_fish_y_location=[] #单独移动后新的乌龟和鱼重合位置的y的坐标
#移动10条鱼的位置
while len(new_fish_location_save) != 10:
for i inlen(self.fish_location_save):
#鱼的最大移动为1,在初始坐标的基础上,x,y+1,或者不动,所以有三种情况
new_fish_x=random.choice(,fish_x_location+1,fish_x_location-1])
new_fish_y=random.choice(,fish_y_location+1,fish_y_location-1])
#判断是否超出边界
if new_fish_x not in location_x:
new_fish_x-=1
if new_fish_y not in location_y:
new_fish_x-=1
me=(new_fish_x,new_fish_y)
if me not in new_fish_location_save:
new_fish_location_save.append(me)
new_fish_x_location.append(new_fish_x)
new_fish_y_location.append(new_fish_y)
turtle=Turtle()
turtlr_location=turtle.turtle_move()
if turtlr_location in new_fish_location_save:
#乌龟和鱼的位置重合,删掉重合位置的鱼的坐标
for i in len(self.fish_location_save):
if fish_location == self.fish_location_save:
me=i
break
self.fish_location_save.pop(me)
class Turtle:
def __init__(self):#定义乌龟的初始情况
#初始体力
turtle_begin=100
#随机设置初始位置
now_x=random.randint(1,10)
now_y=random.randint(1,10)
def turtle_move(self):
turtle_now_x=random.choice()
turtle_now_y=random.choice()
#判断是否超出边界
if turtle_now_x not in location_x:
#反向返回
turtle_now_x-=1
if turtle_now_y not in location_y:
turtle_now_y-=1
turtlr_location=(turtle_now_x,turtle_now_y)
fish=Fish()
fish_location_save=fish.fish_move()
if turtlr_location in fish_location_save:
turtle_begin += 20
print("一条鱼已经被吃掉,还剩%d条鱼"%len(fish_location_save))
returnturtlr_location #返回重合位置的坐标
else:
return (0,0) #不重合的时候返回一个不在范围的坐标,避免鱼在盗用的时候报错
turtle_begin -= 5
if turtle_begin == 0:
g.msgbox("game over")
return turtle_begin
fish_use=Fish()
turtlr_use=Turtle()
fish_use.__init__ ()
turtlr_use.__init__()
turtlr_use.turtle_move()
fish_use.fish_move()
while turtle_begin !=0 or len(fish_location_save) != 0 :
turtlr_use.turtle_move()
fish_use.fish_move()
页:
[1]