简体   繁体   English

我应该如何使用pygame连续旋转图像?

[英]how should I rotate an image continuously using pygame?

I have an image which I want to rotate continuously. 我有一个想要连续旋转的图像。

I thought of rotating it through a certain angle after specific interval of time . 我想在特定的时间间隔后将它旋转一定的角度。 However, I wanted to implement a feature of shooting a bullet in the direction my head of the image is pointing at that moment of time when I click a specific key. 但是,我想实现一个功能,就是当我点击一个特定键时,我的图像头指向那个时刻拍摄子弹的功能。

So at that moment of time , how should I keep the track of my head in the rotating image? 所以在那个时刻,我应该如何在旋转的图像中保持我的头部轨迹?

Creating rotated images is computationally expensive. 创建旋转图像在计算上是昂贵的。 It is something that should be done once before your program enters its main loop. 在程序进入主循环之前,应该完成一次

A "continuously rotating" image is really a set of animation frames, each one advanced by some degree from the previous. “连续旋转”的图像实际上是一组动画帧,每个动画帧在某种程度上都与前一个相同。

The pygame function pygame.transform.rotate() will rotate an image. pygame函数pygame.transform.rotate()将旋转图像。 It might be useful in your case to decide some step-angle, then make N images. 在您的情况下,确定一些步角,然后制作N个图像可能会很有用。

For example, if you desired 12 frames of rotation animation, create 11 more frames of animation each rotated by 360 / 12 degrees (is this off-by-one?). 例如,如果您需要12帧旋转动画,请再创建11个动画帧,每个帧旋转360度/ 12度(这是一个一个吗?)。

This gives us a simple sprite class, which pre-creates the frames at the time of instantiation. 这给了我们一个简单的精灵类,它在实例化时预先创建了帧。 Obviously if you had lots of sprites, it does not make sense to re-compute the same frames for each of them, but it serves as an example. 显然,如果你有很多精灵,为每个精灵重新计算相同的帧是没有意义的,但它就是一个例子。

class RotatingSprite( pygame.sprite.Sprite ):

    def __init__( self, bitmap, rot_count=12 ):
        pygame.sprite.Sprite.__init__( self )
        self.rect        = bitmap.get_rect()
        self.rect.center = ( random.randrange( 0, WINDOW_WIDTH ), random.randrange( 0, WINDOW_HEIGHT ) )
        # start with zero rotation
        self.rotation    = 0
        self.rotations   = [ bitmap ]
        self.angle_step  = rot_count
        self.angle_slots = 360 // self.angle_step
        # pre-compute all the rotated images, and bitmap collision masks
        for i in range( 1, self.angle_slots ):
            rotated_image = pygame.transform.rotate( bitmap, self.ANGLE_STEP * i )
            self.rotations.append( rotated_image )
        self._setRotationImage( 0 ) # sets initial image & rect

    def rotateRight( self ):
        if ( self.rotation == 0 ):
            self._setRotationImage( self.angle_slots - 1 )
        else:
            self._setRotationImage( self.rotation - 1 )

    def rotateLeft( self ):
        if ( self.rotation == self.angle_slots - 1 ):
            self._setRotationImage( 0 )
        else:
            self._setRotationImage( self.rotation + 1 )

    def _setRotationImage( self, rot_index ):
        """ Set the sprite image to the correct rotation """
        rot_index %= self.angle_slots
        self.rotation = rot_index
        # Select the pre-rotated image 
        self.image = self.rotations[ rot_index ]
        # We need to preserve the centre-position of the bitmap, 
        # as rotated bitmaps will (probably) not be the same size as the original
        centerx = self.rect.centerx
        centery = self.rect.centery
        self.rect = self.image.get_rect()
        self.rect.center = ( centerx, centery )

    def update( self ):
        self.rotateRight()  # do something

You could of-course pre-make the 12 frames of rotated bitmap in a graphics package, and just load them in at run-time. 您当然可以在图形包中预先制作12帧旋转位图,并在运行时加载它们。 I like the above method because if you decide to move to say 24 frames, it all happens with the change of a parameter. 我喜欢上面的方法,因为如果你决定移动说24帧,这一切都发生在参数的变化。

The direction the image is "facing" is simply the current index of rotation ( the self.rotation ) in the class above. 图像“面向”的方向仅仅是上面类中的当前旋转指数( self.rotation )。 For example, Imagine a "rotation" with just 4 frames - up/right/down/left. 例如,想象一下只有4帧的“旋转” - 向上/向右/向下/向左。

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

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