Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用pygame连续旋转图像?_Python_Pygame - Fatal编程技术网

Python 如何使用pygame连续旋转图像?

Python 如何使用pygame连续旋转图像?,python,pygame,Python,Pygame,我有一个图像,我想不断旋转 我想在特定的时间间隔后将它旋转一定的角度。然而,我想实现一个功能,当我点击一个特定的键时,朝着我的头部所指的方向射出一颗子弹 因此,在那个时刻,我应该如何在旋转图像中保持头部的轨迹?创建旋转图像在计算上非常昂贵。这是在程序进入主循环之前应该做一次的事情 “连续旋转”图像实际上是一组动画帧,每一帧都比前一帧有一定程度的提升 pygame函数pygame.transform.rotate()将旋转图像。在您的情况下,确定某个步长角度,然后生成N个图像可能会很有用 例如,如

我有一个图像,我想不断旋转

我想在特定的时间间隔后将它旋转一定的角度。然而,我想实现一个功能,当我点击一个特定的键时,朝着我的头部所指的方向射出一颗子弹


因此,在那个时刻,我应该如何在旋转图像中保持头部的轨迹?

创建旋转图像在计算上非常昂贵。这是在程序进入主循环之前应该做一次的事情

“连续旋转”图像实际上是一组动画帧,每一帧都比前一帧有一定程度的提升

pygame函数
pygame.transform.rotate()
将旋转图像。在您的情况下,确定某个步长角度,然后生成N个图像可能会很有用

例如,如果需要12帧旋转动画,请再创建11帧动画,每个帧旋转360/12度(是否禁用1°)

这为我们提供了一个简单的sprite类,它在实例化时预先创建帧。显然,如果你有很多精灵,重新计算每个精灵的相同帧是没有意义的,但这只是一个例子

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
当然,您可以在图形包中预先制作12帧旋转位图,并在运行时加载它们。我喜欢上面的方法,因为如果你决定移动到24帧,这一切都会随着参数的改变而发生


图像“面向”的方向只是上述类中当前的旋转索引(self.rotation)。例如,想象一个只有4帧的“旋转”——上/右/下/左。

非常感谢您的帮助!我们将实施它。