python 如何修复在使用类和turtle时出现的“AttributeError”?

yebdmbv4  于 2024-01-05  发布在  Python
关注(0)|答案(1)|浏览(209)

我正在尝试使用我相当熟悉的turtle和OOP来制作一个自然选择模拟器,OOP对我来说是全新的。我似乎找不到我的特定问题的答案,这可能是因为我不理解它。我的代码目前看起来像这样:

  1. import turtle
  2. import time
  3. import random
  4. turtle.speed(50)
  5. turtle.setworldcoordinates(-50, -50, 1920, 1080)
  6. class Creature: # This class contains the blueprint for the 'blob' creature.
  7. def __init__(self, speed, location_x, location_y): # Initializes parameters.
  8. self.speed = speed # Speed of the blob.
  9. self.location_x = location_x # X coordinate of the blob, paired with y forms location.
  10. self.location_y = location_y # Y coordinate of the blob, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  11. def spawn(self):
  12. turtle.penup()
  13. turtle.goto(Creature.location_x, Creature.location_y) # Lets the turtle spawn in a random point.
  14. turtle.color("Blue")
  15. turtle.dot(20)
  16. def move(self):
  17. pass
  18. class Apple: # This class contains the blueprint for the food (apples) that the blobs can eat.
  19. def __init__(self, location_x, location_y):
  20. self.location_x = location_x
  21. self.location_y = location_y
  22. def spawn(self):
  23. turtle.penup()
  24. turtle.goto(Apple.location_x, Apple.location_y)
  25. turtle.color("Red")
  26. turtle.dot(20)
  27. blob1 = Creature(1.5, random.randint(0, 1000), random.randint(0, 1000))
  28. blob2 = Creature(1.5, random.randint(0, 1000), random.randint(0, 1000))
  29. blob3 = Creature(1.5, random.randint(0, 1000), random.randint(0, 1000))
  30. blob4 = Creature(1.5, random.randint(0, 1000), random.randint(0, 1000))
  31. blob5 = Creature(1.5, random.randint(0, 1000), random.randint(0, 1000))
  32. blob6 = Creature(1.5, random.randint(0, 1000), random.randint(0, 1000))
  33. blob7 = Creature(1.5, random.randint(0, 1000), random.randint(0, 1000))
  34. blob8 = Creature(1.5, random.randint(0, 1000), random.randint(0, 1000))
  35. blob9 = Creature(1.5, random.randint(0, 1000), random.randint(0, 1000))
  36. blob10 = Creature(1.5, random.randint(0, 1000), random.randint(0, 1000))
  37. apple1 = Apple(random.randint(0, 1000), random.randint(0, 1000))
  38. apple2 = Apple(random.randint(0, 1000), random.randint(0, 1000))
  39. apple3 = Apple(random.randint(0, 1000), random.randint(0, 1000))
  40. apple4 = Apple(random.randint(0, 1000), random.randint(0, 1000))
  41. apple5 = Apple(random.randint(0, 1000), random.randint(0, 1000))
  42. Creature.spawn(all)
  43. Apple.spawn(all)
  44. turtle.exitonclick()

字符串
我已经尝试改变一些东西,但没有帮助.一个解释也将是非常有帮助的,以及一些建议如何防止自己在未来遇到这个问题.此外,我不知道这是否是可能的,但你可以让你的程序使一个类的示例?我得到的错误代码如下:

  1. Traceback (most recent call last):
  2. File "c:\Users\sidne\Programs(Python)\Natural selection simulator.py", line 64, in <module>
  3. blob1.spawn()
  4. File "c:\Users\sidne\Programs(Python)\Natural selection simulator.py", line 15, in spawn
  5. turtle.goto(Creature.location_x, Creature.location_y) # Lets the turtle spawn in a random point.
  6. ^^^^^^^^^^^^^^^^^^^
  7. AttributeError: type object 'Creature' has no attribute 'location_x'

pu82cl6c

pu82cl6c1#

你有一些错误,但你离工作代码不远了。
首先,在spawn()函数中,您需要将Creature.location_xApple.location_x更改为self.location_x。对.location_y执行相同的操作。
下一个问题是调用Creature.spawn()Apple.spawn()。这是调用类本身,但我们想调用类的 * 示例 * 的方法。

  1. blobs = [Creature(1.5, random.randint(0, 1000), random.randint(0, 1000)) for x in range(10)]
  2. apples = [Apple(random.randint(0, 1000), random.randint(0, 1000)) for x in range(5)]

字符串
我继续将Creature s和Apple s放入它们自己的列表中,因为这更容易处理。这本质上与您的15行代码做同样的事情,但只有两行。
最后,我们遍历每个列表,并调用每个 * 示例 * 的.spawn()方法。

  1. for blob in blobs:
  2. blob.spawn()
  3. for apple in apples:
  4. apple.spawn()

最终结果?

x1c 0d1x的数据
希望这有帮助!

展开查看全部

相关问题