如何在python程序中检测“space”?

2024-06-25 06:43:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个基于文本的游戏,为了加快过去的对话,我希望用户能够击中'空间',跳过它。你知道吗

import time
import sys

text_speed = .05

def slow_type(line, speed): #You input the dialogue and speed(smaller = faster)
    for l in line:
        sys.stdout.write(l)
        sys.stdout.flush()
        time.sleep(speed)
    time.sleep(.5)

if <'Space'> pressed:
    text_speed = 0

NL1 = "Huh, I see you finally came. "

slow_type(NL1, text_speed)

Tags: text用户文本import游戏timetypestdout
3条回答

我建议你使用keyboard。你知道吗

下面是一个检测空格的示例代码,还请注意该代码将等待空格键被按下。你知道吗

import keyboard

#.....

if keyboard.is_pressed("space"):
    text_speed = 0

#....

空间的Python表示是u"\u0020",称为here。你知道吗

测试方法:

if input('enter:') == u"\u0020":
    print('pass')
else:
    print('fail')

您可以使用^{}模块。你知道吗

import getch
while getch.getch() != ' ':
    pass

或者在Windows上,可以使用msvcr.getch

import msvcrt
while msvcrt.getch() != ' ':
    pass

相关问题 更多 >