Python游戏形状旋转问题

Python游戏形状旋转问题,python,python-3.x,rotation,pygame,Python,Python 3.x,Rotation,Pygame,我是Pygame的新手,我正在尝试制作一个简单的小游戏,我现在遇到了矩形旋转的问题。它自身中的形状会旋转,但是由于某种原因,当形状旋转90度时,它会从地板上浮动几个像素,下面的图像显示了发生的情况: #Player Class class Player(pygame.sprite.Sprite): """ This class represents the bar at the bottom that the player controls. """

我是Pygame的新手,我正在尝试制作一个简单的小游戏,我现在遇到了矩形旋转的问题。它自身中的形状会旋转,但是由于某种原因,当形状旋转90度时,它会从地板上浮动几个像素,下面的图像显示了发生的情况:

    #Player Class
class Player(pygame.sprite.Sprite):
    """ This class represents the bar at the bottom that the player
        controls. """


    # List of sprites we can bump against
    level = None

    # -- Methods
    def __init__(self):
        """ Constructor function """

        # Call the parent's constructor
        pygame.sprite.Sprite.__init__(self)

        # Create an image of the block, and fill it with a color.
        # This could also be an image loaded from the disk.
        self.width = 40
        self.height = 60
        self.image = pygame.Surface([self.width, self.height])
        self.image.fill(RED)
        self.baseImage = self.image        

        # Set a referance to the image rect.
        self.rect = self.image.get_rect()
    def update(self):
        """ Move the player. """

        # Move left/right
        self.rect.x += self.change_x

        # See if we hit anything
        block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
        for block in block_hit_list:
            # If we are moving right,
            # set our right side to the left side of the item we hit
            if self.change_x > 0:
                self.rect.right = block.rect.left
            elif self.change_x < 0:
                # Otherwise if we are moving left, do the opposite.
                self.rect.left = block.rect.right

    # Player-controlled movement:
    def go_left(self):
        """ Called when the user hits the left arrow. """
        self.change_x = -10

    def go_right(self):
        """ Called when the user hits the right arrow. """
        self.change_x = 10

    def stop(self):
        """ Called when the user lets off the keyboard. """
        self.change_x = 0

    def turn_r(self):       
        self.image = pygame.transform.rotate(self.image, angle)
        self.change_x = 0

    def turn_l(self):
        count = -90
        self.image = pygame.transform.rotate(self.image, angle)
        self.change_x = -0


您的问题似乎出现在形状的顶部和左侧,您可能需要重新对齐中心,例如:

def turn_r(self):       
    center_x = self.rect.x + (self.rect.width / 2)
    center_y = self.rect.y + (self.rect.height / 2)

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

    new_center_x = self.rect.x + (self.rect.width / 2)
    new_center_y = self.rect.y + (self.rect.height / 2)

    self.change_x = center_x - new_center_x
    self.change_y = center_y - new_center_y
或者,可以将形状的新Y设置为返回底部:

def turn_r(self):       
    bottom = self.rect.y + self.rect.height
    self.image = pygame.transform.rotate(self.image, angle)
    new_bottom = self.rect.y + self.rect.height
    self.change_y = bottom - new_bottom

我不太了解PyGame,代码可能有一些问题,但它是这样的。

在将其旋转90°之前,宽度是40,高度是60。在您打开它之后,这两个值被交换,因此高度现在只有40,而您的代码仍然在参考原始60像素的高度上闪烁它


我想说,最简单的方法是让程序旋转图像,然后重置到另一个高度级别,使20个像素的差异比以前低。

为了帮助其他人,我刚刚找出了代码的错误。问题是,在形状旋转后,矩形保持不变,我通过简单地在turn\r和turn\l函数中更新矩形来解决这个问题。请随时寻求帮助

我明白了,但问题是,当它旋转时,可见的形状会旋转,但由于某种原因,实际的物理矩形保持在相同的位置,例如,当我旋转它并将其移动到墙上时,一半的形状会穿过它。谢谢你的帮助,不过我会玩弄它来修复它!干杯,朋友
def turn_r(self):       
    bottom = self.rect.y + self.rect.height
    self.image = pygame.transform.rotate(self.image, angle)
    new_bottom = self.rect.y + self.rect.height
    self.change_y = bottom - new_bottom