一、绘制成品
二、绘制代码
实现本文效果的整体思路是:加载库—选择背景音乐—绘制心的外轮廓—填充心并写告白信—绘制心动线。
1.导入库
1
2
3
4
5
6
7
8
9
|
# -*- coding: UTF-8 -*- ''' 代码用途 :情人节表白 作者 :阿黎逸阳 博客 : https://blog.csdn.net/qq_32532663/article/details/106176609 ''' import os import pygame import turtle as t |
本文应用到的库较少,只应用了os、pygame和turtle三个库。
os库可以设置文件读取的位置。
pygame库是为了绘制过程更有趣,在绘图过程中添加了背景音乐,如果无需背景音乐,不用加载该库。
turtle库是绘图库,相当于给你一支画笔,你可以在画布上用数学逻辑控制的代码完成绘图。
2.选择背景音乐
接着应用pygame库播放背景音乐,本文的音乐是《 瞬间的永恒》。
1
2
3
4
5
6
|
#播放音乐 print ( '播放音乐' ) pygame.mixer.init() pygame.mixer.music.load(r "F:\公众号\520\赵海洋 - 《瞬间的永恒》夜色钢琴曲.mp3" ) pygame.mixer.music.set_volume( 0.5 ) pygame.mixer.music.play( 1 , 10 ) |
这一部分的代码和整体代码是剥离的,可以选泽在最开始放上该代码,也可以直接删除。如果选择播放音乐,需要在代码music.load函数中把你想放音乐的地址填进去。
3.绘制心的外轮廓
然后绘制心的外轮廓,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
t.title( '阿黎逸阳的代码公众号' ) t.speed( 10 ) #t.screensize(1000, 800) t.setup(startx = 0 , starty = 0 , width = 800 , height = 600 ) t.hideturtle() print ( '画爱心' ) #画爱心 def heart(x, y): t.penup() t.goto(x, y) t.pendown() t.color( 'pink' ) t.setheading( 50 ) t.circle( - 5 , 180 ) t.circle( - 45 , 12 ) t.setheading( 130 ) t.circle( - 45 , 12 ) t.circle( - 5 , 180 ) heart( - 30 , 155 ) heart( - 220 , 145 ) heart( - 210 , 60 ) heart( - 100 , 100 ) heart( - 20 , 20 ) heart( - 70 , 130 ) heart( - 140 , - 20 ) heart( 30 , 100 ) heart( - 60 , - 20 ) heart( 10 , 60 ) heart( - 100 , - 70 ) heart( 20 , 145 ) heart( - 140 , - 20 ) heart( - 130 , 130 ) heart( - 180 , 20 ) heart( - 170 , 155 ) heart( - 230 , 100 ) |
关键代码详解:
t.penup():抬起画笔,一般用于另起一个地方绘图使用。
t.goto(x,y):画笔去到某个位置,参数为(x,y),对应去到的横坐标和纵坐标。
t.pendown():放下画笔,一般和penup组合使用。
t.color(color):设置画笔的颜色。
t.setheading(θ):设置海龟头与横坐标偏离的度数。
t.circle(radius,extent,steps):radius指半径,若为正,半径在小乌龟左侧radius远的地方,若为负,半径在小乌龟右侧radius远的地方;extent指弧度;steps指阶数。画外轮廓的关键是:通过调节circle函数中的半径和弧度来调节曲线的弧度,从而使得小蜜蜂的轮廓比较流畅。
4.填充心并写告白信
接下来边填充心,边写告白信,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
def write_mes(x, y, size, ss): t.hideturtle() t.penup() t.goto(x, y) t.pendown() t.pencolor( 'black' ) t.write(ss, font = ( 'Times New Roman' , size, 'normal' )) #画红心 print ( '画红心' ) def heart_fill(x, y): t.penup() t.goto(x, y) t.pendown() t.color( 'red' , 'red' ) t.begin_fill() t.setheading( 50 ) t.circle( - 5 , 180 ) t.circle( - 45 , 12 ) t.setheading( 130 ) t.circle( - 45 , 12 ) t.circle( - 5 , 180 ) t.end_fill() x = 90 y = 110 #右边爱心 write_mes(x, y, 11 , '喜 欢 你 的 每 一 天' ) heart_fill( - 100 , 100 ) heart_fill( - 70 , 130 ) heart_fill( - 30 , 155 ) heart_fill( 20 , 145 ) heart_fill( 30 , 100 ) write_mes(x, y - 30 , 11 , '爱 意 不 曾 退 减' ) heart_fill( 10 , 60 ) heart_fill( - 20 , 20 ) heart_fill( - 60 , - 20 ) heart_fill( - 100 , - 70 ) #左边爱心 write_mes(x, y - 30 * 2 , 11 , '时 光 不 曾 走 远' ) heart_fill( - 140 , - 20 ) heart_fill( - 180 , 20 ) heart_fill( - 210 , 60 ) heart_fill( - 230 , 100 ) write_mes(x, y - 30 * 3 , 11 , '幸 福 延 续 到 明 天' ) heart_fill( - 220 , 145 ) heart_fill( - 170 , 155 ) heart_fill( - 130 , 130 ) write_mes(x, y - 30 * 4 , 11 , '永 远 不 说 再 见' ) |
5.画心动线
最后是写姓名并画心动线,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
t.speed( 15 ) print ( '画心动线' ) def heart_bit(): #画心动线 t.penup() t.goto( - 170 , 40 ) t.pendown() t.pencolor( 'red' ) t.setheading( 0 ) t.pensize( 2 ) t.forward( 10 ) #第一个小波浪 t.setheading( 45 ) t.circle( 50 , 10 ) t.setheading( 0 ) t.circle( - 3 , 90 ) t.circle( 50 , 5 ) #横线 t.setheading( 0 ) t.forward( 10 ) #第一个下尖峰 t.setheading( - 80 ) t.forward( 7 ) t.setheading( 70 ) t.forward( 25 ) t.setheading( - 85 ) t.forward( 29 ) t.setheading( 70 ) t.forward( 13 ) t.setheading( 0 ) t.forward( 15 ) #画心 t.setheading( 150 ) t.circle( - 20 , 40 ) t.circle( - 10 , 170 ) t.setheading( 70 ) t.circle( - 10 , 170 ) t.circle( - 20 , 40 ) t.setheading( 0 ) t.forward( 15 ) #2 t.setheading( - 80 ) t.forward( 7 ) t.setheading( 70 ) t.forward( 25 ) t.setheading( - 85 ) t.forward( 29 ) t.setheading( 70 ) t.forward( 13 ) t.setheading( 0 ) t.forward( 15 ) t.setheading( 0 ) t.forward( 10 ) t.setheading( 45 ) t.circle( 50 , 10 ) t.setheading( 0 ) t.circle( - 3 , 90 ) t.circle( 50 , 5 ) t.setheading( 0 ) t.forward( 10 ) def write_name(x, y, size, ss): t.hideturtle() t.penup() t.goto(x, y) t.pendown() t.pencolor( 'black' ) t.write(ss, font = ( 'Times New Roman' , size, 'normal' )) def undo_back(): t.undo() t.undo() t.undo() t.undo() t.undo() t.undo() t.undo() t.undo() t.undo() t.undo() def undo_back2(): t.undo() t.undo() def name_heart_bit(): #写两个人的姓名(需替换成真实姓名) write_name( - 180 , 70 , 11 , '韩商言' ) write_name( - 180 , 70 , 11 , '韩商言' ) write_name( - 180 , 70 , 11 , '韩商言' ) heart_bit() write_name( - 60 , 70 , 11 , '佟年' ) write_name( - 60 , 70 , 11 , '佟年' ) write_name( - 60 , 70 , 11 , '佟年' ) write_name( - 60 , 70 , 11 , '佟年' ) write_name( - 60 , 70 , 11 , '佟年' ) undo_back() undo_back() undo_back() undo_back() undo_back() undo_back() undo_back() undo_back() undo_back() undo_back2() while 1 : name_heart_bit() |
到此这篇关于基于Python绘制520表白代码的文章就介绍到这了,更多相关Python表白内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_32532663/article/details/124786507