鱼C论坛

 找回密码
 立即注册
查看: 1362|回复: 10

[已解决]乌龟一直被饿死???

[复制链接]
发表于 2020-7-24 11:57:26 | 显示全部楼层 |阅读模式

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

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

x
为什么乌龟一直被饿死= = !
O8TS6[PTA{QWR$C8T_1}W7G.png
  1. import random

  2. class Tortois:
  3.     x = random.randint(0,10)
  4.     y = random.randint(0,10)
  5.     position = (x,y)
  6.     mobility = [1,2]
  7.     physical_strength = 100
  8.     def move(self):
  9.         distance = random.randint(1,2)
  10.         direction = random.randint(1,4)
  11.         if direction == 1:
  12.             if self.x - distance >= 0:
  13.                 self.position = (self.x-distance,self.y)
  14.             else:
  15.                 self.position = (self.x+distance,self.y)
  16.         elif direction == 2:
  17.             if self.x + distance <= 10:
  18.                 self.position = (self.x+distance,self.y)
  19.             else:
  20.                 self.position = (self.x-distance,self.y)
  21.         elif direction == 3:
  22.             if self.y - distance >= 0:
  23.                 self.position = (self.x,self.y-distance)
  24.             else:
  25.                 self.position = (self.x,self.y+distance)            
  26.         else:
  27.             if self.y + distance >= 10:
  28.                 self.position = (self.x,self.y+distance)
  29.             else:
  30.                 self.position = (self.x,self.y-distance)
  31.         self.physical_strength -= 1
  32.     def eat(self):
  33.         self.physical_strength += 20

  34. class Fish:
  35.     x = random.randint(0,10)
  36.     y = random.randint(0,10)
  37.     position = (x,y)
  38.     mobility = 1
  39.     def move(self):
  40.         direction = random.randint(1,4)
  41.         if direction == 1:
  42.             if self.x - self.mobility >= 0:
  43.                 self.position = (self.x-self.mobility,self.y)
  44.             else:
  45.                 self.position = (self.x+self.mobility,self.y)
  46.         elif direction == 2:
  47.             if self.x + self.mobility <= 10:
  48.                 self.position = (self.x+self.mobility,self.y)
  49.             else:
  50.                 self.position = (self.x-self.mobility,self.y)
  51.         elif direction == 3:
  52.             if self.y - self.mobility >= 0:
  53.                 self.position = (self.x,self.y-self.mobility)
  54.             else:
  55.                 self.position = (self.x,self.y+self.mobility)            
  56.         else:
  57.             if self.y + self.mobility >= 10:
  58.                 self.position = (self.x,self.y+self.mobility)
  59.             else:
  60.                 self.position = (self.x,self.y-self.mobility)

  61. t = Tortois()
  62. f1 = Fish()
  63. f2 = Fish()
  64. f3 = Fish()
  65. f4 = Fish()
  66. f5 = Fish()
  67. f6 = Fish()
  68. f7 = Fish()
  69. f8 = Fish()
  70. f9 = Fish()
  71. f10 = Fish()
  72. list_fish = [f1,f2,f3,f4,f5,f6,f7,f8,f9,f10]
  73. game = 1
  74. while game:
  75.     if t.physical_strength != 0 and list_fish != []:
  76.         for i in list_fish:
  77.             if t.position == i.position:
  78.                 list_fish.romove(i)
  79.                 t.physical_strength += 20
  80.         t.move()
  81.         for i in list_fish:
  82.             i.move()
  83.     elif t.physical_strength == 0:
  84.         print('乌龟被饿死了。。。')
  85.         game -= 1
  86.     else:
  87.         print('鱼被吃光了。。。')
  88.         game -= 1
  89.    
复制代码
最佳答案
2020-7-24 11:58:25
本帖最后由 Twilight6 于 2020-7-24 12:08 编辑

[b]哈哈哈,本来赢的概率就比较低的,代码如果没问题的话

而且你的 remove 拼成了 romove

你代码这里,Fish 类不能用类属性,类属性是所有 Fish 类的实例对象共享的

也就是你移动一个,全部都会改变,要用实例对象,而乌龟只有一只,所以用类和 实例差别不大,这里我帮你改了下代码:

  1. import random


  2. class Tortois:
  3.     def __init__(self):
  4.         self.x = random.randint(0, 10)
  5.         self.y = random.randint(0, 10)
  6.         self.position = (self.x, self.y)
  7.         self.mobility = [1, 2]
  8.         self.physical_strength = 1000000

  9.     def move(self):
  10.         distance = random.randint(1, 2)
  11.         direction = random.randint(1, 4)
  12.         if direction == 1:
  13.             if self.x - distance >= 0:
  14.                 self.position = (self.x - distance, self.y)
  15.             else:
  16.                 self.position = (self.x + distance, self.y)
  17.         elif direction == 2:
  18.             if self.x + distance <= 10:
  19.                 self.position = (self.x + distance, self.y)
  20.             else:
  21.                 self.position = (self.x - distance, self.y)
  22.         elif direction == 3:
  23.             if self.y - distance >= 0:
  24.                 self.position = (self.x, self.y - distance)
  25.             else:
  26.                 self.position = (self.x, self.y + distance)
  27.         else:
  28.             if self.y + distance >= 10:
  29.                 self.position = (self.x, self.y + distance)
  30.             else:
  31.                 self.position = (self.x, self.y - distance)
  32.         self.physical_strength -= 1

  33.     def eat(self):
  34.         self.physical_strength += 20


  35. class Fish:
  36.     def __init__(self):
  37.         self.x = random.randint(0, 10)
  38.         self.y = random.randint(0, 10)
  39.         self.position = (self.x, self.y)
  40.         self.mobility = 1

  41.     def move(self):
  42.         direction = random.randint(1, 4)
  43.         if direction == 1:
  44.             if self.x - self.mobility >= 0:
  45.                 self.position = (self.x - self.mobility, self.y)
  46.             else:
  47.                 self.position = (self.x + self.mobility, self.y)
  48.         elif direction == 2:
  49.             if self.x + self.mobility <= 10:
  50.                 self.position = (self.x + self.mobility, self.y)
  51.             else:
  52.                 self.position = (self.x - self.mobility, self.y)
  53.         elif direction == 3:
  54.             if self.y - self.mobility >= 0:
  55.                 self.position = (self.x, self.y - self.mobility)
  56.             else:
  57.                 self.position = (self.x, self.y + self.mobility)
  58.         else:
  59.             if self.y + self.mobility >= 10:
  60.                 self.position = (self.x, self.y + self.mobility)
  61.             else:
  62.                 self.position = (self.x, self.y - self.mobility)


  63. t = Tortois()
  64. f1 = Fish()
  65. f2 = Fish()
  66. f3 = Fish()
  67. f4 = Fish()
  68. f5 = Fish()
  69. f6 = Fish()
  70. f7 = Fish()
  71. f8 = Fish()
  72. f9 = Fish()
  73. f10 = Fish()
  74. list_fish = [f1, f2, f3, f4, f5, f6, f7, f8, f9, f10]
  75. game = 1
  76. while game:
  77.     if t.physical_strength != 0 and list_fish != []:
  78.         for i in list_fish:
  79.             if t.position == i.position:
  80.                 list_fish.remove(i)
  81.                 t.physical_strength += 20
  82.         t.move()
  83.         for i in list_fish:
  84.             i.move()
  85.     elif t.physical_strength == 0:
  86.         print('乌龟被饿死了。。。')
  87.         game -= 1
  88.     else:
  89.         print('鱼被吃光了。。。')
  90.         game -= 1
复制代码

[/b]
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-7-24 11:58:25 | 显示全部楼层    本楼为最佳答案   
本帖最后由 Twilight6 于 2020-7-24 12:08 编辑

[b]哈哈哈,本来赢的概率就比较低的,代码如果没问题的话

而且你的 remove 拼成了 romove

你代码这里,Fish 类不能用类属性,类属性是所有 Fish 类的实例对象共享的

也就是你移动一个,全部都会改变,要用实例对象,而乌龟只有一只,所以用类和 实例差别不大,这里我帮你改了下代码:

  1. import random


  2. class Tortois:
  3.     def __init__(self):
  4.         self.x = random.randint(0, 10)
  5.         self.y = random.randint(0, 10)
  6.         self.position = (self.x, self.y)
  7.         self.mobility = [1, 2]
  8.         self.physical_strength = 1000000

  9.     def move(self):
  10.         distance = random.randint(1, 2)
  11.         direction = random.randint(1, 4)
  12.         if direction == 1:
  13.             if self.x - distance >= 0:
  14.                 self.position = (self.x - distance, self.y)
  15.             else:
  16.                 self.position = (self.x + distance, self.y)
  17.         elif direction == 2:
  18.             if self.x + distance <= 10:
  19.                 self.position = (self.x + distance, self.y)
  20.             else:
  21.                 self.position = (self.x - distance, self.y)
  22.         elif direction == 3:
  23.             if self.y - distance >= 0:
  24.                 self.position = (self.x, self.y - distance)
  25.             else:
  26.                 self.position = (self.x, self.y + distance)
  27.         else:
  28.             if self.y + distance >= 10:
  29.                 self.position = (self.x, self.y + distance)
  30.             else:
  31.                 self.position = (self.x, self.y - distance)
  32.         self.physical_strength -= 1

  33.     def eat(self):
  34.         self.physical_strength += 20


  35. class Fish:
  36.     def __init__(self):
  37.         self.x = random.randint(0, 10)
  38.         self.y = random.randint(0, 10)
  39.         self.position = (self.x, self.y)
  40.         self.mobility = 1

  41.     def move(self):
  42.         direction = random.randint(1, 4)
  43.         if direction == 1:
  44.             if self.x - self.mobility >= 0:
  45.                 self.position = (self.x - self.mobility, self.y)
  46.             else:
  47.                 self.position = (self.x + self.mobility, self.y)
  48.         elif direction == 2:
  49.             if self.x + self.mobility <= 10:
  50.                 self.position = (self.x + self.mobility, self.y)
  51.             else:
  52.                 self.position = (self.x - self.mobility, self.y)
  53.         elif direction == 3:
  54.             if self.y - self.mobility >= 0:
  55.                 self.position = (self.x, self.y - self.mobility)
  56.             else:
  57.                 self.position = (self.x, self.y + self.mobility)
  58.         else:
  59.             if self.y + self.mobility >= 10:
  60.                 self.position = (self.x, self.y + self.mobility)
  61.             else:
  62.                 self.position = (self.x, self.y - self.mobility)


  63. t = Tortois()
  64. f1 = Fish()
  65. f2 = Fish()
  66. f3 = Fish()
  67. f4 = Fish()
  68. f5 = Fish()
  69. f6 = Fish()
  70. f7 = Fish()
  71. f8 = Fish()
  72. f9 = Fish()
  73. f10 = Fish()
  74. list_fish = [f1, f2, f3, f4, f5, f6, f7, f8, f9, f10]
  75. game = 1
  76. while game:
  77.     if t.physical_strength != 0 and list_fish != []:
  78.         for i in list_fish:
  79.             if t.position == i.position:
  80.                 list_fish.remove(i)
  81.                 t.physical_strength += 20
  82.         t.move()
  83.         for i in list_fish:
  84.             i.move()
  85.     elif t.physical_strength == 0:
  86.         print('乌龟被饿死了。。。')
  87.         game -= 1
  88.     else:
  89.         print('鱼被吃光了。。。')
  90.         game -= 1
复制代码

[/b]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-24 12:04:15 | 显示全部楼层
Twilight6 发表于 2020-7-24 11:58
哈哈哈,本来赢的概率就比较低的,代码如果没问题的话

而且你的 remove 拼成了 romove

额。。。。那它为什么不报错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-24 12:06:23 | 显示全部楼层
def花 发表于 2020-7-24 12:04
额。。。。那它为什么不报错


你没遇到过而已,我刚刚运行碰搭配一次报错才发现,你看下楼上

你代码还有一些其他问题,细节方面我没看,不知道代码还有没有问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-24 12:13:52 | 显示全部楼层
小污龟的无线死亡
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-24 12:17:15 | 显示全部楼层
本帖最后由 sunrise085 于 2020-7-24 12:22 编辑

二楼说的很对,你把乌龟体力值调高一些,吃一条鱼增加体力也调高一些,就能看到吃鱼了。
另外,吃鱼之后没有任何提示,所以你看不到吃鱼。
我在89行remove鱼后面加了一句
  1. print("乌龟吃掉了一条鱼!")
复制代码

我在你的程序基础上把体力值调整到1000,最多也就吃两条鱼,大部分时候吃不到鱼
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-24 12:21:28 | 显示全部楼层
Twilight6 发表于 2020-7-24 11:58
哈哈哈,本来赢的概率就比较低的,代码如果没问题的话

而且你的 remove 拼成了 romove

移动一个全部都会变,就是第一个鱼移动了,改变了类属性,然后第二个鱼初始的类属性也变了,是这个意思吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-24 12:24:07 | 显示全部楼层
def花 发表于 2020-7-24 12:21
移动一个全部都会变,就是第一个鱼移动了,改变了类属性,然后第二个鱼初始的类属性也变了,是这个意思吗 ...


对的,而且不止第二个,只要是这个类的实例,都会改变,而且我刚刚打印了下鱼的坐标发现坐标不是 1~10,而是 -1 ~11
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-24 13:40:42 | 显示全部楼层
乌龟真惨,一直被饿死。

点评

我很赞同!: 5.0
我很赞同!: 5
哈哈哈  发表于 2020-7-24 14:45
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-24 14:46:09 | 显示全部楼层
虐待动物,举报了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-24 05:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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