如何使用 rotozoom 在 pygame 中设置缩放和旋转动画 [英] How to animate scale and rotate in pygame using rotozoom

查看:141
本文介绍了如何使用 rotozoom 在 pygame 中设置缩放和旋转动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我只是想在缩放之前先让它与旋转一起工作,然后一旦我确定旋转缩放应该很容易.对于我的生活,我似乎无法让它发挥作用.这是一个简单的类,我希望对象随时间旋转以及在 x 方向上进行变换.转换工作正常,但我无法让它旋转.

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

推荐答案

我是这样做的,所以你可以轮换你的粉扑类:

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)

本质上的关键点是:

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

在update方法中,还有两种改变角度的方法:

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.

如果你想旋转并向左移动,那么你只需要添加这个:

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

你应该检查一下:

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

如果你搜索rotate.py",你会看到一段关于旋转精灵的代码

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

尝试将转换方法更改为:

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

这篇关于如何使用 rotozoom 在 pygame 中设置缩放和旋转动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆