Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python Turtle to draw a cherry tree for yourself

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to use Python Turtle to draw a cherry tree for yourself, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

one。 Draw step 1. Environmental installation

You can refer to the previous blog:

Python crawler can grab the information of movie website and store it in the database.

two。 Picture beautification

Did not use Python much before, carry on the analysis and effect optimization to other people's code, learn by the way.

This is the original effect picture, a little gray, slightly changed to make it a little brighter.

The modified code:

From turtle import * from random import * from math import * def tree (NMagnel): pd () # Shadow effect t = cos (radians (heading () + 45)) / 8 minutes 0.25 pencolor (tmemt) T) pensize (nstroke 3) forward (l) # draw the branch if n > 0: B = random () * 15cm 10 # right branch deflection angle c = random () * 15cm 10 # left branch deflection angle d = l * (random () * 0.25mm 0.7) # the length of the next branch # turn right to a certain angle, draw the right branch right (b) tree D) # turn left at a certain angle Draw the left branch left (Bracc) tree (nMube 1) # turn back right (c) else: # draw the leaf right (90) n=cos (radians (heading ()-45)) / 4 minutes 0.5 ran=random () # here compared to the original randomly added filled circle Make the cherry leaves look a little more if (ran > 0. 7): begin_fill () circle (3) fillcolor ('pink') # replace the randomly generated leaves with a uniform pink pencolor ("pink") circle (3) if (ran > 0.7): end_fill () Left (90) # add 0.3x falling leaves if (random () > 0.7): pu () # falling t = heading () an =-40 + random () * 40 setheading (an) dis = int (800*random () * 0.5 + 400*random () * 0.3 + 200*random () * 0.2) forward (dis) setheading (t) # draw leaves pd () right (90) n = cos (radians (heading ()-45)) / 4cm 0.5 pencolor Left (90) pu () # returns t=heading () setheading (an) backward (dis) setheading (t) pu () backward (l) # returns bgcolor (0.956, 0.9255, 0.9882) # sets the background color (changes gray to lavender) ht () # Hidden turtlespeed (0) # Speed 1-10 Progressive 0 fastest tracer (0) pu () # hold pen backward (50) left (90) # turn left 90 degrees pu () # hold pen backward (300) # back 300tree (12100) # Recursive 7 layer done ()

After that, the effect is a little warmer, and I like this more warm tone:

two。 Technical Learning 1. Introduction to turtle

The drawing effect of cherry trees is achieved through turtle in Python. The power of Python is that there are many powerful libraries, one of which is turtle, which is a library that comes with Python.

2. Basic knowledge of turtle drawing

Turtle library drawing principle: there is a sea turtle in the center of the form, walking upstream of the canvas, passing through the track to form a drawn figure, the sea turtle is controlled by the program, and can freely change color, direction width and so on.

(1). Turtle drawing form

Setup is used to set the size of the form. The last two parameters are optional.

Turtle.setup (width,height,startx,starty)

(2). Coordinate system of turtle

On the canvas, by default, there is a coordinate origin that is the coordinate axis of the center of the canvas, and there is a small tortoise facing the positive direction of the x-axis on the coordinate origin.

Here we use two words to describe the little tortoise: the coordinate origin (position), facing the positive direction of the x-axis (direction). In turtle drawing, the position direction is used to describe the state of the little tortoise (brush).

(3). Turtle Brush Properties

(4). Turtle drawing command

Motion command

Control command

Global control command

The above is all the contents of the article "how to use Python Turtle to draw a cherry tree for yourself". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report