CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day01-15/code/Day01/flag.py
Views: 729
1
"""
2
用Python的turtle模块绘制国旗
3
"""
4
import turtle
5
6
7
def draw_rectangle(x, y, width, height):
8
"""绘制矩形"""
9
turtle.goto(x, y)
10
turtle.pencolor('red')
11
turtle.fillcolor('red')
12
turtle.begin_fill()
13
for i in range(2):
14
turtle.forward(width)
15
turtle.left(90)
16
turtle.forward(height)
17
turtle.left(90)
18
turtle.end_fill()
19
20
21
def draw_star(x, y, radius):
22
"""绘制五角星"""
23
turtle.setpos(x, y)
24
pos1 = turtle.pos()
25
turtle.circle(-radius, 72)
26
pos2 = turtle.pos()
27
turtle.circle(-radius, 72)
28
pos3 = turtle.pos()
29
turtle.circle(-radius, 72)
30
pos4 = turtle.pos()
31
turtle.circle(-radius, 72)
32
pos5 = turtle.pos()
33
turtle.color('yellow', 'yellow')
34
turtle.begin_fill()
35
turtle.goto(pos3)
36
turtle.goto(pos1)
37
turtle.goto(pos4)
38
turtle.goto(pos2)
39
turtle.goto(pos5)
40
turtle.end_fill()
41
42
43
def main():
44
"""主程序"""
45
turtle.speed(12)
46
turtle.penup()
47
x, y = -270, -180
48
# 画国旗主体
49
width, height = 540, 360
50
draw_rectangle(x, y, width, height)
51
# 画大星星
52
pice = 22
53
center_x, center_y = x + 5 * pice, y + height - pice * 5
54
turtle.goto(center_x, center_y)
55
turtle.left(90)
56
turtle.forward(pice * 3)
57
turtle.right(90)
58
draw_star(turtle.xcor(), turtle.ycor(), pice * 3)
59
x_poses, y_poses = [10, 12, 12, 10], [2, 4, 7, 9]
60
# 画小星星
61
for x_pos, y_pos in zip(x_poses, y_poses):
62
turtle.goto(x + x_pos * pice, y + height - y_pos * pice)
63
turtle.left(turtle.towards(center_x, center_y) - turtle.heading())
64
turtle.forward(pice)
65
turtle.right(90)
66
draw_star(turtle.xcor(), turtle.ycor(), pice)
67
# 隐藏海龟
68
turtle.ht()
69
# 显示绘图窗口
70
turtle.mainloop()
71
72
73
if __name__ == '__main__':
74
main()
75