如何将多个演员放入列表中并让他们移动 - python pygame

How do I put multiple actors in a list and have them move - python pygame

如何移动 pygame 列表中的演员,这是我尝试过的方法:`

boxes = []
for i in range(10):
    h = random.randint(0,200)
    w = random.randint(0,200)
    boxes.append( Actor("red.png"))
def draw():

    red.draw()
for i in range(len(boxes)):
  boxes[i].center = (-570,250 -50*i)`

但是它给了我一个错误,比如 red is not defined。我该如何解决这个问题?

您必须设置 Actor 对象的 xy 属性:

boxes = []
for i in range(10):
    actor = Actor("red.png")
    actor.x = random.randint(0, 200)
    actor.y = random.randint(0, 200)
    boxes.append(actor)

如果你想移动物体,你需要改变坐标。例如:

for box in boxes:
    box.x += 1