[英]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.我正在用 python 编写一个非常小的和基本的基于文本的冒险游戏,我希望能够对齐我的标题(这是在 ASCII 中)我已经查过了,但我要么不理解,要么不理解”工作。
Here is the ASCII:这是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.您可以使用
str.center
方法。 The code below assumes a screen width of 80 characters:下面的代码假设屏幕宽度为 80 个字符:
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获取终端窗口的大小(主要是列) - linux 命令: “stty -a” ,windows 命令: “mode con” - 并解析 cli 输出,或在此处执行操作: 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):这是一个示例(适用于 Linux 和 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
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)
________ ___
/_ __/ /_ ___ / | ________ ____ ____ _
/ / / __ \/ _ \ / /| | / ___/ _ \/ __ \/ __ `/
/ / / / / / __/ / ___ |/ / / __/ / / / /_/ /
/_/ /_/ /_/\___/ /_/ |_/_/ \___/_/ /_/\__,_/
Don't type ASCII art by yourself - use text2art:不要自己输入 ASCII 艺术 - 使用 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
代替):
text2art(new_string)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.