提问人: 提问时间:9/6/2015 更新时间:11/15/2023 访问量:2043
在同一行上打印 ASCII 艺术
Making ASCII art print on the same line
问:
我正在制作一个程序,它获取带有 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看起来像这样
_______
( ___ )
| ( ) |
| (___) |
| ___ |
| ( ) |
| ) ( |
|/ \|
______
( ___ \
| ( ) )
| (__/ /
| __ (
| ( \ \
| )___) )
|/ \___/
_______
( ____ \
| ( \/
| |
| |
| |
| (____/\
(_______/
______
( __ \
| ( \ )
| | ) |
| | | |
| | ) |
| (__/ )
(______/
_______
( ____ \
| ( \/
| (__
| __)
| (
| (____/\
(_______/
_______
( ____ \
| ( \/
| (__
| __)
| (
| )
|/
_______
( ____ \
| ( \/
| |
| | ____
| | \_ )
| (___) |
(_______)
|\ /|
| ) ( |
| (___) |
| ___ |
| ( ) |
| ) ( |
|/ \|
_________
\__ __/
) (
| |
| |
| |
___) (___
\_______/
_________
\__ _/
) (
| |
| |
| |
|\_) )
(____/
_
| \ /\
| \ / /
| (_/ /
| _ (
| ( \ \
| / \ \
|_/ \/
_
( \
| (
| |
| |
| |
| (____/\
(_______/
_______
( )
| () () |
| || || |
| |(_)| |
| | | |
| ) ( |
|/ \|
_
( ( /|
| \ ( |
| \ | |
| (\ \) |
| | \ |
| ) \ |
|/ )_)
_______
( ___ )
| ( ) |
| | | |
| | | |
| | | |
| (___) |
(_______)
_______
( ____ )
| ( )|
| (____)|
| _____)
| (
| )
|/
_______
( ___ )
| ( ) |
| | | |
| | | |
| | /\| |
| (_\ \ |
(____\/_)
_______
( ____ )
| ( )|
| (____)|
| __)
| (\ (
| ) \ \__
|/ \__/
_______
( ____ \
| ( \/
| (_____
(_____ )
) |
/\____) |
\_______)
_________
\__ __/
) (
| |
| |
| |
| |
)_(
|\ /|
| ) ( |
| | | |
| | | |
| | | |
| (___) |
(_______)
|\ /|
| ) ( |
| | | |
( ( ) )
\ \_/ /
\ /
\_/
|\ /|
| ) ( |
| | _ | |
| |( )| |
| || || |
| () () |
(_______)
|\ /|
( \ / )
\ (_) /
) _ (
/ ( ) \
( / \ )
|/ \|
|\ /|
( \ / )
\ (_) /
\ /
) (
| |
\_/
_______
/ ___ )
\/ ) |
/ )
/ /
/ /
/ (_/\
(_______/
答:
1赞
Martijn Pieters
9/6/2015
#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))
评论
0赞
9/6/2015
♦ 这与我尝试的类似,但我不确定这里发生了什么.然而,我尝试了这个,结果与我所做的另一次尝试几乎相同:线条一个接一个地打印,而不是彼此相邻。for outputline, letterline in zip(output, letter): outputline.append(letterline)
0赞
Martijn Pieters
9/6/2015
@user5303899:将列表中的列表与字体中的每一行配对。所以字体字母的第一行,第一个列表在,然后是第二个,依此类推。output
output
1赞
Martijn Pieters
9/6/2015
@user5303899:完成;该调用将删除字体文件中包含的换行符。str.rstrip()
1赞
Martijn Pieters
9/6/2015
@user5303899:我从通话中删除了空格。您的字体在文件中不包含空格“字符”吗?然后,您可以生成一个列表,例如,然后在每次需要插入空格时使用它而不是字符。''.join()
space = [' ' * int(wi) for _ in range(hi)]
lines
1赞
Martijn Pieters
9/6/2015
@user5303899:你可以在 Python 中这样做;更改列表,替换表示空格的部分:。lines
lines[spacestart:spaceend] = [' ' * width for _ in range(height)]
1赞
Martin Evans
9/6/2015
#2
只是看到这个问题有点晚了。我碰巧做了类似的事情,所以这也可能有帮助:
import itertools
def load_font(font_filename, width, height):
with open(font_filename, 'r') as figlet:
return [map(lambda x: x.rstrip('\n').ljust(width+1), character) for character in iter(lambda: list(itertools.islice(figlet, height)), [])]
def figlet(font, text):
for line in zip(*[font[ord(letter)-65] for letter in text]):
print "".join(line)
font = load_font('file.txt', 8, 8)
figlet(font, "HELLO")
figlet(font, "WORLD")
这将显示以下输出:
_______ _ _ _______
|\ /|( ____ \( \ ( \ ( ___ )
| ) ( || ( \/| ( | ( | ( ) |
| (___) || (__ | | | | | | | |
| ___ || __) | | | | | | | |
| ( ) || ( | | | | | | | |
| ) ( || (____/\| (____/\| (____/\| (___) |
|/ \|(_______/(_______/(_______/(_______)
_______ _______ _ ______
|\ /|( ___ )( ____ )( \ ( __ \
| ) ( || ( ) || ( )|| ( | ( \ )
| | _ | || | | || (____)|| | | | ) |
| |( )| || | | || __)| | | | | |
| || || || | | || (\ ( | | | | ) |
| () () || (___) || ) \ \__| (____/\| (__/ )
(_______)(_______)|/ \__/(_______/(______/
该参数还可用于添加其他填充。width
0赞
Programming_Learner_DK
11/15/2023
#3
有一个更好的答案,Pyfiglet
这:
import pyfiglet
ascii_banner = pyfiglet.figlet_format("Hello!!")
print(ascii_banner)
生成以下内容:
_ _ _ _ _ _
| | | | ___| | | ___ | | |
| |_| |/ _ \ | |/ _ \| | |
| _ | __/ | | (_) |_|_|
|_| |_|\___|_|_|\___/(_|_)
下一个:在同一行上打印 ASCII 艺术
评论
font.txt
cd[i]