Python - Santa Tree Generator
- 环境
- 介绍
- 实现
- 方法一
- 思路
- print 相关设置
- 格式分解
- 示例
- 相关设置值
- 代码
- 方法二
环境
OS: Windows 10
专业版
64-bit
Python Version: 3.6.1
64-bit
介绍
刚刚过了圣诞节, 看了一篇文章,介绍了一些方法来使用python
生成圣诞树
。很有意思,看来过后便自己又写了一遍,外加了一些改进。
一共有三种方法:
- 方法一:利用
print
函数打印到console
或命令行界面
。 - 方法二:利用
Turtle
库来绘制并显示。 - 方法三:利用
Turtle
库来绘制并显示。
实现
按照难度顺序开始实现,依次是方法一、二、三。我建了一个类叫SantaTree
来放置这三个方法。
方法一
最后输出为下图所示:
思路
思路大致为:
- 先生成
字符
,堆叠为圣诞树的模样。 - 再利用
print
命令的相关设置改变字体以及背景的颜色
。 - 最后再改变
特别位置
的字符颜色来增加一些装饰
效果。
print 相关设置
关于改变print输出颜色等设置的问题,我也是在网上查找了相关的资料。
格式为:
print("\033[0;37;40m目标字符串\033[0m")
格式分解
其中:
-
print()
是python的输出指令
-
\033[
是开始
的标志 -
\033[0m
是结束
的标志 -
0;37;40m
中,0的位置
是显示的方式
, 默认为0;37的位置
是字体的颜色
;40的位置
是背景色的颜色
,m
是这些设置结束
的标志。设置
间依靠分号
,;
,进行分隔
。即,显示模式;字体颜色;背景色m``设置
间依靠分号
,;
,进行分隔
。即,显示模式;字体颜色;背景色m
-
目标字符串
就在设置部分结尾
和结束
的中间
。 - 三个设置选项并不要求全部有参数输入。如果不输入设置则为默认,例如:
print("\033[0;30m默认黑字\033[0m")
示例
print("\033[0;30;42m默认黑字绿底\033[0m")
输出:
相关设置值
数值 | 显示风格 | 字体颜色 | 背景色 |
0 | 终端默认设置 | - | - |
1 | 高亮 | - | - |
4 | 下划线 | - | - |
5 | 闪烁 | - | - |
7 | 反色显示 | - | - |
24 | 非 下划线 | - | - |
25 | 非 闪烁 | - | - |
27 | 非 反色显示 | - | - |
- | - | - | - |
30 | - | 黑色 | - |
31 | - | 红色 | - |
32 | - | 绿色 | - |
33 | - | 黄色 | - |
34 | - | 蓝色 | - |
35 | - | 紫色 | - |
36 | - | 青色 | - |
37 | - | 白色 | - |
- | - | - | - |
40 | - | - | 黑色 |
41 | - | - | 红色 |
42 | - | - | 绿色 |
43 | - | - | 黄色 |
44 | - | - | 蓝色 |
45 | - | - | 紫色 |
46 | - | - | 青色 |
47 | - | - | 白色 |
上面的数值都是和设置都是一对一的关系,即,即使你在第三个位置输入的是1,也会输出为格式为高亮的结果。
- 记忆方式为
- 单独记格式
- 颜色结尾都是
0~7
, 都分别对应的黑红绿黄蓝紫靑白
-
字体颜色
开头为3
-
背景色
开头为4
代码
额外写了一个函数封装了一下字符串颜色的设置:
def color_string(self, style=0, font_color=32, back_color=42, fill_string="*"):
"""
return string with input settings
:param style: font display style, default as console default
:param font_color: color of font, default as black
:param back_color: background color, default as white
:param fill_string: symbol inside the color block
:return: String with console display style settings
"""
try:
return "\033[" \
+ str(style) + ";" + str(font_color) + ";" + str(back_color) \
+ "m" \
+ fill_string \
+ "\033[0m"
except:
raise TypeError
圣诞树的代码如下:
def text_xmas_tree(self, height=6):
"""
decoration is the string to be showed as decoration
fills is the base element of the santa tree
:param height: total height of the tree, also the total lines of the whole text
:return: None
"""
# minimum height is 6 or the tree looks ugly
if height < 6:
height = 6
fills = 1
for i in range(1, height):
# decoration color depends on the number of the level
if i % 3 == 0:
# red font and red back with default style
decoration = self.color_string(0, 32, 41, "*")
elif i % 2 == 0:
# green font and green back with default style
decoration = self.color_string(0, 31, 42, "*")
else:
# yellow font and yellow back with default style
decoration = self.color_string(0, 31, 43, "*")
if fills > 1:
# combine string with the format:
# spaces + decoration + fills + decoration
print((' ' * (height - i)) \
+ decoration \
+ (self.color_string(0, 37, 42, "+") * (fills - 2)) \
+ decoration)
else:
# first line has only one symbol
print((' ' * (height - i)) + decoration)
fills += 2
# bottom of the tree, red font and red back
print((' ' * (height - 1)) + self.color_string(5, 31, 41, "|"))
方法二
摸鱼中。。。