Я создал небольшую игру (сейчас работаю над этим).Где я хочу распечатать ключ словаря на экране и перейти к следующему слову после нажатия клавиши пробела.Это работает хорошо, однако, когда я закончил перебирать все клавиши и нажимал кнопку 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 в качестве операционной системы.