Python pygame.key.get_pressed()不起作用
我在堆栈溢出上读过类似的问题,但没有帮助。这是我的密码:Python pygame.key.get_pressed()不起作用,python,pygame,Python,Pygame,我在堆栈溢出上读过类似的问题,但没有帮助。这是我的密码: import pygame from pygame.locals import * pygame.init() screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption('Hello World') pygame.mouse.set_visible(1) done = False clock = pygame.time.Clock() while
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Hello World')
pygame.mouse.set_visible(1)
done = False
clock = pygame.time.Clock()
while not done:
clock.tick(60)
keyState = pygame.key.get_pressed()
if keyState[pygame.K_ESCAPE]:
print('\nGame Shuting Down!')
done = True
按
escape
不会退出游戏或打印消息。这是虫子吗?如果我打印keyState[pygame.K_ESCAPE]的值,它总是为零。我可以建议使用事件que吗?这可能是一个更好的主意:
while True: #game loop
for event in pygame.event.get(): #loop through all the current events, such as key presses.
if event.type == QUIT:
die()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE: #it's better to have these as multiple statments in case you want to track more than one type of key press in the future.
pauseGame()
这样做:
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Hello World')
pygame.mouse.set_visible(1)
done = False
clock = pygame.time.Clock()
while not done:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
key = pygame.key.get_pressed()
if key[K_ESCAPE]:
print('\nGame Shuting Down!')
pygame.display.flip()
你不需要if语句上的
pygame.
,你也应该调用pygame.display.flip()
,这样它会正确显示窗口,然后你需要一个事件循环来退出程序。问题是你没有处理pygame的事件队列。在循环结束时,只需调用pygame.event.pump()
,代码即可正常工作:
...
while not done:
clock.tick(60)
keyState = pygame.key.get_pressed()
if keyState[pygame.K_ESCAPE]:
print('\nGame Shuting Down!')
done = True
pygame.event.pump() # process event queue
从(我的)重点:
pygame.event.pump()
内部处理pygame事件处理程序
pump()->None
对于游戏的每一帧,您都需要对事件队列进行某种调用。这可确保您的程序可以在内部与操作系统的其余部分交互。如果您在游戏中未使用其他事件函数,则应调用pygame.event.pump()以允许pygame处理内部操作
如果您的程序通过其他pygame.event函数一致地处理队列上的事件,则不需要此函数
有一些重要的事情必须在事件队列内部处理。主窗口可能需要重新绘制或响应系统如果调用事件队列的时间过长,系统可能会判定您的程序已锁定
请注意,如果只调用主循环中的任何地方的
pygame.event.get()
,则不必这样做;如果没有,您可能应该调用pygame.event.clear()
,这样事件队列就不会被填满。您应该提供pygame
和python
的版本
当我使用pygame1.9.4dev
和python3.6.5
我在降级pygame
并重新安装python
后修复了这个问题
注意:如果使用pyenv
,则必须确保在安装python时设置了--enable framework
选项
# exit current virtualenv
$ pyenv deactivate
# reinstall python
$ PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.6.5
# And reinstall pygame again.
pip install https://github.com/pygame/pygame/archive/1.9.3.zip
使用以下代码检查它是否正常工作
import pygame
import sys
def run():
"""Initialize pygame, settings, and screen object."""
pygame.init()
screen = pygame.display.set_mode((300, 200))
pygame.display.set_caption('Keyboard Test')
# main loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
print('KEY pressed is ' + str(event.key) + '.')
# Make the most recently drawn screen visible.
pygame.display.flip()
run()
我以前使用过这种方法,但它不能很好地处理同时按下的键。您可以将布尔值设置为变量,并在有
键下时使其为真,在有键上时使其再次为假。然后你只需检查哪些变量是真的。我将用一个例子编辑我的答案second@ZeroDivide您可以使用事件进行混合和匹配,并根据应使用的键获取状态。实际上,您可能只需要调用pygame.event.poll()
,这可能会稍微快一点。