简体   繁体   中英

getting pygame keyboard events as integers

I am making a math game and I want the user to be able to enter the answer. Do I have to do something like this for each number.

for event in pygame.event.get():
    blahblahblah
    quit event
    if event.key == K_1:
        str(answer + 1)

Is there a way to get keyboard events and if they are numbers turn them into integers. Perhaps a build in method?

K_1 is already an integer (49). They are just constants of integers representing a specific key. So in your case, if you want to create an integer '1' when the player presses K_1 and '2' when the player presses K_2, all you have to do is:

answer = event.key - 48

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM