News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

no datas

Python colorful flowers

posted on 2023-06-06 09:47     read(835)     comment(0)     like(23)     collect(3)


Table of contents

foreword

little turtle

flowers

move function 

draw flowers 

end


foreword

Come, come, come, friends, come and get the colorful petal shower! !

little turtle

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(斜体)

花朵类

  1. class Flower(): #每个花朵(花朵类)
  2. def __init__(self):
  3. self.r = ra.randint(8,12) #花朵的半径
  4. self.x = ra.randint(-1000,1000) #花朵的横坐标
  5. self.y = ra.randint(-500,500) #花朵的纵坐标
  6. self.f = ra.uniform(-3.14,3.14) #花朵左右移动呈正弦函数
  7. self.speed = ra.randint(5,10) #花朵移动速度
  8. self.color = ra.choice(colors) #花朵的颜色
  9. self.outline = 1 #花朵的外框大小(可不要)

这段代码是定义了一个名为Flower的类,该类描述了花朵对象的属性和行为。在该类的初始化函数__init__中,定义了花朵的半径、横纵坐标、左右移动方向、移动速度、颜色和外框大小等属性,并通过产生随机数来赋值。这些属性可以用于绘制花朵图像或控制花朵的运动轨迹。Flower类的定义使得在之后的代码中可以更方便地创建和操作花朵对象。 

移动函数 

  1. def move(self): #花朵移动函数
  2. if self.y >= -500: #当花朵还在画布中时
  3. self.y -= self.speed #设置上下移动速度
  4. self.x += self.speed * math.sin(self.f) #设置左右移动速度
  5. self.f += 0.1 #可以理解成标志,改变左右移动的方向
  6. else: #当花朵漂出了画布时,重新生成一个花朵
  7. self.r = ra.randint(8,12)
  8. self.x = ra.randint(-1000,1000)
  9. self.y = 500
  10. self.f = ra.uniform(-3.14,3.14)
  11. self.speed = ra.randint(5,10)
  12. self.color = ra.choice(colors)
  13. self.outline = 1

这段代码定义了一个move函数,描述花朵对象的运动方式。如果花朵仍在画布中(y >= -500),则花朵会以设定的速度(speed)向上移动,并以设定的左右移动方向(f)左右摆动。左右移动的方向变化由f值的增加控制(f += 0.1)。如果花朵漂出了画布,则通过产生随机数重新生成一个花朵对象,以保证画布上始终有花朵。这样,每个花朵对象都具有独立的属性和行为,并且可以在画布上自由运动。 

画花朵 

  1. def draw(self): #画花朵函数,就是用turtle画花朵
  2. t.penup()
  3. t.goto(self.x,self.y)
  4. t.setheading(self.x)
  5. t.pendown()
  6. t.left(36)
  7. t.color(self.color)
  8. t.begin_fill()
  9. t.fillcolor(self.color)
  10. for i in range(5):
  11. t.left(-72)
  12. t.circle(self.r,extent=144)
  13. t.end_fill()
  14. #t.right(36)
  15. #t.begin_fill()
  16. #t.fillcolor("red")
  17. #t.color("white")
  18. #t.circle(12)
  19. #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. 

end

It's almost June 1st, how are you guys going to spend your day?



Category of website: technical article > Blog

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.

23 0
collect article
collected

Comment content: (supports up to 255 characters)