no classification
no tag
no datas
posted on 2023-06-06 09:47 read(835) comment(0) like(23) collect(3)
Table of contents
Come, come, come, friends, come and get the colorful petal shower! !
It's a cliché, before using python to draw cherry blossom trees, let's learn about turtle first!
Turtle is an important package (built-in package) for drawing in Python, which contains rich drawing tools and various drawing functions. After you learn to draw with Turtle, you can draw any pattern you want oh.
1.1 Turtle drawing board
Turtle's artboard size can be set with the turtle.setup() function
turtle.setup(width,height)
Set the size of the artboard, including width and height, where width is width and height is height.
1.2 Turtle Brushes
Turtle's brush has several commonly used functions:
①turtle.penup(): Lift up the paintbrush, moving the brush at this time will not leave marks on the canvas
②turtle.pendown(): Put down the paintbrush, corresponding to turtle.penup. Drawing after painting will leave traces on the canvas)
③turtle.pensize(): Control the size of the brush (you can define the size of the brush according to your needs)
④turtle.pencolor(): Control the color of the brush (you can check all of them online The colors that can be used in python, there are many colors that can be used in python)
⑤turtle.hideturtle(): hide the brush (after hiding the brush, the brush will not be visible when drawing)
1.3 Turtle drawing
In the process of drawing, we often use some simple movement functions:
①turtle.forward(x): Move the brush forward x pixels (x can be understood as a distance)
②turtle.backward(x): Move the brush backward x pixels (x can be understood as a distance)
③turtle.left(n): Rotate the brush to the left by n degrees
④turtle.right(n): Rotate the brush to the right by n degrees
⑤turtle.speed(): Set the speed of the brush drawing (1~10 increments, 0 is the fastest)
1.4 Turtle coloring
After drawing a picture, we often need to fill it with color. Here we can use the turtle.fillcolor() function, and write the color you want to fill in the brackets.
Pay attention to its basic format when using the turtle.fillcolor() function:
turtle.beginfill() #Start filling
turtle.fillcolor() #Enter the color of filling
turtle.endfill() #End filling
1.5 Turtle writing
在完成整个画图后,我们可以使用turtle.write()函数进行写字
turtle.write(" ",move,align,font)
① 第一个位置双引号内填入要写的字
② move(可选):在默认情况下,move为false。如果move为true,则笔将移动到右下角
③ align(可选):可取值是left即左、center即中、right即右之一,是字符串格式
④ font(可选):字体三元组(fontname、fontsize、fonttype),fontname即字体名称(字符串格式,如“宋体”),fontsize即字体大小),fonttype即字体类型如:normal(普通)、bold(粗体)、italic(斜体)
- class Flower(): #每个花朵(花朵类)
- def __init__(self):
- self.r = ra.randint(8,12) #花朵的半径
- self.x = ra.randint(-1000,1000) #花朵的横坐标
- self.y = ra.randint(-500,500) #花朵的纵坐标
- self.f = ra.uniform(-3.14,3.14) #花朵左右移动呈正弦函数
- self.speed = ra.randint(5,10) #花朵移动速度
- self.color = ra.choice(colors) #花朵的颜色
- self.outline = 1 #花朵的外框大小(可不要)
这段代码是定义了一个名为Flower的类,该类描述了花朵对象的属性和行为。在该类的初始化函数__init__中,定义了花朵的半径、横纵坐标、左右移动方向、移动速度、颜色和外框大小等属性,并通过产生随机数来赋值。这些属性可以用于绘制花朵图像或控制花朵的运动轨迹。Flower类的定义使得在之后的代码中可以更方便地创建和操作花朵对象。
- def move(self): #花朵移动函数
- if self.y >= -500: #当花朵还在画布中时
- self.y -= self.speed #设置上下移动速度
- self.x += self.speed * math.sin(self.f) #设置左右移动速度
- self.f += 0.1 #可以理解成标志,改变左右移动的方向
- else: #当花朵漂出了画布时,重新生成一个花朵
- self.r = ra.randint(8,12)
- self.x = ra.randint(-1000,1000)
- self.y = 500
- self.f = ra.uniform(-3.14,3.14)
- self.speed = ra.randint(5,10)
- self.color = ra.choice(colors)
- self.outline = 1
这段代码定义了一个move函数,描述花朵对象的运动方式。如果花朵仍在画布中(y >= -500),则花朵会以设定的速度(speed)向上移动,并以设定的左右移动方向(f)左右摆动。左右移动的方向变化由f值的增加控制(f += 0.1)。如果花朵漂出了画布,则通过产生随机数重新生成一个花朵对象,以保证画布上始终有花朵。这样,每个花朵对象都具有独立的属性和行为,并且可以在画布上自由运动。
- def draw(self): #画花朵函数,就是用turtle画花朵
- t.penup()
- t.goto(self.x,self.y)
- t.setheading(self.x)
- t.pendown()
- t.left(36)
- t.color(self.color)
- t.begin_fill()
- t.fillcolor(self.color)
- for i in range(5):
- t.left(-72)
- t.circle(self.r,extent=144)
- t.end_fill()
- #t.right(36)
- #t.begin_fill()
- #t.fillcolor("red")
- #t.color("white")
- #t.circle(12)
- #t.end_fill()
This code defines a function called draw, which uses the turtle library to draw flowers on the canvas. In this function, first position the flower to be drawn on the canvas by setting the position and angle of the pen. Next, paint the pentagonal petals within the petals by setting the brush color and fill color. Among them, the details of drawing pentagonal petals are realized through the loop statement and the arc function circle(). Finally, use the end_fill() function to fill in the color inside the flower. By calling this function, each flower can be displayed on the canvas, and attributes such as color and shape are randomly generated.
It's almost June 1st, how are you guys going to spend your day?
Author:Soledad
link:http://www.pythonblackhole.com/blog/article/79584/f48d7517ed1b83dd5aa9/
source:python black hole net
Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.
name:
Comment content: (supports up to 255 characters)
Copyright © 2018-2021 python black hole network All Rights Reserved All rights reserved, and all rights reserved.京ICP备18063182号-7
For complaints and reports, and advertising cooperation, please contact vgs_info@163.com or QQ3083709327
Disclaimer: All articles on the website are uploaded by users and are only for readers' learning and communication use, and commercial use is prohibited. If the article involves pornography, reactionary, infringement and other illegal information, please report it to us and we will delete it immediately after verification!