python3实现用turtle模块画一棵随机樱花树

作者:执笔写回憶 时间:2023-11-30 06:04:07 

废话不多说了,直接上代码吧!


#!/usr/bin/env python
# coding=utf-8
# 画一棵樱花

import turtle
import random
from turtle import *
from time import sleep

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

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

def main():
 # 绘图区域
 t = turtle.Turtle()
 # 画布大小
 w = turtle.Screen()
 t.hideturtle() # 隐藏画笔
 getscreen().tracer(5,0)
 w.screensize(bg='wheat') # wheat小麦
 t.left(90)
 t.up()
 t.backward(150)
 t.down()
 t.color('sienna')

# 画樱花的躯干
 tree(60,t)
 # 掉落的花瓣
 petal(200, t)
 w.exitonclick()

main()

python3实现用turtle模块画一棵随机樱花树

来源:https://blog.csdn.net/z564359805/article/details/85861481

标签:python3,turtle,樱花树
0
投稿

猜你喜欢

  • Python实现搜索算法的实例代码

    2023-08-09 14:24:59
  • pytorch:torch.mm()和torch.matmul()的使用

    2022-05-25 21:32:40
  • Python 创建新文件时避免覆盖已有的同名文件的解决方法

    2023-08-23 23:44:46
  • python获取图片颜色信息的方法

    2023-11-10 16:40:41
  • AES算法 asp源码

    2009-08-28 13:05:00
  • pycharm 终端部启用虚拟环境详情

    2022-04-01 17:52:59
  • Python time模块之时间戳与结构化时间的使用

    2024-01-02 09:07:51
  • Tensorflow--取tensorf指定列的操作方式

    2021-06-25 21:21:02
  • 不要使用@import[译]

    2009-05-01 12:01:00
  • python 动态导入模块实现模块热更新的方法

    2022-07-06 17:37:56
  • Django项目在pycharm新建的步骤方法

    2021-01-19 11:46:32
  • Git 教程之服务器搭建详解

    2022-07-28 06:44:20
  • javascript获取ckeditor编辑器的值(实现代码)

    2024-04-16 09:12:54
  • Vue + Node.js + MongoDB图片上传组件实现图片预览和删除功能详解

    2024-05-10 14:14:56
  • Python读取及保存mat文件的注意事项说明

    2022-01-10 13:25:54
  • 使用Python实现分别输出每个数组

    2021-10-30 00:28:49
  • MySQL Left JOIN时指定NULL列返回特定值详解

    2024-01-16 09:55:32
  • oracle 11g的警告日志和监听日志的删除方法

    2023-06-29 13:59:36
  • js设计模式之单例模式原理与用法详解

    2024-04-29 14:09:13
  • CSS滤镜示范(filter)附源代码(静态滤镜)

    2008-05-18 12:42:00
  • asp之家 网络编程 m.aspxhome.com