鱼C论坛

 找回密码
 立即注册
查看: 779|回复: 9

[已解决]37讲 if turtlr_location in fish_location_save:报错(86行)

[复制链接]
发表于 2020-4-9 14:28:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 猪猪虾 于 2020-4-9 14:32 编辑

类里面的函数之间,变量的相互调用,是self.吗

  1. #游戏编程:按以下要求定义一个乌龟类和鱼类并尝试编写游戏。
  2.     #假设游戏场景为范围(x, y)为0<=x<=10,0<=y<=10
  3.     #游戏生成1只乌龟和10条鱼
  4.     #它们的移动方向均随机
  5.     #乌龟的最大移动能力是2(Ta可以随机选择1还是2移动),鱼儿的最大移动能力是1
  6.     #当移动到场景边缘,自动向反方向移动
  7.     #乌龟初始化体力为100(上限)
  8.     #乌龟每移动一次,体力消耗5
  9.     #当乌龟和鱼坐标重叠,乌龟吃掉鱼,乌龟体力增加20
  10.     #鱼暂不计算体力
  11.     #当乌龟体力值为0(挂掉)或者鱼儿的数量为0游戏结束
  12. import easygui as g
  13. import random
  14. location_x=[1,10]
  15. location_y=[1,10]

  16. class Fish:
  17.     def _init_(self):                            #定义鱼的初始情况
  18.        self.fish_location_save=[]                #存放乌龟和鱼重合位置的类似于(1,2)形式的坐标
  19.        self.fish_x_location=[]                   #单独存放乌龟和鱼重合位置的X的坐标
  20.        self.fish_y_location=[]                   #单独存放乌龟和鱼重合位置的y的坐标
  21.        #一次性随即设定10个位置,即生成10条鱼
  22.        while len(self.fish_location_save)!=10 :
  23.            self.fish_now_x=random.randint(1,10)
  24.            self.fish_now_y=random.randint(1,10)
  25.            fish_location=(self.fish_now_x,self.fish_now_y)   #生成元祖,类似于(x,y)形式,然后存于self.fish_location_save列表
  26.            if fish_location not in self.fish_location_save:
  27.               self.fish_location_save.append(fish_location)
  28.               self.fish_x_location.append(self.fish_now_x)
  29.               self.fish_y_location.append(self.fish_now_y)

  30.               
  31.     def fish_move(self):
  32.        #10条鱼,他们都可动可不动
  33.        new_fish_location_save=[]                #存放移动后新的乌龟和鱼重合位置的类似于(1,2)形式的坐标
  34.        new_fish_x_location=[]                   #单独移动后新的乌龟和鱼重合位置的X的坐标
  35.        new_fish_y_location=[]                   #单独移动后新的乌龟和鱼重合位置的y的坐标
  36.        #移动10条鱼的位置
  37.        while len(new_fish_location_save) != 10:
  38.            for i in  len(self.fish_x):
  39.                #鱼的最大移动为1,在初始坐标的基础上,x,y+1,或者不动,所以有三种情况
  40.                new_fish_x=random.choice([self.fish_x_location[i],self.fish_x_location[i]+1,self.fish_x_location[i]-1])
  41.                new_fish_y=random.choice([self.fish_y_location[i],self.fish_y_location[i]+1,self.fish_y_location[i]-1])
  42.                #判断是否超出边界
  43.                if new_fish_x not in location_x:
  44.                    new_fish_x-=1
  45.                if new_fish_y not in location_y:
  46.                    new_fish_x-=1
  47.                me=(new_fish_x,new_fish_y)
  48.                if me not in new_fish_location_save:
  49.                    new_fish_location_save.append(me)
  50.                    new_fish_x_location.append(new_fish_x)
  51.                    new_fish_y_location.append(new_fish_y)

  52.        turtle=Turtle()
  53.        turtlr_location=turtle.turtle_move()
  54.        if turtlr_location in new_fish_location_save:
  55.             #乌龟和鱼的位置重合,删掉重合位置的鱼的坐标
  56.             for i in len(self.fish_location_save):
  57.                 if fish_location == self.fish_location_save[i]:
  58.                     me=i
  59.                     break
  60.             self.fish_location_save.pop(me)
  61.            
  62. class Turtle:
  63.     def _init_(self):  #定义乌龟的初始情况
  64.         #初始体力
  65.         self.turtle_begin=100
  66.         #随机设置初始位置
  67.         self.now_x=random.randint(1,10)
  68.         self.now_y=random.randint(1,10)
  69.         
  70.     def turtle_move(self):
  71.         turtle_now_x=random.choice([self.now_x+1,self.now_x+2,self.now_x-1,self.now_x-2])
  72.         turtle_now_y=random.choice([self.now_y+1,self.now_y+2,self.now_y-1,self.now_y-2])
  73.         #判断是否超出边界
  74.         if turtle_now_x not in location_x:
  75.           #反向返回
  76.           turtle_now_x-=1
  77.         if turtle_now_y not in location_y:
  78.           turtle_now_y-=1                                       
  79.         turtlr_location=(turtle_now_x,turtle_now_y)
  80.         fish=Fish()
  81.         fish_location_save=fish._init_()
  82.         if turtlr_location in fish_location_save:
  83.             turtle_begin += 20
  84.             print("一条鱼已经被吃掉,还剩%d条鱼"%len(fish_location_save))
  85.             return  turtlr_location     #返回重合位置的坐标
  86.         else:
  87.             return (0,0)                #不重合的时候返回一个不在范围的坐标,避免鱼在盗用的时候报错                     
  88.         turtle_begin -= 5
  89.         if turtle_begin == 0:
  90.             g.msgbox("game over")
  91.             return turtle_begin
  92.                                                 

  93. fish_use=Fish()
  94. turtlr_use=Turtle()
  95. fish_use._init_ ()
  96. turtlr_use._init_()
  97. turtlr_use.turtle_move()
  98. fish_use.fish_move()

  99. while turtle_begin !=0 or len(fish_location_save) != 0 :
  100.      turtlr_use.turtle_move()
  101.      fish_use.fish_move()

复制代码
最佳答案
2020-4-9 14:39:42
猪猪虾 发表于 2020-4-9 14:33
TypeError: argument of type 'NoneType' is not iterable

__init__方法只能返回None,你把那个fish_location_save赋值为Fish的__init__,当然会报错
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-9 14:31:26 | 显示全部楼层
报什么错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-9 14:31:34 | 显示全部楼层
魔法方法是双下划线,不是单个
报错信息是?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-9 14:33:14 | 显示全部楼层
qiuyouzhi 发表于 2020-4-9 14:31
魔法方法是双下划线,不是单个
报错信息是?

TypeError: argument of type 'NoneType' is not iterable
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-9 14:39:42 | 显示全部楼层    本楼为最佳答案   
猪猪虾 发表于 2020-4-9 14:33
TypeError: argument of type 'NoneType' is not iterable

__init__方法只能返回None,你把那个fish_location_save赋值为Fish的__init__,当然会报错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-9 14:46:27 | 显示全部楼层
qiuyouzhi 发表于 2020-4-9 14:39
__init__方法只能返回None,你把那个fish_location_save赋值为Fish的__init__,当然会报错

噢,原来是这样,类之间函数变量的调用,是self.吗,我用得对不对呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-9 14:49:06 | 显示全部楼层
猪猪虾 发表于 2020-4-9 14:46
噢,原来是这样,类之间函数变量的调用,是self.吗,我用得对不对呢

那样调用的是实例的变量,而且__init__是在实例化时就调用了,不需要手动调用。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-9 14:52:21 | 显示全部楼层
qiuyouzhi 发表于 2020-4-9 14:49
那样调用的是实例的变量,而且__init__是在实例化时就调用了,不需要手动调用。

是这个意思吗:
class Fish:
    me=0
    def __ init__(self):
          name=random(1,5)
    def fish(self):
         me+=name
可以这么直接用?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-9 14:57:33 | 显示全部楼层
猪猪虾 发表于 2020-4-9 14:52
是这个意思吗:
class Fish:
    me=0

是的,如果不加self的话
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-9 15:04:47 | 显示全部楼层
本帖最后由 猪猪虾 于 2020-4-9 15:06 编辑
qiuyouzhi 发表于 2020-4-9 14:57
是的,如果不加self的话


可是我把self全删掉之后,def turtle_move(self):turtle_now_x=random.choice([now_x+1,now_x+2,now_x-1,now_x-2,now_x]),显示now_x未定义(63行)
  1. import easygui as g
  2. import random
  3. location_x=[1,10]
  4. location_y=[1,10]

  5. class Fish:
  6.     def __init__(self):                          #定义鱼的初始情况,__init__返回值为空
  7.        fish_location_save=[]                #存放乌龟和鱼重合位置的类似于(1,2)形式的坐标
  8.        fish_x_location=[]                   #单独存放乌龟和鱼重合位置的X的坐标
  9.        fish_y_location=[]                   #单独存放乌龟和鱼重合位置的y的坐标
  10.        #一次性随即设定10个位置,即生成10条鱼
  11.        while len(fish_location_save)!=10 :
  12.            fish_now_x=random.randint(1,10)
  13.            fish_now_y=random.randint(1,10)
  14.            fish_location=(fish_now_x,fish_now_y)   #生成元祖,类似于(x,y)形式,然后存于self.fish_location_save列表
  15.            if fish_location not in fish_location_save:
  16.               fish_location_save.append(fish_location)
  17.               fish_x_location.append(fish_now_x)
  18.               fish_y_location.append(fish_now_y)

  19.               
  20.     def fish_move(self):
  21.        #10条鱼,他们都可动可不动
  22.        new_fish_location_save=[]                #存放移动后新的乌龟和鱼重合位置的类似于(1,2)形式的坐标
  23.        new_fish_x_location=[]                   #单独移动后新的乌龟和鱼重合位置的X的坐标
  24.        new_fish_y_location=[]                   #单独移动后新的乌龟和鱼重合位置的y的坐标
  25.        #移动10条鱼的位置
  26.        while len(new_fish_location_save) != 10:
  27.            for i in  len(self.fish_location_save):
  28.                #鱼的最大移动为1,在初始坐标的基础上,x,y+1,或者不动,所以有三种情况
  29.                new_fish_x=random.choice([fish_x_location[i],fish_x_location[i]+1,fish_x_location[i]-1])
  30.                new_fish_y=random.choice([fish_y_location[i],fish_y_location[i]+1,fish_y_location[i]-1])
  31.                #判断是否超出边界
  32.                if new_fish_x not in location_x:
  33.                    new_fish_x-=1
  34.                if new_fish_y not in location_y:
  35.                    new_fish_x-=1
  36.                me=(new_fish_x,new_fish_y)
  37.                if me not in new_fish_location_save:
  38.                    new_fish_location_save.append(me)
  39.                    new_fish_x_location.append(new_fish_x)
  40.                    new_fish_y_location.append(new_fish_y)

  41.        turtle=Turtle()
  42.        turtlr_location=turtle.turtle_move()
  43.        if turtlr_location in new_fish_location_save:
  44.             #乌龟和鱼的位置重合,删掉重合位置的鱼的坐标
  45.             for i in len(self.fish_location_save):
  46.                 if fish_location == self.fish_location_save[i]:
  47.                     me=i
  48.                     break
  49.             self.fish_location_save.pop(me)
  50.            
  51. class Turtle:
  52.     def __init__(self):  #定义乌龟的初始情况
  53.         #初始体力
  54.         turtle_begin=100
  55.         #随机设置初始位置
  56.         now_x=random.randint(1,10)
  57.         now_y=random.randint(1,10)
  58.         
  59.     def turtle_move(self):
  60.         turtle_now_x=random.choice([now_x+1,now_x+2,now_x-1,now_x-2,now_x])
  61.         turtle_now_y=random.choice([now_y+1,now_y+2,now_y-1,now_y-2,now_y])
  62.         #判断是否超出边界
  63.         if turtle_now_x not in location_x:
  64.           #反向返回
  65.           turtle_now_x-=1
  66.         if turtle_now_y not in location_y:
  67.           turtle_now_y-=1                                       
  68.         turtlr_location=(turtle_now_x,turtle_now_y)
  69.         fish=Fish()
  70.         fish_location_save=fish.fish_move()
  71.         if turtlr_location in fish_location_save:
  72.             turtle_begin += 20
  73.             print("一条鱼已经被吃掉,还剩%d条鱼"%len(fish_location_save))
  74.             return  turtlr_location     #返回重合位置的坐标
  75.         else:
  76.             return (0,0)                #不重合的时候返回一个不在范围的坐标,避免鱼在盗用的时候报错                     
  77.         turtle_begin -= 5
  78.         if turtle_begin == 0:
  79.             g.msgbox("game over")
  80.             return turtle_begin
  81.                                                 

  82. fish_use=Fish()
  83. turtlr_use=Turtle()
  84. fish_use.__init__ ()
  85. turtlr_use.__init__()
  86. turtlr_use.turtle_move()
  87. fish_use.fish_move()

  88. while turtle_begin !=0 or len(fish_location_save) != 0 :
  89.      turtlr_use.turtle_move()
  90.      fish_use.fish_move()

  91.                                                 
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 08:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表