CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Day01-15/code/Day01/flag.py
Views: 729
"""1用Python的turtle模块绘制国旗2"""3import turtle456def draw_rectangle(x, y, width, height):7"""绘制矩形"""8turtle.goto(x, y)9turtle.pencolor('red')10turtle.fillcolor('red')11turtle.begin_fill()12for i in range(2):13turtle.forward(width)14turtle.left(90)15turtle.forward(height)16turtle.left(90)17turtle.end_fill()181920def draw_star(x, y, radius):21"""绘制五角星"""22turtle.setpos(x, y)23pos1 = turtle.pos()24turtle.circle(-radius, 72)25pos2 = turtle.pos()26turtle.circle(-radius, 72)27pos3 = turtle.pos()28turtle.circle(-radius, 72)29pos4 = turtle.pos()30turtle.circle(-radius, 72)31pos5 = turtle.pos()32turtle.color('yellow', 'yellow')33turtle.begin_fill()34turtle.goto(pos3)35turtle.goto(pos1)36turtle.goto(pos4)37turtle.goto(pos2)38turtle.goto(pos5)39turtle.end_fill()404142def main():43"""主程序"""44turtle.speed(12)45turtle.penup()46x, y = -270, -18047# 画国旗主体48width, height = 540, 36049draw_rectangle(x, y, width, height)50# 画大星星51pice = 2252center_x, center_y = x + 5 * pice, y + height - pice * 553turtle.goto(center_x, center_y)54turtle.left(90)55turtle.forward(pice * 3)56turtle.right(90)57draw_star(turtle.xcor(), turtle.ycor(), pice * 3)58x_poses, y_poses = [10, 12, 12, 10], [2, 4, 7, 9]59# 画小星星60for x_pos, y_pos in zip(x_poses, y_poses):61turtle.goto(x + x_pos * pice, y + height - y_pos * pice)62turtle.left(turtle.towards(center_x, center_y) - turtle.heading())63turtle.forward(pice)64turtle.right(90)65draw_star(turtle.xcor(), turtle.ycor(), pice)66# 隐藏海龟67turtle.ht()68# 显示绘图窗口69turtle.mainloop()707172if __name__ == '__main__':73main()7475