为什么将我的机芯放入函数后,我的机芯不起作用?

问题描述 投票:1回答:1
def playing():
    global in_game
    x = 250
    y = 250
    width = 10
    height = 10
    white = (255, 255, 255)
    black = (0, 0, 0)
    keys = pygame.key.get_pressed()
    while in_game:
        for event in pygame.event.get():
            print(event)
            if event.type == pygame.QUIT:
                in_game = False

        if keys[pygame.K_UP]:
            y -= 1
        elif keys[pygame.K_DOWN]:
            y += 1
        elif keys[pygame.K_LEFT]:
            x -= 1
        elif keys[pygame.K_RIGHT]:
            x += 1

        win.fill(white)
        pygame.draw.rect(win, black, (x, y, width, height))
        pygame.display.flip()

在此代码中,我将x和y作为水平和垂直移动。我在屏幕底部调用了函数。

python pygame event-loop
1个回答
2
投票

pygame.key.get_pressed()返回代表键当前状态的布尔值列表。您必须在应用程序循环中连续评估键的状态,而不是在循环前评估一次:

pygame.key.get_pressed()
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.