Pygame 按键事件仅检测有限数量的按键被按下
Pygame Key events only detects a limited amount of keys being held down
您好,我已经使用 pygame(python 的模块)有一段时间了。现在,我编写了一个同时按下多个键的 RPG 游戏。按住时似乎只检测到 2 或 3 个键。如果有人知道如何解决这个问题,那就太好了。试试我下面的 python 2.7 代码,看看你是否有同样的问题。谢谢
import pygame
def main():
# Initialise screen
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((150, 50))
pygame.display.set_caption('Basic Pygame program')
# Fill background
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))
# Display some text
font = pygame.font.Font(None, 36)
text = font.render("Hello There", 1, (10, 10, 10))
textpos = text.get_rect()
textpos.centerx = background.get_rect().centerx
background.blit(text, textpos)
# Blit everything to the screen
screen.blit(background, (0, 0))
pygame.display.flip()
q=0
w=0
e=0
r=0
#Event loop
while True:
for event in pygame.event.get():
if event.type == QUIT:
return
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q :
q = 1
if event.key == pygame.K_w :
w = 1
if event.key == pygame.K_e :
e = 1
if event.key == pygame.K_r :
r = 1
if event.type == pygame.KEYUP:
if event.key == pygame.K_q :
q = 0
if event.key == pygame.K_w :
w = 0
if event.key == pygame.K_e :
e = 0
if event.key == pygame.K_r :
r = 0
count = q+w+e+r
print("Total: "+str(count)+" q: "+str(q) + " w: "+str(w)+ " e: "+str(e)+ " r: "+str(r))
clock.tick(30)
screen.blit(background, (0, 0))
pygame.display.flip()
if __name__ == '__main__': main()
我在这里尝试使用 pygame.key.get_pressed() 但它似乎仍然无法在按下超过 3 个键的情况下工作。 )-:
from pygame.locals import *
import pygame
def main():
# Initialise screen
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((150, 50))
pygame.display.set_caption('Basic Pygame program')
# Fill background
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))
# Display some text
font = pygame.font.Font(None, 36)
text = font.render("Hello There", 1, (10, 10, 10))
textpos = text.get_rect()
textpos.centerx = background.get_rect().centerx
background.blit(text, textpos)
# Blit everything to the screen
screen.blit(background, (0, 0))
pygame.display.flip()
#Event loop
while True:
for event in pygame.event.get():
if event.type == QUIT:
return
q=0
w=0
e=0
r=0
keys=pygame.key.get_pressed()
if keys[K_q] :
q = 1
if keys[K_w] :
w = 1
if keys[K_e] :
e = 1
if keys[K_r] :
r = 1
count = q+w+e+r
print("Total: "+str(count)+" q: "+str(q) + " w: "+str(w)+ " e: "+str(e)+ " r: "+str(r))
clock.tick(30)
screen.blit(background, (0, 0))
pygame.display.flip()
if __name__ == '__main__': main(
)
您可以使用 'get_pressed',这将为您提供键盘上每个键的布尔值。
http://www.pygame.org/docs/ref/key.html#pygame.key.get_pressed
keys = pygame.key.get_pressed()
if keys[K_LEFT]:
go_left()
这很可能是 a hardware issue with your keyboard,不是您可以在游戏软件中解决的问题。大多数键盘对同时按下时可以读取的键数有限制(尽管 Shift 和 Control 等常用修饰键通常是分开处理的)。在笔记本电脑或低端台式机键盘上,一次仅支持两个普通(非修饰符)按键的情况并不少见。游戏键盘(以及一般的高端键盘)将支持更多,但通常仍有一些限制。如果您按下的键多于键盘可以处理的数量,它将忽略后面的按下(称为 "jamming")或导致键盘报告其他键的额外按键(称为 "ghosting")。
如果您自己设计游戏,这可能是需要注意的重要事项,因为它会影响您的玩家,而不仅仅是您!您可能想确保您设计的游戏界面不会让低端键盘的用户无法有效地玩游戏。如果您的 UI 在不同时按下多个键的情况下无法工作,您可能希望将一些键分配移动到修饰键,例如 Shift 和 Control,它们在同时按下时更有可能得到支持时间和其他键一样。随着第一人称射击游戏的盛行,WASD键也可能在某些键盘上得到特殊处理。
您好,我已经使用 pygame(python 的模块)有一段时间了。现在,我编写了一个同时按下多个键的 RPG 游戏。按住时似乎只检测到 2 或 3 个键。如果有人知道如何解决这个问题,那就太好了。试试我下面的 python 2.7 代码,看看你是否有同样的问题。谢谢
import pygame
def main():
# Initialise screen
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((150, 50))
pygame.display.set_caption('Basic Pygame program')
# Fill background
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))
# Display some text
font = pygame.font.Font(None, 36)
text = font.render("Hello There", 1, (10, 10, 10))
textpos = text.get_rect()
textpos.centerx = background.get_rect().centerx
background.blit(text, textpos)
# Blit everything to the screen
screen.blit(background, (0, 0))
pygame.display.flip()
q=0
w=0
e=0
r=0
#Event loop
while True:
for event in pygame.event.get():
if event.type == QUIT:
return
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q :
q = 1
if event.key == pygame.K_w :
w = 1
if event.key == pygame.K_e :
e = 1
if event.key == pygame.K_r :
r = 1
if event.type == pygame.KEYUP:
if event.key == pygame.K_q :
q = 0
if event.key == pygame.K_w :
w = 0
if event.key == pygame.K_e :
e = 0
if event.key == pygame.K_r :
r = 0
count = q+w+e+r
print("Total: "+str(count)+" q: "+str(q) + " w: "+str(w)+ " e: "+str(e)+ " r: "+str(r))
clock.tick(30)
screen.blit(background, (0, 0))
pygame.display.flip()
if __name__ == '__main__': main()
我在这里尝试使用 pygame.key.get_pressed() 但它似乎仍然无法在按下超过 3 个键的情况下工作。 )-:
from pygame.locals import *
import pygame
def main():
# Initialise screen
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((150, 50))
pygame.display.set_caption('Basic Pygame program')
# Fill background
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))
# Display some text
font = pygame.font.Font(None, 36)
text = font.render("Hello There", 1, (10, 10, 10))
textpos = text.get_rect()
textpos.centerx = background.get_rect().centerx
background.blit(text, textpos)
# Blit everything to the screen
screen.blit(background, (0, 0))
pygame.display.flip()
#Event loop
while True:
for event in pygame.event.get():
if event.type == QUIT:
return
q=0
w=0
e=0
r=0
keys=pygame.key.get_pressed()
if keys[K_q] :
q = 1
if keys[K_w] :
w = 1
if keys[K_e] :
e = 1
if keys[K_r] :
r = 1
count = q+w+e+r
print("Total: "+str(count)+" q: "+str(q) + " w: "+str(w)+ " e: "+str(e)+ " r: "+str(r))
clock.tick(30)
screen.blit(background, (0, 0))
pygame.display.flip()
if __name__ == '__main__': main(
)
您可以使用 'get_pressed',这将为您提供键盘上每个键的布尔值。
http://www.pygame.org/docs/ref/key.html#pygame.key.get_pressed
keys = pygame.key.get_pressed()
if keys[K_LEFT]:
go_left()
这很可能是 a hardware issue with your keyboard,不是您可以在游戏软件中解决的问题。大多数键盘对同时按下时可以读取的键数有限制(尽管 Shift 和 Control 等常用修饰键通常是分开处理的)。在笔记本电脑或低端台式机键盘上,一次仅支持两个普通(非修饰符)按键的情况并不少见。游戏键盘(以及一般的高端键盘)将支持更多,但通常仍有一些限制。如果您按下的键多于键盘可以处理的数量,它将忽略后面的按下(称为 "jamming")或导致键盘报告其他键的额外按键(称为 "ghosting")。
如果您自己设计游戏,这可能是需要注意的重要事项,因为它会影响您的玩家,而不仅仅是您!您可能想确保您设计的游戏界面不会让低端键盘的用户无法有效地玩游戏。如果您的 UI 在不同时按下多个键的情况下无法工作,您可能希望将一些键分配移动到修饰键,例如 Shift 和 Control,它们在同时按下时更有可能得到支持时间和其他键一样。随着第一人称射击游戏的盛行,WASD键也可能在某些键盘上得到特殊处理。