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

yebdmbv4  于 5个月前  发布在  Python
关注(0)|答案(1)|浏览(68)

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

import turtle
import time
import random

turtle.speed(50)
turtle.setworldcoordinates(-50, -50, 1920, 1080)

class Creature: # This class contains the blueprint for the 'blob' creature.
    def __init__(self, speed, location_x, location_y):  # Initializes parameters.
        self.speed = speed                              # Speed of the blob.
        self.location_x = location_x                    # X coordinate of the blob, paired with y forms location.
        self.location_y = location_y                    # Y coordinate of the blob, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    def spawn(self):
        turtle.penup()
        turtle.goto(Creature.location_x, Creature.location_y)   # Lets the turtle spawn in a random point.
        turtle.color("Blue")
        turtle.dot(20)
    def move(self):
        pass

class Apple: # This class contains the blueprint for the food (apples) that the blobs can eat.
    def __init__(self, location_x, location_y):
        self.location_x = location_x
        self.location_y = location_y
    def spawn(self):
        turtle.penup()
        turtle.goto(Apple.location_x, Apple.location_y)
        turtle.color("Red")
        turtle.dot(20)
    
blob1 =  Creature(1.5, random.randint(0, 1000), random.randint(0, 1000))
blob2 =  Creature(1.5, random.randint(0, 1000), random.randint(0, 1000))
blob3 =  Creature(1.5, random.randint(0, 1000), random.randint(0, 1000))
blob4 =  Creature(1.5, random.randint(0, 1000), random.randint(0, 1000))
blob5 =  Creature(1.5, random.randint(0, 1000), random.randint(0, 1000))
blob6 =  Creature(1.5, random.randint(0, 1000), random.randint(0, 1000))
blob7 =  Creature(1.5, random.randint(0, 1000), random.randint(0, 1000))
blob8 =  Creature(1.5, random.randint(0, 1000), random.randint(0, 1000))
blob9 =  Creature(1.5, random.randint(0, 1000), random.randint(0, 1000))
blob10 = Creature(1.5, random.randint(0, 1000), random.randint(0, 1000))

apple1 = Apple(random.randint(0, 1000), random.randint(0, 1000))
apple2 = Apple(random.randint(0, 1000), random.randint(0, 1000))
apple3 = Apple(random.randint(0, 1000), random.randint(0, 1000))
apple4 = Apple(random.randint(0, 1000), random.randint(0, 1000))
apple5 = Apple(random.randint(0, 1000), random.randint(0, 1000))

Creature.spawn(all)

Apple.spawn(all)

turtle.exitonclick()

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

Traceback (most recent call last):
  File "c:\Users\sidne\Programs(Python)\Natural selection simulator.py", line 64, in <module>
    blob1.spawn()
  File "c:\Users\sidne\Programs(Python)\Natural selection simulator.py", line 15, in spawn
    turtle.goto(Creature.location_x, Creature.location_y)   # Lets the turtle spawn in a random point.
                ^^^^^^^^^^^^^^^^^^^
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()。这是调用类本身,但我们想调用类的 * 示例 * 的方法。

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

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

for blob in blobs:
    blob.spawn()

for apple in apples:
    apple.spawn()

最终结果?

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

相关问题