user5303899
user5303899

Reputation:

Making ASCII art print on the same line

I'm making a program which takes a file with an ASCII art alphabet and uses it to print words. It takes 3 lines of input The width, The Height and The desired word. My problem is that I can make the characters print on the same line, they print one after the other like so:

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

This is my code:

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 looks like this

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

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

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

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

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

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

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

Upvotes: 2

Views: 2159

Answers (3)

Python_Learner
Python_Learner

Reputation: 1659

There is a better answer, Pyfiglet

This:

import pyfiglet

ascii_banner = pyfiglet.figlet_format("Hello!!")
print(ascii_banner)

Produces this:

 _   _      _ _       _ _
| | | | ___| | | ___ | | |
| |_| |/ _ \ | |/ _ \| | |
|  _  |  __/ | | (_) |_|_|
|_| |_|\___|_|_|\___/(_|_)

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1124748

If your font has been properly padded (all lines are the same length) then you can use a list of lists that represent a matrix of lines and characters; you only assemble this into output to print after processing all letters of your text:

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))

Note that you need to remove the newlines included in the font file; the str.rstrip() call takes care of that.

Alternatively, you can remove all the newlines when reading the file:

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

If your font is not properly padded, you need to get the maximum width of any given character first and pad out the rest of the lines:

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))

Upvotes: 1

Martin Evans
Martin Evans

Reputation: 46779

Just saw this question a bit late. I happened to have done something similar so this might also be helpful:

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")

This would display the following output:

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

The width parameter can also be used to add additional padding.

Upvotes: 1

Related Questions