I want the user to press on any letter of the alphabet; it should block the input from the rest of the keys. How can I do this using pygame?
Keys=pygame.key.get_pressed()
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.
Sign up to join this communityIt's not exactly 'only allowing letters', but more 'only responding to letters'.
I'm not familiar with pygame, and my python is rusty, but according to the documentation, you could do something like this:
press=pygame.key.get_pressed()
for i in xrange( pygame.K_a, pygame.K_z + 1 ):
if press[i] == True:
name=pygame.key.name(i)
# do something with the key
However, I must emphasize the documentation of get_keypressed
:
Getting the list of pushed buttons with this function is not the proper way to handle text entry from the user. You have no way to know the order of keys pressed, and rapidly pushed keys can be completely unnoticed between two calls to pygame.key.get_pressed(). There is also no way to translate these pushed keys into a fully translated character value. See the pygame.KEYDOWN events on the event queue for this functionality.
Depending on what you exactly intend to do with these alphabet letters, you might want to use the event route instead.
if x == True
is less nice than just if x
, similarly if x == False
should be if not x
\$\endgroup\$
Mar 21, 2017 at 22:42