Use pythonturtle to draw simple patterns, and python uses the turtle library to draw tree shapes

This article mainly introduces drawing simple patterns with pythonturtle, which has certain reference value, and friends who need it can refer to it. I hope that you will gain a lot after reading this article. Let the editor take you to understand it together.

Recently, many readers are more interested in python's turtle library. Draw a rose with python for you. I also wrote an article about how to draw a rose. PHP and PYTHON: based on syntax, function, application, etc. Compare in detail . Today I found a python program that uses turtle to draw trees on Zhihu, let’s take a look```*``

In fact, this kind of program only needs to understand the general idea. If you are interested, you can familiarize yourself with the usage and function of each function in the turtle. I have also introduced some in the article on drawing roses. You can refer to it below sci paper weight reduction skills .

Let me first talk about the general idea of ​​painting this tree. First of all, this painting can be divided into two parts, one part is the tree, and the other part is the petals on the ground. The key point is how to draw the tree. The tree can be divided into leaves and branches. Each branch can be divided into two branches at most, and the leaves have two colors.

The thickness of the branches can be controlled by the size of the brush, and the color of the leaves can be controlled by the color of the brush. In this way, we only need to confirm the size and color of the brush at each position.

Different branches or different leaves can be realized by the same method, but the position is different, so in the program we use recursion to achieve, through recursion to generate a large number of branches and leaves, each recursion will generate branches in different positions and leaves.

The petals under the tree are easy, and it can be done in one cycle, and the brush moves randomly up, down, left, and right for each cycle.

The code for drawing branches and leaves is posted below. You can compare it with the ideas provided above. For the complete code, please reply to the keyword "tree" in the background of this official account to get it.

#python The main implementation method of drawing trees

#branchLen represents the length of the branch, t represents the turtle object

def tree(branchLen, t):

if branchLen > 2:

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')

t.pensize(branchLen / 10)

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

Guess you like

Origin blog.csdn.net/mynote/article/details/132702589