使ASCII艺术打印在同一行

时间:2015-09-05 16:49:56

标签: python python-3.x fonts io ascii

我正在制作一个程序,该程序采用带有ASCII艺术字母的文件并用它来打印文字。它需要3行输入宽度,高度和所需的单词。我的问题是我可以让字符在同一行上打印,然后像这样一个接一个地打印出来:

Height: 8
Width: 9
Text: TOP
_________
\__   __/
   ) (   
   | |   
   | |   
   | |   
   | |   
   )_(   
 _______ 
(  ___  )
| (   ) |
| |   | |
| |   | |
| |   | |
| (___) |
(_______)
 _______ 
(  ____ )
| (    )|
| (____)|
|  _____)
| (      
| )      
|/       

这是我的代码:

temp = []
hi = input('Height: ')
wi = input('Width: ')
tx = input('Text: ')
fi = open("font.txt")
for i in tx:
  temp = cd[i]
  var1 = int(temp[0])
  ra1 = (var1 * int(hi))
  ra1n = (ra1 + int(hi))
  temp = []
  fi = open("font.txt")
  lines = fi.readlines()
  print(''.join(lines[ra1:ra1n]),end='')

font.txt看起来像这样

 _______ 
(  ___  )
| (   ) |
| (___) |
|  ___  |
| (   ) |
| )   ( |
|/     \|
 ______  
(  ___ \ 
| (   ) )
| (__/ / 
|  __ (  
| (  \ \ 
| )___) )
|/ \___/ 
 _______ 
(  ____ \
| (    \/
| |      
| |      
| |      
| (____/\
(_______/
 ______  
(  __  \ 
| (  \  )
| |   ) |
| |   | |
| |   ) |
| (__/  )
(______/ 
 _______ 
(  ____ \
| (    \/
| (__    
|  __)   
| (      
| (____/\
(_______/
 _______ 
(  ____ \
| (    \/
| (__    
|  __)   
| (      
| )      
|/       
 _______ 
(  ____ \
| (    \/
| |      
| | ____ 
| | \_  )
| (___) |
(_______)

|\     /|
| )   ( |
| (___) |
|  ___  |
| (   ) |
| )   ( |
|/     \|
_________
\__   __/
   ) (   
   | |   
   | |   
   | |   
___) (___
\_______/
_________
\__    _/
   )  (  
   |  |  
   |  |  
   |  |  
|\_)  )  
(____/   
 _       
| \    /\
|  \  / /
|  (_/ / 
|   _ (  
|  ( \ \ 
|  /  \ \
|_/    \/
 _       
( \      
| (      
| |      
| |      
| |      
| (____/\
(_______/
 _______ 
(       )
| () () |
| || || |
| |(_)| |
| |   | |
| )   ( |
|/     \|
 _       
( (    /|
|  \  ( |
|   \ | |
| (\ \) |
| | \   |
| )  \  |
|/    )_)
 _______ 
(  ___  )
| (   ) |
| |   | |
| |   | |
| |   | |
| (___) |
(_______)
 _______ 
(  ____ )
| (    )|
| (____)|
|  _____)
| (      
| )      
|/       
 _______ 
(  ___  )
| (   ) |
| |   | |
| |   | |
| | /\| |
| (_\ \ |
(____\/_)
 _______ 
(  ____ )
| (    )|
| (____)|
|     __)
| (\ (   
| ) \ \__
|/   \__/
 _______ 
(  ____ \
| (    \/
| (_____ 
(_____  )
      ) |
/\____) |
\_______)
_________
\__   __/
   ) (   
   | |   
   | |   
   | |   
   | |   
   )_(   

|\     /|
| )   ( |
| |   | |
| |   | |
| |   | |
| (___) |
(_______)

|\     /|
| )   ( |
| |   | |
( (   ) )
 \ \_/ / 
  \   /  
   \_/   

|\     /|
| )   ( |
| | _ | |
| |( )| |
| || || |
| () () |
(_______)

|\     /|
( \   / )
 \ (_) / 
  ) _ (  
 / ( ) \ 
( /   \ )
|/     \|

|\     /|
( \   / )
 \ (_) / 
  \   /  
   ) (   
   | |   
   \_/   
 _______ 
/ ___   )
\/   )  |
    /   )
   /   / 
  /   /  
 /   (_/\
(_______/

2 个答案:

答案 0 :(得分:1)

如果您的字体已正确填充(所有行的长度相同),则可以使用表示行和字符矩阵的列表列表;只有在处理完文本的所有字母后才能将其汇总到输出中进行打印:

hi = int(hi)
output = [[] for _ in range(hi)]
with open("font.txt") as fi:
    lines = fi.readlines()

for character in tx:
    offset = int(cd[character])
    start = offset * hi
    end = start + hi
    letter = lines[start:end]
    for outputline, letterline in zip(output, letter):
        outputline.append(letterline.rstrip('\n'))

for line in output:
    print(''.join(line))

请注意,您需要删除字体文件中包含的换行符; str.rstrip()电话会照顾到这一点。

或者,您可以在阅读文件时删除所有换行符:

with open("font.txt") as fi:
    lines = fi.read().splitlines()

如果您的字体没有正确填充,则需要先获取任何给定字符的最大宽度,然后填充其余部分:

width = max(len(l) for l in lines[start:end])
letter = lines[start:end]
for outputline, letterline in zip(output, letter):
    outputline.append(letterline.rstrip('\n').ljust(width))

答案 1 :(得分:1)

刚才看到这个问题有点晚了。我碰巧做了类似的事情,所以这也可能有所帮助:

          _______  _        _        _______ 
|\     /|(  ____ \( \      ( \      (  ___  )
| )   ( || (    \/| (      | (      | (   ) |
| (___) || (__    | |      | |      | |   | |
|  ___  ||  __)   | |      | |      | |   | |
| (   ) || (      | |      | |      | |   | |
| )   ( || (____/\| (____/\| (____/\| (___) |
|/     \|(_______/(_______/(_______/(_______)
          _______  _______  _        ______  
|\     /|(  ___  )(  ____ )( \      (  __  \ 
| )   ( || (   ) || (    )|| (      | (  \  )
| | _ | || |   | || (____)|| |      | |   ) |
| |( )| || |   | ||     __)| |      | |   | |
| || || || |   | || (\ (   | |      | |   ) |
| () () || (___) || ) \ \__| (____/\| (__/  )
(_______)(_______)|/   \__/(_______/(______/

这将显示以下输出:

width

{{1}}参数也可用于添加其他填充。