Reputation: 144
I'm coding a very small and basic text based adventure game in python, and I want to be able to align my title (which is in ASCII) I've looked it up but I either didn't understand it or it didn't work.
Here is the ASCII:
________ ___
/_ __/ /_ ___ / | ________ ____ ____ _
/ / / __ \/ _ \ / /| | / ___/ _ \/ __ \/ __ `/
/ / / / / / __/ / ___ |/ / / __/ / / / /_/ /
/_/ /_/ /_/\___/ /_/ |_/_/ \___/_/ /_/\__,_/
(It's not really creative, I know)
I want to get it to align to the center, is that possible?
Upvotes: 1
Views: 2170
Reputation: 543
Don't type ASCII art by yourself - use text2art:
from art import *
text2art("The Arena")
The alternative for this is:
from art import *
tprint("The Arena")
To center this, do the following:
from art import *
string="The Arena"
new_string = string.center(90)
tprint(new_string)
or (using text2art
instead):
text2art(new_string)
Upvotes: 0
Reputation: 436
I would:
get the size of terminal window (columns mainly) - linux command: "stty -a", windows command: "mode con" - and parse the cli output, or do it like in here: How to get Linux console window width in Python
get the max size of the text (the line with most columns needed)
EDIT
Here's an example (Works under Linux and Windows):
import shutil
import math
def str_repeat(string, length):
return (string * (int(length / len(string)) + 1))[:length]
def print_center(lines_out):
columns, rows = shutil.get_terminal_size()
max_line_size = 0
left_padding = 0
for line in lines_out:
if max_line_size == 0 or max_line_size < len(line):
max_line_size = len(line)
if columns > max_line_size:
left_padding = math.floor((columns - max_line_size) / 2)
if left_padding > 0:
for line in lines_out:
print(str_repeat(' ', left_padding) + line)
lines = [
' ________ ___',
' /_ __/ /_ ___ / | ________ ____ ____ _',
' / / / __ \/ _ \ / /| | / ___/ _ \/ __ \/ __ `/',
' / / / / / / __/ / ___ |/ / / __/ / / / /_/ /',
'/_/ /_/ /_/\___/ /_/ |_/_/ \___/_/ /_/\__,_/'
]
print_center(lines)
Output:
________ ___
/_ __/ /_ ___ / | ________ ____ ____ _
/ / / __ \/ _ \ / /| | / ___/ _ \/ __ \/ __ `/
/ / / / / / __/ / ___ |/ / / __/ / / / /_/ /
/_/ /_/ /_/\___/ /_/ |_/_/ \___/_/ /_/\__,_/
Process finished with exit code 0
Upvotes: 1
Reputation: 106543
You can use the str.center
method. The code below assumes a screen width of 80 characters:
s = ''' ________ ___
/_ __/ /_ ___ / | ________ ____ ____ _
/ / / __ \/ _ \ / /| | / ___/ _ \/ __ \/ __ `/
/ / / / / / __/ / ___ |/ / / __/ / / / /_/ /
/_/ /_/ /_/\___/ /_/ |_/_/ \___/_/ /_/\__,_/ '''
print('\n'.join(l.center(80) for l in s.splitlines()))
This outputs:
________ ___
/_ __/ /_ ___ / | ________ ____ ____ _
/ / / __ \/ _ \ / /| | / ___/ _ \/ __ \/ __ `/
/ / / / / / __/ / ___ |/ / / __/ / / / /_/ /
/_/ /_/ /_/\___/ /_/ |_/_/ \___/_/ /_/\__,_/
Upvotes: 3
Reputation: 36662
You could, line by line, add the number of white space needed to center your text.
here is one approach, where you must provide the text to be centered, and the width available:
def align_center(text, width):
lext = text.split('\n')
for line in lext:
assert len(line) < width, 'insufficient width'
max_w = max(len(line) for line in lext)
res = []
for line in lext:
res.append(' ' * ((width - max_w) // 2))
res.append(line)
res.append('\n')
return ''.join(res)
text = """ ________ ___
/_ __/ /_ ___ / | ________ ____ ____ _
/ / / __ \/ _ \ / /| | / ___/ _ \/ __ \/ __ `/
/ / / / / / __/ / ___ |/ / / __/ / / / /_/ /
/_/ /_/ /_/\___/ /_/ |_/_/ \___/_/ /_/\__,_/ """
centered = align_center(text, 90)
print(centered)
________ ___
/_ __/ /_ ___ / | ________ ____ ____ _
/ / / __ \/ _ \ / /| | / ___/ _ \/ __ \/ __ `/
/ / / / / / __/ / ___ |/ / / __/ / / / /_/ /
/_/ /_/ /_/\___/ /_/ |_/_/ \___/_/ /_/\__,_/
Upvotes: 1