我正在制作游戏的基础,对于介绍,我需要它像打字机一样打印出来。当我将其称为“多个”时,它会打印出整个对象,而不是像打字机那样慢慢打印出来
import random
import sys
from time import sleep
test_list = ['obj 1', 'obj 2']
def writer(text, multi = True):
if multi == True:
obj = 0
for item in text:
sleep(1)
sys.stdout.write(text[obj])
obj += 1
sys.stdout.flush()
else:
for char in text:
sleep(0.05)
sys.stdout.write(char)
sys.stdout.flush()
writer(test_list)
代码:
import random
import sys
from time import sleep
test_list = ['obj 1', 'obj 2']
def writer(text, multi=True):
if multi:
for item in text:
for char in item:
sleep(0.5)
sys.stdout.write(char)
sys.stdout.flush()
sys.stdout.write('\n')
else:
for char in text:
sleep(1)
sys.stdout.write(char)
sys.stdout.flush()
sys.stdout.write('\n')
writer(test_list)
你错过了字符串中每个符号的内部 -
for char in item:
import random
import sys
from time import sleep
test_lists = ['obj 1', 'obj 2']
def writer(text, multi = True):
if multi == True:
obj = 0
for item in text:
for char in item:
sys.stdout.write(char)
obj += 1
sys.stdout.flush()
sleep(1)
else:
for char in text:
sleep(0.05)
sys.stdout.write(char)
sys.stdout.flush()
writer(test_lists)