首页 > 解决方案 > 按键盘上的 Q 键无法完成游戏

问题描述

我创建了一个小游戏(目前正在开发中)。我想在屏幕上打印出字典的键并在按下空格键后传递到下一个单词。它运行良好,但是,当我完成对所有键的迭代并按 Q 按钮退出游戏时,它什么也没做。

这是我的代码:

import sys 
pygame.init() 
pygame.font.init() 

keywords  = {

            'auto': 'gives a local variable a local lifetime',
            'break': 'exits out of a compound statement',
            'case': 'a branch in a switch-statement',
            'char': 'a character data type',
            'const': 'makes a variable unmodifiable',
            'continue': 'continues to the top of a loop',
            'default': 'default branch in a switch-statement',
            'do': 'starts a do-while loop',
            'double': 'a double floating-point data type',
            'else': 'an else branch of an if-statement',
            'enum': 'defines a set of int constants',
            'extern': 'declares an identifier is defined externally',
            'float': 'a floating-point data type',
            'for': 'starts a for loop',
            'goto': 'jumps to a label',
            'if': 'starts an if statement',
            'int': 'an integer data type',
            'long': 'a long integer data type',
            'register': 'declares a variable be stored in a CPU register',
            'return': 'returns from a function',
            'short': 'a short integer data type',
            'signed': 'a signed modifier for integer data types',
            'sizeof': 'determines the size of the data',
            'static': 'preserves variable value after its scope exits',
            'struct': 'combine variables into a single record',
            'switch': 'starts a switch-statement',
            'typedef': 'creates a new type',
            'union': 'starts an union-statement',
            'unsigned': 'an unsigned modifier for integer data types',
            'void': 'declares a data type empty',
            'volatile': 'declares a variable might be modified elsewhere',
            'while': 'starts a while loop'

}



counter  = 0

size = (500, 700) 
screen = pygame.display.set_mode(size)
myfont = pygame.font.SysFont("Comic Sans MS", 30) 

while True:

    for event in pygame.event.get():

        if event.type == pygame.K_q:
            sys.exit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                try: 
                    counter += 1 
                    list_keys = list(keywords.keys()) 
                    screen.fill((255,255,255))
                    keyword = myfont.render(list_keys[counter], False, (0,0,0))
                    screen.blit(keyword, (200, 350))

                except IndexError: 
                    end_of_game_text = myfont.render("end of flashcards", False,(0,0,0))
                    screen.blit(end_of_game_text, (175, 325))


    pygame.display.flip()

它是否与在while循环中设置按键事件的位置有关?它必须在关键事件之后发生吗?

我正在使用 python 2.7 和 Windows 10 作为操作系统。

标签: pythonpygame

解决方案


pygame.K_q is not an even type (see pygame.event.EventType, it is a key see (pygame.key).
Verify if the event type is pygame.KEYDOWN (or pygame.KEYUP), then compare event.key to the k key (pygame.K_q). e.g:

while True:

    for event in pygame.event.get():

        if event.type == pygame.KEYDOWN:

            if event.key == pygame.K_q:
                sys.exit()

            elif event.key == pygame..K_SPACE:
                # [...]

推荐阅读