Pygame翻转继续在错误的时间翻转

时间:2019-04-25 13:59:32

标签: python pygame

我试图使图像在pygame上水平翻转,但是我对它的工作方式有些困惑

我试图使用pygame.transform.flip(bool_x,bool_y)函数,但是它似乎采用了两个布尔参数,这让人很困惑,我正在尝试使我的玩家在x_speed为负数时使用或零(向左),播放器将朝左,而x_speed为正(向右),则播放器将朝右。

class Player:
    [...]

    def turn_around(self, direction):
        if direction == "left":
            self.picture = pygame.transform.flip(self.picture, True, False)

        elif direction == "right":
            pass

while True:
    [...]

    if keyboard_in == False:
        if joystick:
            movement = 3
            axis_x, axis_y = (joystick.get_axis(0), joystick.get_axis(1))
            if abs(axis_x) > 0.1:
                player_one.speed_x = movement * axis_x

    if player_one.speed_x >= 0:
        player_one.turn_around("left")

    if player_one.speed_x < 0:
        player_one.turn_around("right")

实际结果是,当玩家移动时,玩家不断地向左或向右翻转,而当我放开操纵杆时,它似乎面向随机方向:

the pic

很抱歉,如果该图难以理解,则很难获得准确的图片,因为子画面会非常快地左右翻转

1 个答案:

答案 0 :(得分:0)

使用 2 张图片。一左一右:

class Player:
    def __init__():
        # [...]

        self.right = self.picture
        self.left = pygame.transform.flip(self.picture, True, False)

根据方向选择图像:

class Player:
    # [...]

    def turn_around(self, direction):
        if direction == "left":
            self.picture = self.left

        elif direction == "right":
            self.picture = self.right