同时打印到多行

2024-04-24 06:01:42 发布

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

我正在做一个基于文本的游戏,我试图打印这个代码一个字符在每一列的时间。你知道吗

''''.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'

让我们开始。。。你知道吗

ŧŧŧŧŧŧŧŧŧŧŧŧŧŧ

我想不出怎么一列一列地写出来。你知道吗


Tags: 代码文本游戏时间字符
2条回答

嗯,尽管你的问题含糊不清,我还是想回答这个问题。也许这就是您要找的,它一次打印一列(每行一个字符):

import subprocess
import platform
from time import sleep


def clear_screen():
    # thanks to: https://stackoverflow.com/a/23075152/2923937
    if platform.system() == "Windows":
        subprocess.Popen("cls", shell=True).communicate()
    else:
        print("\033c", end="")


# obviously you can create a function to convert your string into this
# list rather than doing it manually like I did, but that is another question :p.
views = ['#\nh\n#', '##\nhe\n##', '###\nhel\n###', '####\nhell\n####', '#####\nhello\n#####']

for view in views:
    clear_screen()
    print(view)
    sleep(0.5)

如果已经对字符串中的每个字符执行了print(c, end=''),只需将flush=True添加到对print()的调用中。休眠调用将引入足够的延迟,以便您可以看到一次打印一个字符:

>>> import time
>>> s = '''###############
... Let us begin...
... ###############'''
>>> for c in s:
...    print(c, end='', flush=True)
...    time.sleep(0.1)

相关问题 更多 >