python用turtle库绘画圣诞树
大家好,本篇文章主要讲的是python用turtle库绘画圣诞树,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
目录
前言
一、Turtle是什么?
二、使用步骤
1.引入库
2.turtle绘图的基础知识
三、圣诞树代码
总结
前言
圣诞节快到了,是不是想用python画一个可爱的圣诞树,我在各大网站都查了一下,都不太美观,然后我就学习了一下别人的代码改写了一下,自己加了一些东西,弄的好看一些了,给大家开源出来,欢迎大家指正学习,也欢迎转载,请注明出处哦~
画出来的图形如图:
一、Turtle是什么?
Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。
二、使用步骤
1.引入库
代码如下:
1 2 3 | import turtle as t # as就是取个别名,后续调用的t都是turtle from turtle import * import random as r |
2.turtle绘图的基础知识
(1) 画笔运动命令
(2) 画笔控制命令
(3) 全局控制命令
(4) 全局控制命令
三、圣诞树代码
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | # 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() # 完成,否则会直接关闭 |
总结
这就是基本的代码啦,我代码写的也不是很流畅,欢迎大家评论指正学习!
到此这篇关于python用turtle库绘画圣诞树的文章就介绍到这了
原文链接:https://blog.csdn.net/TYJhhxx/article/details/121962889