文章目录
- 1.初始化
- 2.颜色
- 3.画笔
- 4.其他
- 案例:分形树的绘制
1.初始化
import turtle
pen = turtle.Turtle()
turtle.done()
2.颜色
turtle.colormode(255)
turtle.bgcolor("blue")
pen.fillcolor("red")
pen.begin_fill()
pen.end_fill()
3.画笔
pen.color(255, 0, 0)
pen.color("blue")
pen.speed(0)
pen.pensize(40)
pen.forward(100)
pen.left(90)
pen.circle(80, extent=360, steps=6)
pen.penup()
pen.pendown()
pen.goto(0, 0)
4.其他
turtle.bgpic("../res/su7.png")
turtle.setup(width=1839, height=690, startx=200, starty=200)
案例:分形树的绘制
import turtle
import random
pen = turtle.Turtle()
turtle.colormode(255)
pen.speed(0)
R, G, B = 0, 0, 0
length = 120
PEN_SIZE = 14
pen.pensize(PEN_SIZE)
pen.goto(0, -120)
pen.left(90)
pen.goto(0, 0)
def fractal_tree(l, lv):
global R, G, B
R += 10
G += 50
B += 80
pen.pencolor(R % 200, G % 200, B % 200)
size = pen.pensize()
l = l * (3 / 4)
pen.pensize(size * 3 / 4)
pen.left(45)
pen.forward(l)
if lv < 14:
fractal_tree(l, lv + 1)
pen.backward(l)
pen.right(90)
pen.forward(l)
if lv < 14:
fractal_tree(l, lv + 1)
pen.backward(l)
pen.left(45)
pen.pensize(size)
if __name__ == '__main__':
fractal_tree(length, 6)
turtle.done()