Python Pygame形状旋转问题

时间:2014-04-16 18:19:43

标签: python python-3.x rotation pygame

我对Pygame相当新,而且我正在尝试制作一个简单的小游戏,我目前在旋转矩形方面遇到问题。它的形状自我旋转然而由于某种原因,当形状旋转时说90度它从地板浮动几个像素,这里有一个图像来显示发生了什么:

enter image description here

    #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

#--------主程序循环-----------

 while not done:
        for event in pygame.event.get(): # User did something
            if event.type == pygame.QUIT: # If user clicked close
                done = True # Flag that we are done so we exit this loop

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    player.go_left()                
                if event.key == pygame.K_RIGHT:
                    player.go_right()
                if event.key == pygame.K_UP:
                    player.jump()
                if event.key == pygame.K_d:
                    player.turn_r()
                    count += 90
                if event.key == pygame.K_a:
                    player.turn_l()
                    count -= 90

3 个答案:

答案 0 :(得分: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,代码可能有一些问题,但它是这样的。

答案 1 :(得分:0)

在将其旋转90°之前,宽度为40,高度为60.转动后,这两个值会被交换,因此现在高度仅为40,而您的代码仍然在高度上将其高亮显示为60像素。

我想说最简单的方法是让程序转动图像并重置到另一个高度水平,这个高度水平比以前低20个像素。

答案 2 :(得分:0)

只是为了帮助别人,我才弄明白代码出了什么问题。问题是,在形状旋转之后,rect保持不变,我通过简单地更新turn_r和turn_l函数中的rect来解决这个问题。随意寻求帮助!