后来,唐李钰曾说过,樱花在春天会昏昏欲睡,在秋千架下又会回来。滴落的暗斜的月亮晚了,花儿开在枝头。当樱花落下时,春天就结束了,当它回到秋千下的时候。天空中的斜月已经晚了,花儿还在枝头。
[En]
Later Tang Li Yu once said that the cherry blossoms would fall sleepy in spring and return under the swing frame. The leaking dark oblique moon is late, and the flowers are in the branches. When the cherry blossoms fall, spring will be over, and when it goes back under the swing. The oblique moon in the sky is late, and the flowers are still on the branches.
关于python画图相关的,我们一直使用的是turtle来画,用专业的非标准库来做专业的事儿。将需要使用到的内置库或者非标准库全部都导入到当前的代码块中。
from time import sleep # 用于控制程序运行时的阻塞停顿
from turtle import * # 绘图相关接口
import random # 生成随机数
设置需要全局初始化的对象以及相关的全局参数设置,这样就不必在以后的每个笔划中设置它们。
[En]
Set the objects that need to be initialized globally, as well as the relevant global parameter settings, so that you don’t have to set them with each stroke later.
但是画布上有一个箭头是无法消除的,有一个知道的老板请在上面留言解决,非常感谢!
[En]
But there is an arrow on the canvas can not be eliminated, there is a know boss please leave a message on the solution, thank you very much!
tle = Turtle() # 初始化小乌龟画笔对象
tle.hideturtle() # 全局隐藏画笔
tle.clear() # 清除屏幕
tle.speed(0) # 设置画笔速度为0,也就是最快的画笔速度
scr = Screen() # 初始化屏幕对象
getscreen().tracer(30, 0) # 返回一个TurtleScreen类的绘图对象,并开启绘画,加速绘画过程
scr.screensize(bg='white') # 设置背景颜色
开始绘制并在开始时设置与画笔相关的属性或参数。
[En]
Start painting and set brush-related properties or parameters at the beginning.
tle.left(90)
tle.up()
tle.backward(150)
tle.down()
tle.color('sienna')
接下来是编写两个函数,一个是用于生成树枝/树干并在对应的树枝旁边画上樱花。另一个则是在樱花树的地上铺满掉落的樱花,这样的思路来源于CSDN博客平台,向大佬递茶!
编写画樱花树的树枝/树干部分/树枝樱花的函数draw_branch_flower。
def draw_branch_flower(len):
'''
绘制树枝/花函数
:param len: 树枝长度
:return:
'''
sleep(0.0005)
if len > 3:
if 8
编写画樱花树旁边的地上的花瓣函数draw_ground_flower。
def draw_ground_flower(flower_num):
'''
绘制地上掉落的花瓣
:param flower_num: 花瓣数量
:return:
'''
for i in range(flower_num):
a = 200 - 400 * random.random()
b = 10 - 20 * random.random()
tle.up()
tle.forward(b)
tle.left(90)
tle.forward(a)
tle.down()
tle.color('lightcoral')
tle.circle(1)
tle.up()
tle.backward(a)
tle.right(90)
tle.backward(b)
调用樱花树相关绘制函数,快速完成绘制过程。
[En]
Call the drawing function related to the cherry blossom tree to quickly complete the drawing process.
draw_branch_flower(60)
draw_ground_flower(200)
使用exitonclick函数保持在绘画完成时,窗口不会直接关闭。
scr.exitonclick()
以上是画樱花树的全过程。请回复‘1001’,获取公众号中的完整代码块文件并直接运行。
[En]
The above is the whole process of drawing the cherry blossom tree. Please reply ‘1001’ to get the complete code block file in the official account and run it directly.
【往期精彩】
Python 读取PDF文件为文本字符并转换为音频…
python 获取最新房价信息-以北京房价为例
办公自动化:Image图片转换成PDF文档存储…
python做一个微型美颜图片处理器,十行代码即可完成…
用python做一个文本翻译器,自动将中文翻译成英文,超方便的!
小王,给这2000个客户发一下节日祝福的邮件…
python 一行命令开启网络间的文件共享…
PyQt5 批量删除 Excel 重复数据,多个文件、自定义重复项一键删除…
再见XShell,这款国人开源的终端命令行工具更nice!
python 表情包下载器,轻松下载上万个表情包、斗图不用愁…
Python 自动清理电脑垃圾文件,一键启动即可…
有了jmespath,处理python中的json数据就变成了一种享受…
Original: https://www.cnblogs.com/lwsbc/p/16167449.html
Author: Python集中营
Title: 又是樱花盛开的季节,使用小乌龟来画一颗樱花树吧
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/499872/
转载文章受原作者版权保护。转载请注明原作者出处!