有趣的python库-turtle
阅读原文时间:2023年07月08日阅读:2

turtle-绘制图像用

画樱花树

import turtle as tt
import random

画樱花的躯干(60,t)

def tree(branch, t):
if branch > 3:
if 8 <= branch <= 12:
if random.randint(0, 2) == 0:
t.color('snow') # 白
else:
t.color('lightcoral') # 淡珊瑚色
t.pensize(branch / 3)
elif branch < 8:
if random.randint(0, 1) == 0:
t.color('snow')
else:
t.color('lightcoral') # 淡珊瑚色
t.pensize(branch / 2)
else:
t.color('sienna') # 赭(zhě)色
t.pensize(branch / 10) # 6
t.forward(branch)
a = 1.5 * random.random()
t.right(20 * a)
b = 1.5 * random.random()
tree(branch - 10 * b, t)
t.left(40 * a)
tree(branch - 10 * b, t)
t.right(20 * a)
t.up()
t.backward(branch)
t.down()

掉落的花瓣

def petal(m, tr):
for i in range(m):
a = 200 - 400 * random.random()
b = 10 - 20 * random.random()
tr.up()
tr.forward(b)
tr.left(90)
tr.forward(a)
tr.down()
tr.color('lightcoral') # 淡珊瑚色
tr.circle(1)
tr.up()
tr.backward(a)
tr.right(90)
tr.backward(b)

绘图区域

te = tt.Turtle()

画布大小

w = tt.Screen()
te.hideturtle() # 隐藏画笔
te.getscreen().tracer(5, 0)
w.screensize(bg='wheat') # wheat小麦
te.left(90)
te.up()
te.backward(150)
te.down()
te.color('sienna')

画樱花的躯干

tree(60, te)

掉落的花瓣

petal(200, te)

去一个合适的位置,中心为(0,0),写一段话

te.goto(20, -80)
te.write("庆年最美", font=("Microsoft YaHei", 10, "normal"))
w.exitonclick()

画玫瑰花

import turtle

设置画布大小

turtle.screensize(canvwidth=None, canvheight=None, bg=None)

turtle.setup(width=0.6, height=0.6)

设置初始位置

turtle.penup()
turtle.left(90)
turtle.fd(200)
turtle.pendown()
turtle.right(90)

输出文字

printer = turtle.Turtle()
printer.hideturtle()
printer.penup()
printer.goto(60, -120)
printer.write("So beautiful rose! the same as you \n\n", align="right", font=("楷体", 16, "bold"))
printer.write(" ——from 俊雄", align="center", font=("楷体", 12, "normal"))

花蕊

turtle.fillcolor("red")
turtle.begin_fill()
turtle.circle(10, 180)
turtle.circle(25, 110)
turtle.left(50)
turtle.circle(60, 45)
turtle.circle(20, 170)
turtle.right(24)
turtle.fd(30)
turtle.left(10)
turtle.circle(30, 110)
turtle.fd(20)
turtle.left(40)
turtle.circle(90, 70)
turtle.circle(30, 150)
turtle.right(30)
turtle.fd(15)
turtle.circle(80, 90)
turtle.left(15)
turtle.fd(45)
turtle.right(165)
turtle.fd(20)
turtle.left(155)
turtle.circle(150, 80)
turtle.left(50)
turtle.circle(150, 90)
turtle.end_fill()

花瓣1

turtle.left(150)
turtle.circle(-90, 70)
turtle.left(20)
turtle.circle(75, 105)
turtle.setheading(60)
turtle.circle(80, 98)
turtle.circle(-90, 40)

花瓣2

turtle.left(180)
turtle.circle(90, 40)
turtle.circle(-80, 98)
turtle.setheading(-83)

叶子1

turtle.fd(30)
turtle.left(90)
turtle.fd(25)
turtle.left(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(-80, 90)
turtle.right(90)
turtle.circle(-80, 90)
turtle.end_fill()

turtle.right(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(85)
turtle.left(90)
turtle.fd(80)

叶子2

turtle.right(90)
turtle.right(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(80, 90)
turtle.left(90)
turtle.circle(80, 90)
turtle.end_fill()

turtle.left(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(60)
turtle.right(90)
turtle.circle(200, 60)

turtle.done()

乱画一顿

import turtle
turtle.speed('fastest')
turtle.pensize(1)
for y in range(200):
turtle.forward(3*y)
turtle.left(20)
turtle.right(175)