How to animate scale and rotate in pygame using rotozoom

。_饼干妹妹 提交于 2020-01-04 15:27:53

问题


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. Heres a simple class that I want the object to rotate over time as well a transform in the x direction. The transform is working fine but i just cant get it to rotate.

class Puff(pygame.sprite.Sprite):

    def __init__(self, screen):
        pygame.sprite.Sprite.__init__(self)
        self.screen = screen
        try:
            self.imagePuff = pygame.image.load(os.path.join("Images","puff.tga")).convert()
        except:
            raise UserWarning, "Unable to find Puff image"    
        self.imagePuff.set_colorkey((0,0,255))           

        self.image = self.imagePuff
        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 = 0

    def update(self):
        self.calcPos()
        self.rect.center = self.x, self.y
        self.transform()
        if self.count > self.lifespan:
            self.kill()

    def calcPos(self):
        self.x -= 5
        self.y = self.y

    def transform(self):
        self.count += 1.1
        self.angle += self.count*25
        self.newTrans = pygame.transform.scale(self.copyImage, (400,400))
        self.newRect = self.newTrans.get_rect()
        self.newRect.center = self.rect.center

回答1:


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

import pygame

class Puff(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 = 0


    def update(self):

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


    def calcPos(self):
        self.x -= 5
        self.y = self.y

    def turnLeft(self):
        self.angle = (self.angle + 45) % 360

    def turnRight(self):
        self.angle = (self.angle - 45) % 360



if __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 = True
    while 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:

    def turnLeft(self):
        self.angle = (self.angle + 45) % 360

    def turnRight(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:

def update(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 angle
    if 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


def calcPos(self):
    self.x -= 5

def rotate(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:

def transform(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


来源:https://stackoverflow.com/questions/16748897/how-to-animate-scale-and-rotate-in-pygame-using-rotozoom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!