【问题标题】:Trouble in detecting key presses in python using getch使用getch在python中检测按键时遇到问题
【发布时间】:2017-06-21 09:42:27
【问题描述】:

我是 python 新手,我正在尝试制作一个控制台游戏。为了检测按键,我使用了 getch (https://github.com/joeyespo/py-getch)。但是当我按下 a 时,代码开始重复。

key = getch()
while (True):
    if (key == 'a'):
        principal.adicionaragua()
        principal.gastaragua()
        principal.aumentardias()
        principal.estado()
        time.sleep(2)
        clear()

编辑:我使用的是 windows 和 python 2.7

【问题讨论】:

    标签: python getch


    【解决方案1】:

    您需要在循环中获取key。否则,它将始终为 'a',因为您没有检查循环内部。

    while (True):
        key = getch()
        if (key == 'a'):
            principal.adicionaragua()
            principal.gastaragua()
            principal.aumentardias()
            principal.estado()
            time.sleep(2)
            clear()
    

    【讨论】:

    • 祝你有美好的一天:D!
    • 乐于助人,祝您有美好的一天! :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多