Skip to content Skip to sidebar Skip to footer

How To Animate Scale And Rotate In Pygame Using Rotozoom

Okay so Im just trying to get it working with rotations first before scaling then once I nail that the rotozoom should be easy. For the life of me I cant seem to get it working. He

Solution 1:

This is how I've made it so you can rotate your puff class:

import pygame

classPuff(pygame.sprite.Sprite):

    def__init__(self, screen):
        pygame.sprite.Sprite.__init__(self)
        self.screen = screen

        try:
            self.imagePuff = pygame.image.load("puff.jpg").convert()
        except:
            raise UserWarning, "Unable to find Puff image"    
        self.image = self.imagePuff
        self.imagePuff.set_colorkey((0,0,255))           
        self.rect = self.image.get_rect()
        self.x = self.screen.get_width()/3
        self.y = self.screen.get_height()/3+10
        self.rect.center = (self.x, self.y)

        self.lifespan = 60
        self.speed = 1
        self.count = 0
        self.angle = 0defupdate(self):

        oldCenter = self.rect.center
        self.image = pygame.transform.rotate(self.imagePuff, self.angle)
        self.rect = self.image.get_rect()
        self.rect.center = oldCenter


    defcalcPos(self):
        self.x -= 5
        self.y = self.y

    defturnLeft(self):
        self.angle = (self.angle + 45) % 360defturnRight(self):
        self.angle = (self.angle - 45) % 360if __name__  == "__main__":
    pygame.init()
    screen = pygame.display.set_mode((400,300))
    clock = pygame.time.Clock()
    background = pygame.Surface(screen.get_size())
    background.fill((0, 0, 0))
    pygame.display.set_caption("Spinning sprite!!!")
    ball = Puff(screen)
    sprites = pygame.sprite.Group()
    sprites.add(ball)
    keepGoing = Truewhile keepGoing:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False
        ball.turnRight()
        sprites.clear(screen, background)
        sprites.update()
        sprites.draw(screen)
        pygame.display.flip()   
        clock.tick(30)

Essentially the key points are:

self.image = pygame.transform.rotate(self.imagePuff, self.angle)

In the update method, and the two methods for changing the angle:

defturnLeft(self):
        self.angle = (self.angle + 45) % 360defturnRight(self):
        self.angle = (self.angle - 45) % 360

In the code above every clock tick the sprite is rotated to the right:

ball.turnRight()

As you omitted your transform method, I had to edit the code to show how to do just rotations.

Hope this helps and if you need any more help please leave a comment.

Edit:

if you want to rotate and move to the left then you only really need to add this:

defupdate(self):
    self.calcPos()  # This moves the position - 5 in the x direction
    self.rect.center = self.x, self.y
    self.rotate()  # This calls the bit to change the angleif self.count > self.lifespan:
        self.kill()
    oldCenter = self.rect.center
    self.image = pygame.transform.rotate(self.imagePuff, self.angle) # This rotates
    self.rect = self.image.get_rect()
    self.rect.center = oldCenter


defcalcPos(self):
    self.x -= 5defrotate(self):
    self.angle = (self.angle + 45) % 360

You should check out:

http://www.gamedev.net/topic/444490-pygame-easy-as-py/

If you search for "rotate.py" you'll see the a section of code on rotating sprites

EDIT:

Try changing you transform method to:

deftransform(self):
    self.count += 1.1
    self.angle += (self.count*25) % 360
    self.copyImage = pygame.transform.rotate(self.imagePuff, self.angle)
    self.newTrans = pygame.transform.scale(self.copyImage, (400,400))
    self.image =  self.newTrans

Post a Comment for "How To Animate Scale And Rotate In Pygame Using Rotozoom"