Mother's Day is here, write a blessing applet for mom with Python ~

Mother's Day is coming, I wonder if your partners have prepared gifts for mom?

Original link to WeChat official account

 


Today, Xiaobencong will share with you a small program for Mother's Day blessing, just like Valentine's Day , let's start happily!

Let's take a look at the effect first:


This small program is very simple, the libraries used are mainly  pygame  and  colorama . How are the specific steps implemented?

 

1. Play background music

We can achieve background music with pygame (code below). Of course, you can also change the background music, just replace the bgm.mp3 file in the file with your favorite music.

1 # 背景音乐
2 defplayBGM(bgm_path):
3    pygame.mixer.init()
4    pygame.mixer.music.load(bgm_path)
5    pygame.mixer.music.play(-1)

2. Design pattern elements and coordinates and output

We only need to use the colorama library to change the color of the text display in the Windows terminal for the patterns of flowers, love, text, etc. in the video, and then print out the coordinates of each element corresponding to these patterns.

Code to draw love:

 1 # 画爱心
 2  def drawHeart():
 3    num_spaces = random.randint(8, 80)
 4    print(' ' * num_spaces, end='')
 5    for i in range(78):
 6        if i in HEARTS:
 7            nextLine()
 8            print(' ' * num_spaces, end='')
 9        elif i in STARS:
10            print(RED + '*', end='')
11        elif i in [32, 36]:
12            print(GREEN + 'M', end='')
13        elif i == 34:
14            print(GREEN + 'O', end='')
15        else:
16            print(' ', end='')

Code to display blessing text:

1 # 显示祝福文字
2 def showText():
3    print(' ' * random.randint(8, 80), end='')
4    print(CYAN + "H a p p y  M o t h e r ' s   D a y !", end='')


The code to draw the little flower:

 1 # 画小花花
 2 def drawFlower():
 3    num_spaces = random.randint(8, 80)
 4    print(' ' * num_spaces, end='')
 5    for i in range(47):
 6        if i in FLOWERS:
 7            nextLine()
 8            print(' ' * num_spaces, end='')
 9        elif i in [2, 8, 12, 18]:
10            print(MAGENTA + '{', end='')
11        elif i in [3, 9, 13, 19]:
12            print(MAGENTA + '_', end='')
13        elif i in [4, 10, 14, 20]:
14            print(MAGENTA + '}', end='')
15        elif i in [27, 35, 43]:
16            print(GREEN + '|',  end='')
17        elif i in [34, 44]:
18            print(GREEN + '~', end='')
19        elif i == 11:
20            print(YELLOW + 'o', end='')
21        else:
22            print(' ', end='')


The above is the analysis process of this Mother's Day blessing applet.

WeChat public account " financial learner who learns programming " back-end " I love mom " to get the source code.

Original link to WeChat official account

 

Recommended in the past

1. Wandering Earth Movie Review

2. North Shanghai, Guangzhou and Shenzhen renting house book

3. Figure insect net beauty

4. Pig fart video

5. Lagou network data

Your likes and attention is my greatest support!

Save the scan code and pay attention to the public number

Published 11 original articles · won 11 · visited 5723

Guess you like

Origin blog.csdn.net/weixin_39270299/article/details/90147189