嗨我已经使用pygame(模块为python)一段时间。现在我已经写了一个RPG游戏,有多个键一次被按下。似乎只有2或3个按键被按下。如果有人知道如何解决这个问题,那就太好了。试试我的代码下面的python 2.7,看看你是否有同样的问题。由于pygame键盘事件只能检测到有限的键被按下
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()
在这里,我tryed与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(
)
这似乎是这种情况。我只在我的笔记本电脑和其他一些老电脑上运行游戏。只要我尝试了我的游戏键盘,它就可以完美运行。如果你知道解决这个问题的任何方法(即允许更老的键盘获得更多的输入),那将是非常棒的。谢谢( - : – Samuel
我怀疑是否有直接的解决方法,因为问题与其内部设计有关,如果您的游戏支持关键重新映射,您的用户可能能够通过重新绑定某些操作来解决某些问题可能冲突更少的修改键,但这可能是您可以做的最多(除了修改游戏设计以便不需要同时进行多次按键操作)。 – Blckknght