简体   繁体   English

如何使用rotozoom在pygame中缩放和旋转动画

[英]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. 对于我的一生,我似乎无法正常工作。 Heres a simple class that I want the object to rotate over time as well a transform in the x direction. 这是一个简单的类,我希望对象随时间旋转以及在x方向上进行变换。 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

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. 当您省略了transform方法时,我不得不编辑代码以演示如何进行旋转。

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/ 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 如果您搜索“ rotate.py”,您将看到有关旋转精灵的一段代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM