在Python中慢速打印但没有截断单词

时间:2017-06-10 16:02:22

标签: python

我正在编写一个文本冒险,以获得Python的一些乐趣,并找到了一个很酷的函数,可以将文本缓慢打印到终端:

def print_slow(str):
for letter in str:
    sys.stdout.write(letter)
    sys.stdout.flush()
    time.sleep(0.1)

然而,正如它在字母末尾之前没有完成的那样逐个字母地写它,它会截断它,即:

bla bla bla bla rea
ding.

而我宁愿这样做:

bla bla bla bla
reading

这样做的简单方法是什么?

完整代码:请忽略这个坏故事,这只是一个概念证据,我可以让它发挥作用! :)

from __future__ import print_function
import textwrap
import sys,time,random
import pygame
from pygame.locals import *


def print_slow(str):
    for letter in str:
        sys.stdout.write(letter)
        sys.stdout.flush()
        time.sleep(0.1)


def displayIntro():
    pygame.mixer.init()
    pygame.mixer.music.load('storm.wav')
    pygame.mixer.music.play()
    print('')
    print('')

    for line in textwrap.wrap(a,20):
        print_slow('The year is 2054, you have just arrived home from four years of travelling ')
        print()
    print_slow('The year is 2054, you have just arrived home from four years of travelling ')
    print_slow('Europe, studying the virus that has killed all the grass in the mainland. ')
    time.sleep(1)
    print('\n')
    pygame.mixer.music.load('metaldoor.wav')
    pygame.mixer.music.play(0)
    print_slow('As you open the gates of your estate, you hear a familar voice coming from ')
    print_slow('the house. ')
    time.sleep(1)

def gateDecision():
    decision = ''
    print('\n') 
    print_slow('As you walk down the drive, the voice suddenly turns into a scream. ')
    time.sleep(1)
    print('\n')
    print_slow('You start running, getting to the door which is locked. ')
    time.sleep(1)
    print('\n')
    print_slow('You reach into your pocket to get the key, but it is locked from the inside. ')
    time.sleep(1)
    print('\n')

    while decision != 'a' and decision != 'b':
    print_slow('Do you: ')
    print('\n')
        print_slow('(a) Climb in through the open window, or (b) Go around and try the back door?')
    decision = raw_input()

    if decision == "a":
        time.sleep(1)
        print('')
        print_slow('As you open the window, you feel the shotgun against your forehead and the ')
        pygame.mixer.music.load('shotgun.wav')
        pygame.mixer.music.play(0)
        print_slow('cool breeze of air that rushes through your skull as you drop to the ')
    print_slow('ground dead. ')
    print('\n')
    print_slow('THE END... ')
    print('\n')
    print('\n')
    else:
        time.sleep(1)
        print_slow('You get to the back door to find it ajar, you slowly step in... ')
print('-------------------------------------------------------------')        
print('\n')
print('\n')
print('\n')
print('\n')
print('\n')
print('\n')
print('\n')
print(' #    # # #####  #    #  ####  ')
print(' #    # # #    # #    # #      ')
print(' #    # # #    # #    #  ####  ')
print(' #    # # #####  #    #      # ')
print('  #  #  # #   #  #    # #    # ')
print('   ##   # #    #  ####   ####  ')

displayIntro()
gateDecision()              

1 个答案:

答案 0 :(得分:4)

使用textwrap将文字拆分为多行。调用print_slow()输出每一行,然后打印换行符。

from __future__ import print_function
import textwrap
for line in textwrap.wrap(a,20):
    print_slow(line)
    print()