为什么我的 pygame 键盘输入不起作用?

时间:2021-01-29 11:15:11

标签: input keyboard

我遇到了一个问题,当我按下向上箭头时,鸟不会上升。游戏似乎没有检测到我的键盘输入。重力功能工作正常。请帮忙。

import pygame
import sys

pygame.init()

screen = pygame.display.set_mode((800, 600))

birdImg = pygame.image.load("bird.png")
birdImg = pygame.transform.scale(birdImg, (64, 64))
birdX = 300
birdY: int = 300
pipeImg = pygame.image.load("flappybirdpipe.png")
pipeImg = pygame.transform.scale(pipeImg, (100, 300))

def gravity():
    global birdY
    birdY += 3


background = pygame.image.load("flappybird.png")
background = pygame.transform.scale(background, (800, 600))

while True:
    screen.blit(background, (0, 0))
    screen.blit(birdImg, (birdX, birdY))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if pygame.event == pygame.KEYDOWN:
            if pygame.key == pygame.K_UP:
                birdY += -5
        if pygame.event == pygame.KEYUP:
            if pygame.key == pygame.K_UP:
                birdY = 0

    
    gravity()
    pygame.display.flip()
    pygame.display.update()

0 个答案:

没有答案