The keyboard events (e.g. pygame.KEYDOWN
) occure only once when a button is pressed.
Use pygame.key.get_pressed()
to continuously evaluate the states of the buttons. e.g.:
finish = False
while not finish:
for event in pygame.event.get():
if event.type == pygame.QUIT:
finish = True
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
print "A"
if keys[pygame.K_RIGHT]:
print "B"
if keys[pygame.K_LEFT]:
print "C"
Or if you would rather get a list:
finish = False
while not finish:
for event in pygame.event.get():
if event.type == pygame.QUIT:
finish = True
keys = pygame.key.get_pressed()
if any(keys):
kmap = {pygame.K_UP : "A", pygame.K_RIGHT : "B", pygame.K_LEFT : "C"}
sl = [kmap[key] for key in kmap if keys[key]]
print sl
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…