每当有朋友过生日时,生日蛋糕自然是必不可少的,今天我们来看一下如何用 Python 画一个生日蛋糕。
本文我们用到的 Python 库包括:turtle、math 和 random。
实现的主要代码如下:
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
106
107
108
109
110
111
|
import math as m import random as r import turtle as t t.speed( 0 ) t.delay( 0 ) # 设置背景颜色及窗口 t.bgcolor( "#FFFFFF" ) t.setup( 800 , 600 ) t.penup() t.goto( 150 , 0 ) t.pendown() t.pencolor( "white" ) t.begin_fill() for i in range ( 360 ): x = drawX( 150 , i) y = drawY( 60 , i) t.goto(x, y) t.fillcolor( "#fef5f7" ) t.end_fill() t.begin_fill() for i in range ( 180 ): x = drawX( 150 , - i) y = drawY( 70 , - i) t.goto(x, y) for i in range ( 180 , 360 ): x = drawX( 150 , i) y = drawY( 60 , i) t.goto(x, y) t.fillcolor( "#f2d7dd" ) t.end_fill() t.pu() t.goto( 120 , 0 ) t.pd() t.begin_fill() for i in range ( 360 ): x = drawX( 120 , i) y = drawY( 48 , i) t.goto(x, y) t.fillcolor( "#33CCFF" ) t.end_fill() t.begin_fill() t.pencolor( "#fee48c" ) for i in range ( 540 ): x = drawX( 120 , i) y = drawY( 48 , i) + 70 t.goto(x, y) t.goto( - 120 , 0 ) t.fillcolor( "#99FFFF" ) t.end_fill() t.pu() t.goto( 120 , 70 ) t.pd() t.pencolor( "#fff0f3" ) t.begin_fill() for i in range ( 360 ): x = drawX( 120 , i) y = drawY( 48 , i) + 70 t.goto(x, y) t.fillcolor( "#fff0f3" ) t.end_fill() t.pu() t.goto( 110 , 70 ) t.pd() t.pencolor( "#fff9fb" ) t.begin_fill() for i in range ( 360 ): x = drawX( 110 , i) y = drawY( 44 , i) + 70 t.goto(x, y) t.fillcolor( "#FFCCCC" ) t.end_fill() t.pu() t.goto( 120 , 0 ) t.pd() t.begin_fill() t.pencolor( "#ffa79d" ) for i in range ( 180 ): x = drawX( 120 , - i) y = drawY( 48 , - i) + 10 t.goto(x, y) t.goto( - 120 , 0 ) for i in range ( 180 , 360 ): x = drawX( 120 , i) y = drawY( 48 , i) t.goto(x, y) t.fillcolor( "#ffa79d" ) t.end_fill() for i in range ( 50 ): t.pu() x = r.randint( - 500 , 500 ) y = r.randint( 120 , 300 ) t.goto(x, y) t.pd() t.dot(r.randint( 3 , 5 ), color[r.randint( 0 , 7 )]) t.penup() t.goto( - 130 , 230 ) t.pencolor( "#FF0000" ) t.write( "Happy Birthday" , font = ( "Curlz MT" , 30 )) t.hideturtle() t.done() |
实现效果:
以上就是Python+Turtle绘制一个可爱的生日蛋糕的详细内容,更多关于Python Turtle蛋糕的资料请关注服务器之家其它相关文章!
原文链接:https://mp.weixin.qq.com/s/bkgWFlxyPot7w8HPkCRh5Q