[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [pygame] Rotating Sprites
Leonardo Santagada wrote:
> thanks but I also want to know how to make the sprite rotations and
> how to put it in memory to grab it fast. Should I make a for loop
> with the rotozoom or use the pygame.transform.rotate and should I
> put my images in a list a dict or tuple? why don't pygame already
> come with a sprite object that do that?
a simple loop to create the images will likely work best. it might be a
good idea to include something like this with pygame. if someone creates
one it should definitely be in the code repository.
you may also end up preferring pygame.tranform.rotate() over the rotozoom
function. the straight rotate has cleaner "90 degree" rotation than
rotozoom, plus rotozoom will convert your image to you pixel alphas, which
are slower than regular colorkeys. if you do go with rotozoom, i recommend
calling convert() on the result and resetting the proper colorkey?
in any event, the class would look something like this...
class RotateSprite(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
img = pygame.image.load('spriteimage')
self.generate_rotations(img, 10)
def generate_rotations(self, image, numsteps=5):
self.rotates = []
for angle in range(0, 360, numsteps):
rot = pygame.transform.rotate(image, angle)
self.rotates.append(rot)
self.angle = 0
self.image = self.rotates[0]
self.rect = self.image.get_rect()
self.numsteps = numsteps
def update_rotation(self):
oldcenter = self.rect.center
pick = (self.angle % 360) / self.numsteps
self.image = self.rotates[pick]
self.rect = self.image.get_rect()
self.rect.center = oldcenter
def update(self): #simple animation
self.angle += 2
self.update_rotation()
____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org