簡體   English   中英

sys.stdout.write()和.flush()帶變量

[英]sys.stdout.write() and .flush() with variables

我一直在從事RPG游戲,我喜歡sys.stdout.write()和.flush()如何使其具有鍵入字母的效果。 但是,當我在要鍵入的句子中添加變量時,它不會鍵入句子的任何部分,甚至連引號中包含的部分(非變量)也沒有。 我想知道是否有一種解決方法,其中不包括我為變量以及引號中的內容編寫單獨的代碼塊,或者使它鍵入變量。

if girl1 == True:
   t="You and",girl1realname,"have found a sandy beach while looking for a 
   place of shelter. \nWhether you look left or right, the beach ranges for 
   miles.\n"
   y=girl1realname+": 'Hey, I guess I always wanted a house on the beach...I 
   think. I can't really remember.'\n"
   for char in (t):
       time.sleep(0.05)
       sys.stdout.write(char)
       sys.stdout.flush()
   time.sleep(1)
   for char in (y):
       time.sleep(0.05)
       sys.stdout.write(char)
       sys.stdout.flush()

t的定義中,逗號將使其成為一個元組。 然后進行迭代,應一次寫入每個塊( "You and"girl1realname"have found..." ),而不是每個塊中的每個char。 +替換逗號。

您對y定義似乎不錯。

至於重用,您可以定義一個函數,例如:

def typeout(x):
  for char in x:
    time.sleep(0.05)
    sys.stdout.write(char)
    sys.stdout.flush()

像這樣使用:

typeout(t)
time.sleep(1)
typeout(y)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM