[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Moving sprites.



Alright, create a sprite group with all your sprites in it, like so:

group = pygame.sprite.RenderUpdates(self.ship1, self.ship2)

you can add more sprites if you need them. You can also use group.add(sprite) to add a sprite to an existing group.
That's basically all for setup. Now, in your rendering code, you move your sprites just like the way you did:

self.ship.rect.center = [loc1, loc2]

Then you use the sprite group to clear the sprites that were drawn in the previous render, and then you draw the sprites for the current render. Sort of like your code, except you're using a group:

group.clear(self.screen, self.background)
rectlist = group.draw(self.screen)

note that the draw function returned a list of rects. These are the areas that were affected by the draw and clear calls, and we must update these only on the screen. For that we have the display.update function:

pygame.display.update(rectlist)

and that should basically do it.

On Sun, Jul 27, 2008 at 11:38 PM, <percivalkelley@xxxxxxxxx> wrote:
So I've read piman's tutorial and checked out a few others. I'm still
confused. I don't really understand the process of moving a sprite
around. I would like to use RenderUpdates and such, but I'm lost.

I basically have a simple script that moves a ship around a background
(the background is an image.)

To create the sprite I'm using:

class Units(pygame.sprite.Sprite):

       def __init__(self, img, loc1 = 250, loc2 = 250):
               pygame.sprite.Sprite.__init__(self) #start up sprites

               #locs
               self.loc1 = loc1
               self.loc2 = loc2

               #create sprite
               self.image, self.rect = load_image(img)
               self.rect.center = [self.loc1, self.loc2]

I create the ship like this:
self.ship = Units('bship.png')
self.screen.blit(self.ship.image, self.ship.rect)

To redraw the ship when it moves I'm using this:

self.ship.loc1 = n1 #n1 and n2 are setup earlier in the code that
looks for key presses and sets the pixel difference
self.ship.loc2 = n2
self.ship.rect.center = [self.ship.loc1, self.ship.loc2]
print self.ship.rect.center
self.screen.blit(self.bg, (0,0))
self.screen.blit(self.ship.image, self.ship.rect.center)
pygame.display.flip()

I'm really pretty confused at this point. I know I'm just doing
something stupid, but I haven't managed to get any other code working
to redraw the ship properly when it's moved. I don't have the code
from when I was doing it wrong, but basically, can someone point me in
the right direction to changing this over to just redraw the
appropriate areas?

Thanks, and sorry this was rambling.