zoukankan      html  css  js  c++  java
  • python 利用turtle库绘制五角星


    # -*- coding: utf-8 –*-
    import turtle
    import math

    def draw_polygon(aTurtle, size=50, n=3):
    for i in range(n):
    aTurtle.forward(size)
    aTurtle.left(360.0/n)

    def draw_n_angle(aTurtle, size=50, num=5, color=None):
    if color:
    aTurtle.begin_fill()
    aTurtle.fillcolor(color)
    for i in range(num):
    aTurtle.forward(size)
    aTurtle.left(360.0/num)
    aTurtle.forward(size)
    aTurtle.right(2*360.0/num)
    if color:
    aTurtle.end_fill()

    def draw_5_angle(aTurtle=None, start_pos=(0,0), end_pos=(0,10), radius=100, color=None):
    aTurtle = aTurtle or turtle.Turtle()
    size = radius * math.sin(math.pi/5)/math.sin(math.pi*2/5)
    aTurtle.pencolor('yellow')
    aTurtle.left(math.degrees(math.atan2(end_pos[1]-start_pos[1], end_pos[0]-start_pos[0])))
    aTurtle.penup()
    aTurtle.goto(start_pos)
    aTurtle.fd(radius)
    aTurtle.pendown()
    aTurtle.right(math.degrees(math.pi*9/10))
    draw_n_angle(aTurtle, size, 5, color)

    def draw_5_star_flag(times=20.0):
    width, height = 30*times, 20*times
    window = turtle.Screen()
    aTurtle = turtle.Turtle()
    aTurtle.hideturtle()
    aTurtle.speed(10)

    # 画五星
    draw_5_angle(aTurtle, start_pos=(-10*times, 5*times), end_pos=(-10*times, 8*times), radius=3*times, color='red')

    # 点击关闭窗口
    window.exitonclick()

    if __name__ == '__main__':
    draw_5_star_flag()

    如有不懂,请加群讨论

  • 相关阅读:
    014.Python函数
    013.Python的文件操作
    012.Python的字典和集合的相关函数
    011.Python的列表的相关操作
    010.Python字符串的格式化
    009.Python字符串相关函数
    008.Python循环for循环
    007.Python循环语句while循环嵌套
    Java 反射机制 初探*
    Java 正则初探
  • 原文地址:https://www.cnblogs.com/chaihy/p/10118839.html
Copyright © 2011-2022 走看看