Search code examples
pythonpython-3.xpgzero

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


How can I move actors in a list in pygame, here's what I've tried: `

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)`

But it gives me an error like red is not defined. How do I fix this?


Solution

  • You have to set the x and y attribute of the Actor object:

    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)
    

    If you want to move the objects, you need to change the coordinates. e.g:

    for box in boxes:
        box.x += 1