pygame 未检测到按键

pygame not detecting keypresses

我一直在尝试让 pygame 检测按键,以便稍后我可以在屏幕上四处移动角色。我是编码新手,所以我可能只是遗漏了一些简单的东西,但我无法弄清楚代码有什么问题,我应该切换到 pygame.key.get_pressed() 还是什么?提前致谢。

running = True
while running: 
    screen.fill((0,0,0))
    # set background
    screen.blit(background, (0,0))
    player(playerX, playerY)
    pygame.display.update()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        
# detecting key presses
    if event.type == pygame.KEYDOWN:
        if event.type == pygame.K_LEFT:
            print("left")
        if event.type == pygame.K_RIGHT:
            print("right")
        if event.type == pygame.K_UP:
            print("up")
    if event.type == pygame.KEYUP:
        if event.type == pygame.K_LEFT or event.type == pygame.K_RIGHT:
            print("stop")
        pygame.display.update()

您需要将这部分代码缩进 for 循环中:

    if event.type == pygame.KEYDOWN:
        if event.type == pygame.K_LEFT:
            print("left")
        if event.type == pygame.K_RIGHT:
            print("right")
        if event.type == pygame.K_UP:
            print("up")
    if event.type == pygame.KEYUP:
        if event.type == pygame.K_LEFT or event.type == pygame.K_RIGHT:
            print("stop")
        pygame.display.update()

否则,event 将始终只是 pygame.event.get() 列表中的最后一个 event

所以基本上,从

running = True
while running: 
    screen.fill((0,0,0))
    # set background
    screen.blit(background, (0,0))
    player(playerX, playerY)
    pygame.display.update()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        
# detecting key presses
    if event.type == pygame.KEYDOWN:
        if event.type == pygame.K_LEFT:
            print("left")
        if event.type == pygame.K_RIGHT:
            print("right")
        if event.type == pygame.K_UP:
            print("up")
    if event.type == pygame.KEYUP:
        if event.type == pygame.K_LEFT or event.type == pygame.K_RIGHT:
            print("stop")
        pygame.display.update()

至:

running = True
while running: 
    screen.fill((0,0,0))
    # set background
    screen.blit(background, (0,0))
    player(playerX, playerY)
    pygame.display.update()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                print("left")
            if event.key == pygame.K_RIGHT:
                print("right")
            if event.key == pygame.K_UP:
                print("up")
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                print("stop")
            pygame.display.update()

(注意我用 event.key 替换了你的部分 event.type。)

这是Indentation的事情。您必须在事件循环而不是应用程序循环中评估事件。
如果你想确定某个键是否被按下,你必须验证事件类型是否为 pygame.KEYDOWN(或 pygame.KEYUP 用于按钮释放)以及是否为 .key 属性事件等于关键枚举器。

running = True
while running: 
    screen.fill((0,0,0))
    # set background
    screen.blit(background, (0,0))
    player(playerX, playerY)
    pygame.display.update()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        
    # INDENTATION
    #-->|
        # detecting key presses
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:  #<-- "key" instead of "type"
                print("left")
            if event.key == pygame.K_RIGHT: #<-- "key" instead of "type"
                print("right")
            if event.key == pygame.K_UP:    #<-- "key" instead of "type"
                print("up")
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: #<-- "key" instead of "type"
                print("stop")
    #-->|
    # INDENTATION

但是,我建议使用 pygame.key.get_pressed() 而不是 KEYDOWN 事件。

键盘事件(参见 pygame.event 模块)仅在键状态改变时发生一次。 KEYDOWN 事件在每次按下一个键时发生一次。 KEYUP 每松开一个键就会出现一次。使用键盘事件进行单个动作或一步步移动

pygame.key.get_pressed() returns a sequence with the state of each key. If a key is held down, the state for the key is True, otherwise False. Use pygame.key.get_pressed() 评估按钮的当前状态并获得连续移动。

clock = pygame.time.Clock()
velocity = 5

running = True
while running: 
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        
    keys = pygame.key.get_pressed()
    playerX += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * velocity
    playerY += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * velocity

    screen.blit(background, (0,0))
    player(playerX, playerY)
    pygame.display.update()