绘图库_turtle

图形绘制库turtle库

绘制函数

  • turtle.forward(distance)默认起点为窗口正中心,向右为前
  • turtle.backward(distance)
  • turtle.right(degree)
  • turtle.left(degree)
  • tertle.exitonclick()点击关闭画图界面,默认自动关闭
  • turtle.penup()抬起画笔,之后的移动不绘制
  • turtle.pendown()落下画笔,之后的移动绘制形状
  • turtle.pensize()设置话画笔宽度
  • turtle.pencolor()设置画笔颜色

绘制五角星

import turtle

def five_pint_star(length):
    '''
    绘制五角星
    :param length:五角星一条边长度
    :return:
    '''
    for i in range(5):
        turtle.forward(length)
        turtle.right(144)

def main():
    for i in range(40,120,40):
        five_pint_star(i)
    turtle.exitonclick()

if __name__=='__main__':
    main() 

绘制分形数(引入迭代)

import turtle
def initial_position(pos,angle,speed=3,size=2,color='black'):
    '''
    对画笔进行初始化
    :param pos: 坐标(二维的数组)
    :param angle: 角度(右转角度)
    :param speed: 绘制速度
    :param size: 画笔大小
    :param color: 画笔颜色
    :return:
    '''
    turtle.penup()
    turtle.setposition(pos[0],pos[1])
    turtle.right(angle)
    turtle.pendown()
    turtle.speed(speed)
    turtle.pensize(size)
    turtle.pencolor(color)

def little_tree(genLen):
    if genLen>10:
        turtle.forward(genLen)
        turtle.right(30)
        little_tree(genLen-8)
        turtle.left(60)
        little_tree(genLen-8)
        turtle.right(30)
        turtle.penup()
        turtle.backward(genLen)
        turtle.pendown()

def main():
    initial_position((0,-50),-90)
    little_tree(60)
    turtle.exitonclick()

if __name__=='__main__':
    main()

你可能感兴趣的:(python基础教程)