明天就是母亲节了,你是否已经给你的麻麻准备了节日礼物呢?在这里菜鸟小白预祝天下的妈妈节日快乐,每天都过的开开心心。
在学习python的菜鸟小白当然也要通过自己学习的内容来表达出妈妈的节日祝福咯。
效果展示
首先我们看看今天程序的实现效果,今天我们打包了一个exe文件,双击执行后就可以循环播放彩色祝福。
python编码
接下来我们来看具体的编码内容,首先我们需要介绍一下今天我们需要用的一个新python库——colorama。colorama是一个python专门用来在控制台、命令行输出彩色文字的模块,可以跨平台使用。这个库并不是python的默认库,所以我们需要通过pip来安装。
pip install colorama
可用的格式有:
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转义序列:
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!!")
接下来我们就通过这个库来实现一个彩色的爱心祝福程序。
# 配置颜色
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