鱼C论坛

 找回密码
 立即注册
查看: 916|回复: 2

75行,turtle_now_x=random.choice([now_x+1,now_x+2,now_x-1,now_x-2,now_x])报错

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

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

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

x
提示now_x未定义

  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):                          #定义鱼的初始情况,__init__返回值为空
  18.        fish_location_save=[]                #存放乌龟和鱼重合位置的类似于(1,2)形式的坐标
  19.        fish_x_location=[]                   #单独存放乌龟和鱼重合位置的X的坐标
  20.        fish_y_location=[]                   #单独存放乌龟和鱼重合位置的y的坐标
  21.        #一次性随即设定10个位置,即生成10条鱼
  22.        while len(fish_location_save)!=10 :
  23.            fish_now_x=random.randint(1,10)
  24.            fish_now_y=random.randint(1,10)
  25.            fish_location=(fish_now_x,fish_now_y)   #生成元祖,类似于(x,y)形式,然后存于self.fish_location_save列表
  26.            if fish_location not in fish_location_save:
  27.               fish_location_save.append(fish_location)
  28.               fish_x_location.append(fish_now_x)
  29.               fish_y_location.append(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_location_save):
  39.                #鱼的最大移动为1,在初始坐标的基础上,x,y+1,或者不动,所以有三种情况
  40.                new_fish_x=random.choice([fish_x_location[i],fish_x_location[i]+1,fish_x_location[i]-1])
  41.                new_fish_y=random.choice([fish_y_location[i],fish_y_location[i]+1,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.         turtle_begin=100
  66.         #随机设置初始位置
  67.         now_x=random.randint(1,10)
  68.         now_y=random.randint(1,10)
  69.         
  70.     def turtle_move(self):
  71.         turtle_now_x=random.choice([now_x+1,now_x+2,now_x-1,now_x-2,now_x])
  72.         turtle_now_y=random.choice([now_y+1,now_y+2,now_y-1,now_y-2,now_y])
  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.fish_move()
  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()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-9 16:11:27 | 显示全部楼层
你本来就没定义啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-9 16:56:56 | 显示全部楼层
BngThea 发表于 2020-4-9 16:11
你本来就没定义啊

这样呢,现在不报错了,跑不出结果了
  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):                          #定义鱼的初始情况,__init__返回值为空
  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.            fish_now_x=random.randint(1,10)
  24.            fish_now_y=random.randint(1,10)
  25.            fish_location=(fish_now_x,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(fish_now_x)
  29.               self.fish_y_location.append(fish_now_y)

  30.               
  31.     def fish_move(self):
  32.        #10条鱼,他们都可动可不动
  33.        self.new_fish_location_save=[]                #存放移动后新的乌龟和鱼重合位置的类似于(1,2)形式的坐标
  34.        self.new_fish_x_location=[]                   #单独移动后新的乌龟和鱼重合位置的X的坐标
  35.        self.new_fish_y_location=[]                   #单独移动后新的乌龟和鱼重合位置的y的坐标
  36.        #移动10条鱼的位置
  37.        while len(self.new_fish_location_save) != 10:
  38.                #鱼的最大移动为1,在初始坐标的基础上,x,y+1,或者不动,所以有三种情况
  39.             for i in range(0,10):
  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 self.new_fish_location_save:
  49.                            self.new_fish_location_save.append(me)
  50.                            self.new_fish_x_location.append(new_fish_x)
  51.                            self.new_fish_y_location.append(new_fish_y)

  52.        turtle=Turtle()
  53.        turtlr_location=turtle.turtle_move()
  54.        if turtlr_location in self.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,self.now_x])
  72.         turtle_now_y=random.choice([self.now_y+1,self.now_y+2,self.now_y-1,self.now_y-2,self.now_y])
  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.fish_move()
  82.         if turtlr_location in fish_location_save:
  83.             self.turtle_begin += 20
  84.             print("一条鱼已经被吃掉,还剩%d条鱼"%len(fish_location_save))
  85.             return  turtlr_location     #返回重合位置的坐标
  86.         else:
  87.             return (0,0)                #不重合的时候返回一个不在范围的坐标,避免鱼在盗用的时候报错                     
  88.         self.turtle_begin -= 5
  89.         if self.turtle_begin == 0:
  90.             g.msgbox("game over")
  91.             return self.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()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 07:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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