简体   繁体   中英

ASCII Characters Text Align

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?

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:

                 ________            ___                                        
                /_  __/ /_  ___     /   |  ________  ____  ____ _               
                 / / / __ \/ _ \   / /| | / ___/ _ \/ __ \/ __ `/               
                / / / / / /  __/  / ___ |/ /  /  __/ / / / /_/ /                
               /_/ /_/ /_/\___/  /_/  |_/_/   \___/_/ /_/\__,_/                 

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)

  • and then: padding_left = (terminal_columns - text-max_columns) / 2 , ceil or floor the number as you wish
  • prepend all the text lines with so many spaces as the padding_left value is

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

Output screenshot

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)

output:

              ________            ___                         
             /_  __/ /_  ___     /   |  ________  ____  ____ _
              / / / __ \/ _ \   / /| | / ___/ _ \/ __ \/ __ `/
             / / / / / /  __/  / ___ |/ /  /  __/ / / / /_/ / 
            /_/ /_/ /_/\___/  /_/  |_/_/   \___/_/ /_/\__,_/  

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM