python用turtle库绘画圣诞树

作者:小唐YiJiaTang 时间:2023-05-24 19:35:40 

前言

圣诞节快到了,是不是想用python画一个可爱的圣诞树,我在各大网站都查了一下,都不太美观,然后我就学习了一下别人的代码改写了一下,自己加了一些东西,弄的好看一些了,给大家开源出来,欢迎大家指正学习,也欢迎转载,请注明出处哦~

画出来的图形如图:

python用turtle库绘画圣诞树

一、Turtle是什么?

Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。

二、使用步骤

1.引入库

代码如下:


import turtle as t  # as就是取个别名,后续调用的t都是turtle
from turtle import *
import random as r

2.turtle绘图的基础知识

(1) 画笔运动命令

python用turtle库绘画圣诞树

(2) 画笔控制命令

python用turtle库绘画圣诞树

(3) 全局控制命令

python用turtle库绘画圣诞树

(4) 全局控制命令

python用turtle库绘画圣诞树

三、圣诞树代码

代码如下:


# TangYiJia 2021/12/15

import turtle as t  # as就是取个别名,后续调用的t都是turtle
from turtle import *
import random as r

n = 100.0

speed(1000)  # 定义速度
pensize(5)  # 画笔宽度
screensize(800, 800, bg='black')  # 定义背景颜色,可以自己换颜色
left(90)
forward(250)              # 开始的高度
color("orange", "yellow")  # 定义最上端星星的颜色,外圈是orange,内部是yellow
begin_fill()
left(126)

for i in range(5):  # 画五角星
   forward(n / 5)
   right(144)  # 五角星的角度
   forward(n / 5)
   left(72)  # 继续换角度
end_fill()
right(126)

def drawlight():  # 定义画彩灯的方法
   if r.randint(0, 50) == 0:  # 如果觉得彩灯太多,可以把取值范围加大一些,对应的灯就会少一些
       color('tomato')  # 定义第一种颜色
       circle(3)  # 定义彩灯大小
   elif r.randint(0, 30) == 1:
       color('orange')  # 定义第二种颜色
       circle(4)  # 定义彩灯大小
   elif r.randint(0, 50) == 2:
       color('blue')  # 定义第三种颜色
       circle(2)  # 定义彩灯大小
   elif r.randint(0, 30) == 3:
       color('white')  # 定义第四种颜色
       circle(4)  # 定义彩灯大小
   else:
       color('dark green')  # 其余的随机数情况下画空的树枝

color("dark green")  # 定义树枝的颜色
backward(n * 4.8)

def tree(d, s):  # 开始画树
   if d <= 0: return
   forward(s)
   tree(d - 1, s * .8)
   right(120)
   tree(d - 3, s * .5)
   drawlight()  # 同时调用小彩灯的方法
   right(120)
   tree(d - 3, s * .5)
   right(120)
   backward(s)

tree(15, 100)
backward(50)

for i in range(200):  # 循环画最底端的小装饰
   a = 200 - 400 * r.random()
   b = 10 - 20 * r.random()
   up()
   forward(b)
   left(90)
   forward(a)
   down()
   if r.randint(0, 1) == 0:
       color('tomato')
   else:
       color('wheat')
   circle(2)
   up()
   backward(a)
   right(90)
   backward(b)

def drawsnowman(n,m,a,b):  # 画雪人 (n,m)是头和身子交点的坐标,a是头的大小,m是身体的大小
   t.goto(n, m)
   t.pencolor("white")
   t.pensize(2)
   t.fillcolor("white")
   t.seth(0)
   t.begin_fill()
   t.circle(a)
   t.end_fill()
   t.seth(180)
   t.begin_fill()
   t.circle(b)
   t.end_fill()
   t.pencolor("black")
   t.fillcolor("black")
   t.penup()    # 右眼睛
   t.goto(n-a/4, m+a)
   t.seth(0)
   t.pendown()
   t.begin_fill()
   t.circle(2)
   t.end_fill()
   t.penup()    # 左眼睛
   t.goto(n+a/4, m+a)
   t.seth(0)
   t.pendown()
   t.begin_fill()
   t.circle(2)
   t.end_fill()
   t.penup()  # 画嘴巴
   t.goto(n, m+a/2)
   t.seth(0)
   t.pendown()
   t.fd(5)
   t.penup()       # 画扣子
   t.pencolor("red")
   t.fillcolor("red")
   t.goto(n, m-b/4)
   t.pendown()
   t.begin_fill()
   t.circle(2)
   t.end_fill()
   t.penup()
   t.pencolor("yellow")
   t.fillcolor("yellow")
   t.goto(n, m-b/2)
   t.pendown()
   t.begin_fill()
   t.circle(2)
   t.end_fill()
   t.penup()
   t.pencolor("orange")
   t.fillcolor("orange")
   t.goto(n, m-(3*b)/4)
   t.pendown()
   t.begin_fill()
   t.circle(2)
   t.end_fill()

drawsnowman(-200, -200, 20, 30)
drawsnowman(-250, -200, 30, 40)

t.up()
t.goto(100, 200)
t.down()
t.color("dark red", "red")  # 定义字体颜色
t.penup()
t.write("小唐's Christmas Tree", font=("Comic Sans MS", 16, "bold"))  # 定义文字、位置、字体、大小
t.end_fill()

def drawsnow():  # 画雪花
   t.ht()  # 隐藏笔头,ht=hideturtle
   t.pensize(2)  # 定义笔头大小
   for i in range(200):  # 画多少雪花
       t.pencolor("white")  # 定义画笔颜色为白色,其实就是雪花为白色
       t.pu()  # 提笔,pu=penup
       t.setx(r.randint(-350, 350))  # 定义x坐标,随机从-350到350之间选择
       t.sety(r.randint(-100, 350))  # 定义y坐标,注意雪花一般在地上不会落下,所以不会从太小的纵座轴开始
       t.pd()  # 落笔,pd=pendown
       dens = 6  # 雪花瓣数设为6
       snowsize = r.randint(1, 10)  # 定义雪花大小
       for j in range(dens):  # 就是6,那就是画5次,也就是一个雪花五角星
           # t.forward(int(snowsize))  #int()取整数
           t.fd(int(snowsize))
           t.backward(int(snowsize))
           # t.bd(int(snowsize))  #注意没有bd=backward,但有fd=forward,小bug
           t.right(int(360 / dens))  # 转动角度

drawsnow()  # 调用画雪花的方法
t.done()  # 完成,否则会直接关闭

来源:https://blog.csdn.net/TYJhhxx/article/details/121962889

标签:python,圣诞树
0
投稿

猜你喜欢

  • 让ExtJs的combobox不显示HTML……

    2009-05-31 17:01:00
  • python中验证码连通域分割的方法详解

    2022-09-30 11:04:00
  • python实现数组求和与平均值

    2021-09-27 07:15:49
  • javascript 获取select下拉列表值的代码

    2024-04-19 09:50:04
  • Python中Dict两种实现的原理详解

    2022-11-13 06:20:29
  • VScode查看python f.write()的文件乱码问题及解决方法

    2023-01-25 19:02:10
  • numpy实现RNN原理实现

    2023-09-21 23:47:33
  • python生成器/yield协程/gevent写简单的图片下载器功能示例

    2022-02-25 10:08:03
  • 注册表单之死

    2008-08-07 13:02:00
  • Mysql误删除DELETE数据找回操作指南

    2024-01-16 20:48:18
  • 分步骤教你用python一步步提取PPT中的图片

    2023-07-01 19:58:22
  • Python使用爬虫猜密码

    2023-03-21 12:38:39
  • 老生常谈Python之装饰器、迭代器和生成器

    2023-04-08 08:28:19
  • Python urlopen 使用小示例

    2022-08-23 19:54:55
  • 利用索引提高SQL Server数据处理的效率

    2009-01-08 15:32:00
  • 一文学习MySQL 意向共享锁、意向排他锁、死锁

    2024-01-21 23:17:27
  • 解决TensorFlow模型恢复报错的问题

    2022-06-15 16:24:31
  • python中类的属性和方法介绍

    2023-10-22 05:14:47
  • AlmaLinux 9 安装 MySQL 8.0.32的详细过程

    2024-01-21 21:38:36
  • python super的使用方法及实例详解

    2023-08-19 11:04:49
  • asp之家 网络编程 m.aspxhome.com