【问题标题】:Continuous b'\xff'连续 b'\xff'
【发布时间】:2017-06-21 08:24:19
【问题描述】:

我想用 Python 3.6 制作游戏,但遇到了问题。当我尝试设置控制(如 WASD)时,Python Shell 检测到我不断按 ÿ(unicode:b'\xff')。我的键盘上没有 ÿ。我写道:print(getch())print(chr(ord(getch())))

我有两个问题:

  1. 我的问题有什么解决方案?
  2. 在使用 Python 制作的游戏中设置控件的最佳方法是什么?

提前谢谢你。

【问题讨论】:

  • 可能是EOF 返回码-10xFF,如果您尝试将其转换为字符),表示输入结束,您将其解释为 unicode 序列?这当然是一个控制信号,而不是实际的键盘输入。如果您正在使用 Curses,请查看此 stackoverflow.com/questions/4241366/getch-returns-1

标签: python python-3.x python-3.6


【解决方案1】:

我在使用 Spyder 中包含的 IPython 控制台时遇到了类似的问题。以下是我的建议:

首先,尝试使用 cmd 控制台检查问题是否仍然存在。

第二,getch()不等你按键,连续读取。如果您需要捕获一些特定的输入,您可能需要使用:

while True:
    if msvcrt.kbhit():
        ch = msvcrt.getch()
        print(ch)

第三,由于您有字节并且您可能想检查代码中的几个字符,我对 Windows 的建议是使用 getwch() 而不是 getch()。这是我的代码,它记录了字符被按下的时间:

import msvcrt, sys, datetime
while True:
    if msvcrt.kbhit():
        ch = msvcrt.getwch()
        if ch == 'q':
           sys.exit()
        else:
           print (ch, " Pressed at : ", datetime.datetime.now().time())

【讨论】:

    猜你喜欢
    • 2021-11-30
    • 2016-02-05
    • 1970-01-01
    • 2018-07-27
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    相关资源
    最近更新 更多