简体   繁体   English

pygame键盘事件获取为整数

[英]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). K_1已经是整数(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: 因此,在您的情况下,如果要在播放器按下K_1时创建一个整数“ 1”,而在按下K_2时创建一个整数“ 2”,那么您要做的就是:

answer = event.key - 48

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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