欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

樱花树

程序员文章站 2023-10-30 17:15:22
为什么要画樱花今天,我在路边看到了一颗樱花树。从侧面看去他是异常的美丽。所以我们今天就用turtle来画樱花树吧!樱花树为什么要画樱花所需模块樱花树的躯干掉落的花般运行部分总代码所需模块需要的库import turtleimport randomimport time需要的模块turtle.setheading()#快速绘图turtle.forward()#向当前画笔方向移动distance像素长turtle.backward()#向当前画笔相反方向移动distance像素长...

为什么要画樱花

今天,我在路边看到了一颗樱花树。从侧面看去他是异常的美丽。
所以我们今天就用turtle来画樱花树吧!

所需模块

需要的库

import turtle
import random
import time

需要的模块

turtle.setheading()
#快速绘图
turtle.forward()
#向当前画笔方向移动distance像素长
turtle.backward()
#向当前画笔相反方向移动distance像素长度
turtle.left()
#逆时针移动
turtle.right()
#顺时针移动
turtle.penup()
#移动时不绘制图形,提起笔,用于另起一个地方绘制时用
turtle.pendown()
#移动时绘制图形,缺省时也为绘制
turtle.fillcolor()
#绘制图形的填充颜色
turtle.end_fill()
#填充完成
turtle.begin_fill()
#准备开始填充图形
turtle.circle()
#以给定半径画圆
turtle.goto()
#将画笔移动到坐标为x,y的位置
turtle.tracer()
#追踪运行轨迹
turtle.hideturtle()	
#隐藏箭头显示
turtle.pensize()
#绘制图形时的宽度

樱花树的躯干

# 画樱花的躯干(60,turtle)
def Tree(branch, turtle):
    time.sleep(0.0005)
    if branch > 3:
        if 8 <= branch <= 12:
            if random.randint(0, 2) == 0:
                turtle.color('snow') 
            else:
                turtle.color('lightcoral')  
            turtle.pensize(branch / 3)
        elif branch < 8:
            if random.randint(0, 1) == 0:
                turtle.color('snow')
            else:
                turtle.color('lightcoral')  
            turtle.pensize(branch / 2)
        else:
            turtle.color('sienna')  
            turtle.pensize(branch / 10)  # 6
        turtle.forward(branch)
        a = 1.5 * random.random()
        turtle.right(20 * a)
        b = 1.5 * random.random()
        Tree(branch - 10 * b,turtle)
        turtle.left(40 * a)
        Tree(branch - 10 * b, turtle)
        turtle.right(20 * a)
        turtle.up()
        turtle.backward(branch)
        turtle.down()

掉落的花般

# 掉落的花瓣
def Petal(m, t):
    for i in range(m):
        a = 200 - 400 * random.random()
        b = 10 - 20 * random.random()
        turtle.up()
        turtle.forward(b)
        turtle.left(90)
        turtle.forward(a)
        turtle.down()
        turtle.color('lightcoral')  # 淡珊瑚色
        turtle.circle(1)
        turtle.up()
        turtle.backward(a)
        turtle.right(90)
        turtle.backward(b)

运行部分

turtle.hideturtle()  # 隐藏画笔
turtle.getscreen().tracer(5, 0)
#w.screensize(bg='wheat')   wheat小麦
turtle.left(90)
turtle.up()
turtle.backward(150)
turtle.down()
turtle.color('sienna')
# 树干
Tree(60, turtle)
# 掉落的花瓣
Petal(200, turtle)
w.exitonclick()

总代码

import turtle
import random
import time

# 画樱花的躯干(60,turtle)
def Tree(branch, turtle):
    time.sleep(0.0005)
    if branch > 3:
        if 8 <= branch <= 12:
            if random.randint(0, 2) == 0:
                turtle.color('snow') 
            else:
                turtle.color('lightcoral')  
            turtle.pensize(branch / 3)
        elif branch < 8:
            if random.randint(0, 1) == 0:
                turtle.color('snow')
            else:
                turtle.color('lightcoral')  
            turtle.pensize(branch / 2)
        else:
            turtle.color('sienna')  
            turtle.pensize(branch / 10)  # 6
        turtle.forward(branch)
        a = 1.5 * random.random()
        turtle.right(20 * a)
        b = 1.5 * random.random()
        Tree(branch - 10 * b,turtle)
        turtle.left(40 * a)
        Tree(branch - 10 * b, turtle)
        turtle.right(20 * a)
        turtle.up()
        turtle.backward(branch)
        turtle.down()

# 掉落的花瓣
def Petal(m, t):
    for i in range(m):
        a = 200 - 400 * random.random()
        b = 10 - 20 * random.random()
        turtle.up()
        turtle.forward(b)
        turtle.left(90)
        turtle.forward(a)
        turtle.down()
        turtle.color('lightcoral')  # 淡珊瑚色
        turtle.circle(1)
        turtle.up()
        turtle.backward(a)
        turtle.right(90)
        turtle.backward(b)


turtle.hideturtle()  # 隐藏画笔
turtle.getscreen().tracer(5, 0)
#w.screensize(bg='wheat')   wheat小麦
turtle.left(90)
turtle.up()
turtle.backward(150)
turtle.down()
turtle.color('sienna')
# 树干
Tree(60, turtle)
# 掉落的花瓣
Petal(200, turtle)
w.exitonclick()

本文地址:https://blog.csdn.net/skywalle/article/details/107582390