前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >这个母亲节的礼物,我为你们准备好了!(内含福利)

这个母亲节的礼物,我为你们准备好了!(内含福利)

作者头像
菜鸟小白的学习分享
发布2021-05-13 16:42:48
4531
发布2021-05-13 16:42:48
举报

明天就是母亲节了,你是否已经给你的麻麻准备了节日礼物呢?在这里菜鸟小白预祝天下的妈妈节日快乐,每天都过的开开心心。

在学习python的菜鸟小白当然也要通过自己学习的内容来表达出妈妈的节日祝福咯。

效果展示

首先我们看看今天程序的实现效果,今天我们打包了一个exe文件,双击执行后就可以循环播放彩色祝福。

python编码

接下来我们来看具体的编码内容,首先我们需要介绍一下今天我们需要用的一个新python库——colorama。colorama是一个python专门用来在控制台、命令行输出彩色文字的模块,可以跨平台使用。这个库并不是python的默认库,所以我们需要通过pip来安装。

代码语言:javascript
复制
pip install colorama

可用的格式有:

代码语言:javascript
复制
Fore: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
Back: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
Style: DIM, NORMAL, BRIGHT, RESET_ALL

跨平台印刷彩色文本可以使用彩色光的常数简称ANSI转义序列:

代码语言:javascript
复制
from colorama import Fore,Back,Style
print (Fore.RED + "some red text")
print (Back.GREEN + "and with a green background")
print (Style.DIM + "and in dim text")
print (Style.RESET_ALL)
print ("back to normal now!!")

接下来我们就通过这个库来实现一个彩色的爱心祝福程序。

代码语言:javascript
复制
# 配置颜色
colorama.init(convert=True)
RED = colorama.Fore.RED + colorama.Style.BRIGHT
CYAN = colorama.Fore.CYAN + colorama.Style.BRIGHT
GREEN = colorama.Fore.GREEN + colorama.Style.BRIGHT
YELLOW = colorama.Fore.YELLOW + colorama.Style.BRIGHT
MAGENTA = colorama.Fore.MAGENTA + colorama.Style.BRIGHT

# 打印抬头
for i in range(1, 35):
    print('')
# \*的位置
heartStars = [2, 4, 8, 10, 14, 20, 26, 28, 40, 44, 52, 60, 64, 76]
# 空格的位置
heartBreakLines = [13, 27, 41, 55, 69, 77]
# 玫瑰的空列位置
flowerBreakLines = [7, 15, 23, 31, 39, 46]


# 添加空列
def addSpaces(a):
    count = a
    while count > 0:
        print(' ', end='')
        count -= 1


# 添加空行
def newLineWithSleep():
    time.sleep(0.3)
    print('\n', end='')


play = 2    # 循环次数
while play != 0:
    Left_Spaces = randint(8, 80)
    addSpaces(Left_Spaces)
    # 画心
    for i in range(0, 78):
        if i in heartBreakLines:
            newLineWithSleep()
            addSpaces(Left_Spaces)
        elif i in heartStars:
            print(RED + '*', end='')
        elif i in (32, 36):
            print(GREEN + 'M', end='')
        elif i == 34:
            print(GREEN + 'O', end='')
        else:
            print(' ', end='')
    newLineWithSleep()
    addSpaces(randint(8, 80))
    print(YELLOW + "妈妈,母亲节快乐!", end='')
    newLineWithSleep()
    newLineWithSleep()
    Left_Spaces = randint(8, 80)
    addSpaces(Left_Spaces)
    # 画花
    for i in range(0, 47):
        if i in flowerBreakLines:
            newLineWithSleep()
            addSpaces(Left_Spaces)
        elif i in (2, 8, 12, 18):
            print(MAGENTA + '{', end='')
        elif i in (3, 9, 13, 19):
            print(MAGENTA + '_', end='')
        elif i in (4, 10, 14, 20):
            print(MAGENTA + '}', end='')
        elif i in (27, 35, 43):
            print(GREEN + '|', end='')
        elif i in (34, 44):
            print(GREEN + '~', end='')
        elif i == 11:
            print(YELLOW + 'o', end='')
        else:
            print(' ', end='')
    print('\n', end='')
    play = play-1
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-05-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 菜鸟小白的学习分享 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档