下面是我想要实现的相关代码片段(start()中的f-字符串)。我为变量分配了函数参数的值,该参数允许解释器在调用脚本时暂停运行脚本。这部分就是关于这个的。(虽然不起作用)
def pause_here(delay=0):
time.sleep(delay)
quartsec_pause = pause_here(.25)
halfsec_pause = pause_here(.5)
sec_pause = pause_here(1)
sec2_pause = pause_here(2)这是我的文字冒险开始,主屏幕。在这里,我试图在这里使用这个f-字符串实现这个特性:
def start():
slow_print(f"""_
It's Monday, 9:33pm, August 4th, 2024.{halfsec_pause}
You lay in the back of an old truck headed down what now feels
like an endless dirt road.{quartsec_pause} The sound of the truck clunking
down the road enters your consciousness.{halfsec_pause}
Just as quickly,{quartsec_pause} the spatter of the engine reduces to a
complete silence.{quartsec_pause} Your eyes open for the first time in well,
that's the thing...{sec_pause}
You can't remember the last time you were awake.{halfsec_pause}
Enter (1) to observe your surroundings.{quartsec_pause}
Enter (2) to scream your lungs out.{quartsec_pause}
Enter (3) to climb out of the truck.{quartsec_pause}
Enter (4) to hear what's going on around you.
_""", 3)下面是我处理所有用户输入内容的地方。与手头的问题不太相关,所以这可以被忽略。我只想展示一下,万一有人需要帮助的话,我想要做的是全面了解我想要创造的东西。
choice = input("--> ")
reset_console()
if "1" in choice:
start_observe()
elif "2" in choice:
start_scream()
elif "3" in choice:
start_climb()
elif "4" in choice:
start_hear()
else:
start()我很感激我在这个问题上得到的任何解决方案,如果我能以某种方式实现它,让它在运行时看起来非常漂亮,那就太好了!
更新:
def start():
for i in [.5, .25, .5, .25, .25, 1, .5, .25, .25, .25]:
time.sleep(i)
slow_print(f"""_
It's Monday, 9:33pm, August 4th, 2024.{i}
You lay in the back of an old truck headed down what now feels
like an endless dirt road.{i} The sound of the truck clunking
down the road enters your consciousness.{i}
Just as quickly,{i} the spatter of the engine reduces to a
complete silence.{i} Your eyes open for the first time in well,
that's the thing...{i}
You can't remember the last time you were awake.{i}
Enter (1) to observe your surroundings.{i}
Enter (2) to scream your lungs out.{i}
Enter (3) to climb out of the truck.{i}
Enter (4) to hear what's going on around you.
_""", 3)在这里等着发这个问题。我试过上面的方法,也没有用。
发布于 2022-04-30 20:45:47
解决此问题的一种方法是将打印操作与暂停操作分开。在下面的示例中,我创建了一个元组列表,其中包括要打印的文本以及要在打印操作后插入的暂停。函数解压缩这些元组并使用第一个值(文本)打印,使用第二个值(暂停)休眠:
import time
pause_text_list = [
("It's Monday, 9:33pm, August 4th, 2024.", .5),
("You lay in the back of an old truck headed down what now feels like an endless dirt road.", .25),
("The sound of the truck clunking down the road enters your consciousness.", .5),
("Just as quickly,", .25),
("the spatter of the engine reduces to a complete silence.", .25),
("Your eyes open for the first time in well, that's the thing...", 1.0),
("You can't remember the last time you were awake.", .5),
("Enter (1) to observe your surroundings.", .25),
("Enter (2) to scream your lungs out.", .25),
("Enter (3) to climb out of the truck.", .25),
("Enter (4) to hear what's going on around you.", .25),
]
def print_pause_text(pause_text_list):
for text, pause in pause_text_list:
print(text)
time.sleep(pause)
print_pause_text(pause_text_list)It's Monday, 9:33pm, August 4th, 2024.
You lay in the back of an old truck headed down what now feels like an endless dirt road.
The sound of the truck clunking down the road enters your consciousness.
Just as quickly,
the spatter of the engine reduces to a complete silence.
Your eyes open for the first time in well, that's the thing...
You can't remember the last time you were awake.
Enter (1) to observe your surroundings.
Enter (2) to scream your lungs out.
Enter (3) to climb out of the truck.
Enter (4) to hear what's going on around you.https://stackoverflow.com/questions/72069723
复制相似问题