pygame2.3.0中的key.get_pressed()问题
pygame2.0之前的代码没有问题。但是最近用了pygame2.3.0就有问题了。问题就是pygame.key.get_pressed() 在获取键盘字母的时候 ,无效。下面代码我设置了上下左右键,使用的时候没有任何问题,但是按下q键的时候上下左右都会失效。
import pygame,sys
pygame.init()
screen = pygame.display.set_mode((1280,720))
image = pygame.Surface((32,64))
image.fill('green')
rect = image.get_rect(center = (640,360))
direction = pygame.math.Vector2()
pos = pygame.math.Vector2(rect.center)
speed = 2
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys:
direction.y = -1
elif keys:
direction.y = 1
else:
direction.y = 0
if keys:
direction.x = -1
elif keys:
direction.x = 1
else:
direction.x = 0
if keys:
print('这边是重点')
if direction.magnitude() > 0:
direction = direction.normalize()
pos.x += direction.x * speed
rect.centerx = pos.x
pos.y += direction.y * speed
rect.centery = pos.y
screen.fill((0,0,0))
screen.blit(image,rect)
pygame.display.flip()
clock.tick(60)
这可能是pygame2.3.0版本中的一个已知问题。pygame.key.get_pressed()在pygame2.3.0版本中的实现方式已经改变了,它现在使用了一个新的系统来处理多个键的输入。与之前版本不同,pygame.key.get_pressed()现在处理所有键的输入,包括控制键和非字母数字键,这可能会导致输入的字母键失效。
解决方案是使用pygame.event.get()获取按键事件,这将返回每个键按下和松开的事件,然后你可以检查哪个键被按下或松开。在你的代码中,你可以使用下面的代码来检查q键是否被按下:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
print('这边是重点')
这个代码段将处理所有按键事件,并检查是否按下了q键,如果是,它将打印出"This is important"。注意,这个代码段应该放在keys = pygame.key.get_pressed()之前,因为它处理每个按键事件并将其从事件队列中移除。 根据问题描述,似乎按下q键时上下左右都会失效。这可能是因为事件循环中的if语句嵌套顺序不当导致的。
可以尝试一下修改代码:
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys:
print('这边是重点')
else:# 将这里的else改为elif,防止上下左右键状态被q键语句覆盖
if keys:
direction.y = -1
elif keys:
direction.y = 1
else:
direction.y = 0
if keys:
direction.x = -1
elif keys:
direction.x = 1
else:
direction.x = 0
if direction.magnitude() > 0:
direction = direction.normalize()
pos.x += direction.x * speed
rect.centerx = pos.x
pos.y += direction.y * speed
rect.centery = pos.y
screen.fill((0,0,0))
screen.blit(image,rect)
pygame.display.flip()
clock.tick(60)
将按下q键的判断语句移到了开头,这样就不会影响到上下左右键的状态了。同时,根据上下左右键的状态来移动图像位置的代码也需要放到elif语句块中,避免与按下q键的语句冲突。
有用请设置最佳答案 使用pygame.key.set_mods(0) 可以解决这个问题。在检查按键之前添加这一行,它会禁用所有修饰符(如shift,ctrl等)的影响。在你的代码中,在 keys = pygame.key.get_pressed() 前添加 pygame.key.set_mods(0) :
import pygame,sys
pygame.init()
screen = pygame.display.set_mode((1280,720))
image = pygame.Surface((32,64))
image.fill('green')
rect = image.get_rect(center = (640,360))
direction = pygame.math.Vector2()
pos = pygame.math.Vector2(rect.center)
speed = 2
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.key.set_mods(0)# 添加这一行
keys = pygame.key.get_pressed()
if keys:
direction.y = -1
elif keys:
direction.y = 1
else:
direction.y = 0
if keys:
direction.x = -1
elif keys:
direction.x = 1
else:
direction.x = 0
if keys:
print('这边是重点')
if direction.magnitude() > 0:
direction = direction.normalize()
pos.x += direction.x * speed
rect.centerx = pos.x
pos.y += direction.y * speed
rect.centery = pos.y
screen.fill((0,0,0))
screen.blit(image,rect)
pygame.display.flip()
clock.tick(60)
现在当你按下 q 键时,上下左右键不会失效。这是因为在pygame 2.3.0中,默认的键盘修饰符行为已经发生了变化。将其设置为0可禁用这些修饰符,使其恢复到预期的行为。 陶远航 发表于 2023-5-2 18:03
这可能是pygame2.3.0版本中的一个已知问题。pygame.key.get_pressed()在pygame2.3.0版本中的实现方式已经改 ...
这样还是不行。删除下面的
if keys:
print('这边是重点')
添加
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
print('这边是重点')
还是一个样子的。按q之后方向键也不起作用了 sfqxx 发表于 2023-5-2 18:07
根据问题描述,似乎按下q键时上下左右都会失效。这可能是因为事件循环中的if语句嵌套顺序不当导致的。
...
不行,没有用 isdkz 发表于 2023-5-2 18:37
使用pygame.key.set_mods(0) 可以解决这个问题。在检查按键之前添加这一行,它会禁用所有修饰符(如shift, ...
不行。按过q后方向键盘 还是一样会失效。 wangqq157 发表于 2023-5-3 09:51
不行。按过q后方向键盘 还是一样会失效。
楼主,我昨天写的模拟钢琴的代码也是一样的问题,按数字键盘关联的音频文件可以播放,但是按字母键盘的就是不出声,还会中断数字键盘的音频输出,试了好多办法都没用,在室友的电脑上也跑了,都是一样的情况。请问你解决了吗?
vigilnet 发表于 2023-5-3 11:18
楼主,我昨天写的模拟钢琴的代码也是一样的问题,按数字键盘关联的音频文件可以播放,但是按字母键盘的就 ...
以上三个方法都上机测试了 都无法解决。官方文档给的说法是。。注意:使用此功能获取按钮列表不是处理用户输入文本的正确方法。无法知道按键的顺序,在对pygame.key.get_pressed()的两次调用之间,快速按下的按键可能会被完全忽略。也无法将这些按下的按键转换为完全转换的字符值。请参阅pygame.eventpygame模块上的pygame.KEYDOWN事件,了解与此功能的事件和队列的交互。
pygame 2.2.0中的新功能:get_pressed返回的布尔集合无法迭代,因为内部元组的索引与键代码不一致。
但是应该可以用其他方法解决。我用事件去解决也是不行的只要用了pygame.key.get_pressed 键盘就失效,但是游戏还是在跑
页:
[1]