Question

I have a class of pygame.draw.rects like the one below:

pygame.draw.rect(windowSurface, BROWN, (x+25,  y+35,   5,   5))

These are all under the class Archer(), in the function draw(x, y). However, I would like to move all of these objects as one. How would I move all of the objects within this class without moving each individual one? My code is here: http://pastebin.com/APkgTnTk

Was it helpful?

Solution

Include these objects in a RenderUpdates group. This lets you draw the items on the screen as one, and call functions on them as one. For example, you could do:

window_surfaces = pygame.sprite.RenderUpdates()

then include inside the init function of the class you want to call in a group:

window_surfaces.add(self)

then, when you want to call a function on all of them, you can call

window_surfaces.draw()

You might consider containing the individual arguments for each class inside "self" beforehand.

Also, if you just want to draw a group of sprites at their current location, you could just call:

pygame.display.update(window_surfaces.draw(screen))

Be aware, however, that this draw function is not the one you defined, but one built in to the render updates object. If you use render updates you probably don't want to overwrite this function.

Also, it seems that you may, in fact, not be using sprites. If this is the case (and you may want to seriously consider using them, they come with many useful features that you have to build from scratch built into them) than you will want to have the classes append themselves to a list in the init function, then use a for loop to call the function like so:

for itemtodraw in mygroup:
    itemtodraw.draw()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top