Python PyGame KEYDOWN无效

时间:2017-03-12 14:06:00

标签: python input menu keyboard pygame

我不知道为什么这不起作用但它没有捕获keydown。单击交叉工作正常并退出游戏。它我可能很简单,但我无法看到它。 keydown在我的程序的其他部分工作正常它只是主菜单不起作用。以下是菜单部分的代码:

        while menuOver == False:

            Screen.fill(background_colour)
            font1 = pygame.font.SysFont("Impact", 100)
            font2 = pygame.font.SysFont("Impact", 50)
            font3 = pygame.font.SysFont("Impact", 25)
            lblTitle = font1.render("Python", 100, (black))
            Screen.blit(lblTitle, (265, 50))

            lblSubTitle = font2.render("Created With Python", 100, (black))
            Screen.blit(lblSubTitle, (200, 200))

            lblEasy = font3.render('Easy - Press 1', 100, (red))
            Screen.blit(lblEasy, (100, 300))

            lblNormal = font3.render('Normal - Press 2', 100, (red))
            Screen.blit(lblNormal, (300, 300))

            lblNightmare = font3.render('Nightmare - Press 3', 100, (red))
            Screen.blit(lblNightmare, (525, 300))

            lblLeaderboard = font3.render('Press L To View Leaderboard', 100, (black))
            Screen.blit(lblLeaderboard, (90, 400))

            lblInstructions = font3.render('Press I To View Instructions', 100, (black))
            Screen.blit(lblInstructions, (425, 400))

            pygame.display.update()

            for event in pygame.event.get():

                if event.type == pygame.QUIT:
                    pygame.quit()

                if event.type == pygame.KEYDOWN:

                    if event.type == pygame.K_1:

                        menuOver = True

1 个答案:

答案 0 :(得分:1)

在您的代码中,您键入了event.type == pygame.K_1:,但它应该是if event.key == pygame.K_1

当您输入for event in pygame.event.get():时,它会将pygame.event.get()的每个元素一次插入event。当您使用eventevent.type会返回事件类型,并且当您使用event.key时,如果相关,则会按下该键。

有关详细信息,请参阅https://www.pygame.org/docs/ref/event.html

编辑:对不起,我刚刚意识到您的问题已在评论中得到解答......