连续按下向左或向右键时如何在pygame中水平翻转图像?

问题描述 投票:0回答:1

我是代码和Pygame的新手。每当我从按向右箭头键切换到向左箭头键以使其面向正确的方向切换时,我都试图让我的子画面水平翻转。目前,我的图像可以翻转,但它每秒都在翻转,因为按键似乎每次都算作单个按键。我怎样才能解决这个问题?我希望一键按下就可以算作用户放开其中一个箭头键。这样我就可以使角色面对正确的方向。这是控制精灵运动的代码。精灵是图像。雪橇。

if event.type == pygame.KEYDOWN:
    print("User pressed a key.")

    if (event.key == pygame.K_UP):
        imageSleigh_y -= 5
    elif (event.key == pygame.K_DOWN):
        imageSleigh_y += 5
    elif (event.key == pygame.K_LEFT):
        imageSleigh_x -= 5
    elif (event.key == pygame.K_RIGHT):
        imageSleigh_x += 5

# Changing the orientation of the sleigh depending on the direction it's facing
pressed = pygame.key.get_pressed()
if pressed[pygame.K_RIGHT]:
    imageSleigh = pygame.transform.flip(imageSleigh, True, False)

if pressed[pygame.K_LEFT]:
    imageSleigh = pygame.transform.flip(imageSleigh, True, False)

clock = pygame.time.Clock()
clock.tick(500)
python image pygame sprite keypress
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.