我正在尝试使用我相当熟悉的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'
型
1条答案
按热度按时间pu82cl6c1#
你有一些错误,但你离工作代码不远了。
首先,在
spawn()
函数中,您需要将Creature.location_x
和Apple.location_x
更改为self.location_x
。对.location_y
执行相同的操作。下一个问题是调用
Creature.spawn()
和Apple.spawn()
。这是调用类本身,但我们想调用类的 * 示例 * 的方法。字符串
我继续将
Creature
s和Apple
s放入它们自己的列表中,因为这更容易处理。这本质上与您的15行代码做同样的事情,但只有两行。最后,我们遍历每个列表,并调用每个 * 示例 * 的
.spawn()
方法。型
最终结果?
x1c 0d1x的数据
希望这有帮助!