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 turtle Library to draw Christmas Tree in python

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

Share

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

This article mainly explains "how to use turtle library to draw Christmas tree in python". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to draw Christmas tree with turtle library in python".

The figure drawn is as follows:

What is Turtle?

Turtle library is a very popular function library for drawing images in Python language. imagine a small tortoise, starting at the origin of a coordinate system with horizontal axis x and vertical axis y, and moving in this plane coordinate system according to the control of a set of function instructions, thus drawing a figure on its crawling path.

Second, use step 1. Import the library

The code is as follows:

Import turtle as t # as is an alias, and the subsequent calls to t are the basics of turtlefrom turtle import * import random as r2.turtle drawing.

(1) Brush Motion Command

(2) Brush control command

(3) Global control command

(4) Global control command

Third, Christmas tree code

The code is as follows:

# TangYiJia 2021/12/15import turtle as t # as is an alias. All subsequent calls are turtlefrom turtle import * import random as rn = 100.0speed (1000) # define speed pensize (5) # brush width screensize (800,800, bg='black') # define background color You can change your own color left (90) forward (250) # height color ("orange", "yellow") # defines the color of the uppermost star, and the outer circle is orange Inside is yellowbegin_fill () left (126) for i in range (5): # draw pentagram forward (n / 5) right (144C) # Angle of pentagram forward (n / 5) left (72) # continue to change angle end_fill () right (126) def drawlight (): # define the method of drawing colored lights if r.randint (0,50) = 0: # if you think there are too many colored lights We can enlarge the range of values. The corresponding lights will be less color ('tomato') # define the first color circle (3) # define the color lamp size elif r.randint (0,30) = = 1: color (' orange') # define the second color circle (4) # define the color lamp size elif r.randint (0 50) = 2: color ('blue') # define a third color circle (2) # define lamp size elif r.randint (0 30) = 3: color ('white') # define the fourth color circle (4) # define the size of the lights else: color (' dark green') # draw empty branches in the case of the rest of the random numbers color ("dark green") # define the color of the branches backward (n * 4.8def tree (d, s): # start drawing the tree if d

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