簡體   English   中英

Python PyGame KEYDOWN不起作用

[英]Python PyGame KEYDOWN not working

我不知道為什么這不起作用,但它沒有捕獲按鍵。 單擊十字可以正常工作並退出游戲。 我可能很簡單,但我看不到它。 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

在您的代碼中,您鍵入event.type == pygame.K_1:但應該是if event.key == pygame.K_1

當您for event in pygame.event.get():輸入for event in pygame.event.get():每個元素pygame.event.get()插入到event 當您使用event.typeevent返回事件類型,而在使用event.key時,按下鍵(如果相關)。

有關更多信息,請參閱https://www.pygame.org/docs/ref/event.html

編輯:對不起,我只是意識到您的問題已在評論中得到了解答...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM