Python 生成圣诞树 Santa Tree Generator 不务正业系列#1

环境

OS: Windows 10 专业版 64-bit
Python Version: 3.6.1 64-bit

介绍

刚刚过了圣诞节, 看了一篇文章,介绍了一些方法来使用python生成圣诞树。很有意思,看来过后便自己又写了一遍,外加了一些改进。原文 地址为 https://mp.weixin.qq.com/s/UTAJG8K0YpEzZ7QQ1B3obg .

一共有三种方法:

  1. 方法一:利用print函数打印到console命令行界面
  2. 方法二:利用Turtle库来绘制并显示。
  3. 方法三:利用Turtle库来绘制并显示。

实现

按照难度顺序开始实现,依次是方法一、二、三。我建了一个类叫SantaTree来放置这三个方法。

方法一

最后输出为下图所示:
方法一输出

思路

思路大致为:

  1. 先生成字符,堆叠为圣诞树的模样。
  2. 再利用print命令的相关设置改变字体以及背景的颜色
  3. 最后再改变特别位置的字符颜色来增加一些装饰效果。

print 相关设置

关于改变print输出颜色等设置的问题,我也是在网上查找了相关的资料。
格式为:

print("\033[0;37;40m目标字符串\033[0m")

格式分解

其中:

  1. print() 是python的输出指令
  2. \033[开始的标志
  3. \033[0m结束的标志
  4. 0;37;40m 中, 0的位置显示的方式, 默认为0;37的位置字体的颜色40的位置背景色的颜色m这些设置结束的标志。
    设置间依靠分号;,进行分隔。即,显示模式;字体颜色;背景色m``设置间依靠分号;,进行分隔。即,显示模式;字体颜色;背景色m
  5. 目标字符串 就在 设置部分结尾结束中间
  6. 三个设置选项并不要求全部有参数输入。如果不输入设置则为默认,例如:
    print("\033[0;30m默认黑字\033[0m")
    
    没有设置最后一项背景色,则背景色为命令行或console默认颜色。

示例

print("\033[0;30;42m默认黑字绿底\033[0m")

输出:
print示例输出

相关设置值

数值 显示风格 字体颜色 背景色
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, "|")) 

方法二

摸鱼中。。。

猜你喜欢

转载自blog.csdn.net/weixin_44218389/article/details/85294726