Python学习(8)---3月26日打卡

Python学习(8)---3月26日打卡

递归函数:

Python学习(8)---3月26日打卡

"""
功能:五角星的绘制
"""
import turtle
def draw_pentagram(size):
    """
    绘制五角星(使得结构简洁,程序化)
    """
    # 绘制一个
    count = 1
    while count <= 5:
        turtle.forward(size)
        turtle.right(144)
        # count = count + 1
        count+=1
# 迭代绘制五角星
def draw_recuisive_pentagram(size):
    count = 1
    while count <= 5:
        turtle.forward(size)
        turtle.right(144)
        # count = count + 1
        count += 1
#         五角星绘制完成,更新参数
    size+=10
    if size<=100:
        draw_recuisive_pentagram(size)
    draw_recuisive_pentagram(size)
def main():
    """
    主函数
    """
    turtle.penup()
    turtle.backward(200)
    turtle.pendown()
    turtle.pensize(2)
    turtle.pencolor("red")
    # 计数器
    size=50
    draw_recuisive_pentagram(size)
    # while size<=500:
    #     # 调用函数
    #     draw_pentagram(size)
    #     # size=size+50
    #     size+=50

    turtle.exitonclick()
if __name__=='__main__':
    main()

分形树绘制:

Python学习(8)---3月26日打卡

Python学习(8)---3月26日打卡

Python学习(8)---3月26日打卡

Python学习(8)---3月26日打卡

"""
利用递归函数绘制分形树
"""
import turtle

def draw_branch(branch_length):
    """
    绘制分形树
    """
    if branch_length>5:
        # 绘制右侧树枝
        #     主干
        turtle.forward(branch_length)
        print("向前",branch_length)
        #     向右转20度
        turtle.right(20)
        print("向右转20度")
        draw_branch(branch_length-15)
#         绘制左侧树枝
        turtle.left(40)
        draw_branch(branch_length-15)
#         返回之前的树枝
        turtle.right(20)
        turtle.backward(branch_length)
        print("向后", branch_length)
def main():
    """
    主函数\
    """
    turtle.left(90)
    # 坐标轴的方向是向右的,所以进行转角度
    # 因为绘制的话是从画布的中心点开始,所以若初始值比较大的话会看不到,所以移动绘制的位置
    # 抬起画笔
    turtle.penup()
    turtle.backward(150)
    # 放下画笔
    turtle.pendown()
    # 加颜色
    turtle.color("brown")
    draw_branch(100)
    # 实参
    turtle.exitonclick()
if __name__ == '__main__':
    main()

实现效果:

Python学习(8)---3月26日打卡

习题:

Python学习(8)---3月26日打卡

Python学习(8)---3月26日打卡

Python学习(8)---3月26日打卡

Python学习(8)---3月26日打卡

 

相关文章:

  • 2021-08-21
  • 2021-07-26
  • 2021-07-26
  • 2021-04-25
  • 2022-01-12
  • 2021-07-18
  • 2021-07-15
  • 2021-07-10
猜你喜欢
  • 2021-04-21
  • 2022-01-04
  • 2021-05-31
  • 2021-10-24
  • 2021-08-11
  • 2021-10-19
  • 2022-01-04
相关资源
相似解决方案