python - 按键盘上的 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 作为操作系统。
解决方案
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:
# [...]
推荐阅读
- c - C 以日期命名文件
- python - 增加我的 Python 频谱图的分辨率
- sql - 如何在另一个分组变量上添加 SQL 中列的第一个值
- intellij-idea - 在 Junit5 中执行测试类时出现“未找到测试错误”
- bots - 在 Telegram Bot 中使下一个输入消息成为 callback_query 函数的一部分
- javascript - 太多的重新渲染。react 限制渲染的数量以防止无限循环。使用效果
- android - 在华为 Map Kit 中使用 Google 的 maps sdk android 实用程序库
- python - odoo PyCharm 配置/没有名为“odoo”的模块
- c - 如何指定 gtk+ 组合框位置弹出
- c# - 如何解决asp.net mvc中的重复问题?