How to Draw a Simple Tree in Python Turtle

preface

The annual Christmas is coming soon. Many children in the circle of friends have begun to show off their Christmas trees. Today, I'd like to share with you how to draw a Christmas tree with the turtle library through pythoy.

The turtle library is a very popular function library for drawing images in Python language. Imagine a little turtle starting at the origin (0,0) of a coordinate system with a horizontal axis of X and a vertical axis of Y. it moves in this plane coordinate system according to the control of a group of function instructions, so as to draw graphics on its crawling path.

1. Square Christmas tree

                import turtle screen = turtle.Screen() screen.setup(375, 700) circle = turtle.Turtle() circle.shape('circle') circle.color('red') circle.speed('fastest') circle.up() square = turtle.Turtle() square.shape('square') square.color('green') square.speed('fastest') square.up() circle.goto(0, 280) circle.stamp() k = 0 for i in range(1, 13):     y = 30 * i     for j in range(i - k):         x = 30 * j         square.goto(x, -y + 280)         square.stamp()         square.goto(-x, -y + 280)         square.stamp() if i % 4 == 0:     x = 30 * (j + 1)     circle.color('red')     circle.goto(-x, -y + 280)     circle.stamp()     circle.goto(x, -y + 280)     circle.stamp()     k += 3 if i % 4 == 3:     x = 30 * (j + 1)     circle.color('yellow')     circle.goto(-x, -y + 280)     circle.stamp()     circle.goto(x, -y + 280)     circle.stamp() square.color('brown') for i in range(13, 17):     y = 30 * i     for j in range(2):         x = 30 * j         square.goto(x, -y + 280)         square.stamp()         square.goto(-x, -y + 280)         square.stamp()              

2. Line Christmas tree

                import turtle  #Define the green leaf function of the Christmas tree def tree(d, s):     if d <= 0:         return     turtle.forward(s)     tree(d - 1, s * .8)     turtle.right(120)     tree(d - 3, s * .5)     turtle.right(120)     tree(d - 3, s * .5)     turtle.right(120)     turtle.backward(s) n = 100 "" "set drawing speed" "" 'fastest' : 0 'fast'  : 10 'normal' : 6 'slow'  : 3 'slowest' : 1 """ turtle. Speed ('fast ') # sets the speed  turtle.left(90) turtle.forward(3 * n) turtle.color("orange", "yellow") turtle.left(126)  # turtle.begin_fill() for i in range(5):     turtle.forward(n / 5)     turtle.right(144)     turtle.forward(n / 5)     turtle.left(72)     turtle.end_fill() turtle.right(126) turtle.color("dark green") turtle.backward(n * 4.8)  #Execution function tree(15, n) turtle.backward(n / 5)              

3. Luxury Christmas tree

                Import turtle as t #as is an alias. Subsequent calls of T are always turtle from turtle import * import random as r import time       n = 100.0   Speed ("fast") # defines the speed Screensize (BG ='black ') # defines the background color. You can change the color yourself left(90) forward(3*n) Color ("orange", "yellow") # defines the color of the uppermost star. The outer ring is orange and the inner is yellow begin_fill() left(126)   For I in range (5): # draw a five pointed star     forward(n/5)     Right (144) # Pentagram angle     forward(n/5)     Left (72) # continue to change the angle end_fill() right(126)   Def drawlight(): # defines the method of drawing colored lights     If r.randint (0, 30) = = 0: # if you think there are too many colored lights, you can increase the value range and there will be fewer corresponding lights         Color ('tomato ') # defines the first color         Circle (6) # defines the size of the lantern     elif r.randint(0,30) == 1:         Color ('Orange ') # defines the second color         Circle (3) # defines the size of the lantern     else:         Color ('dark green ') # other random numbers draw empty branches     Color ("dark green") # defines the color of the branches backward(n*4.8) Def tree (D, s): # start drawing the tree     if d <= 0: return     forward(s)     tree(d-1, s*.8)     right(120)     tree(d-3, s*.5)     Drawlight() # also calls the method of small colored lights     right(120)     tree(d-3, s*.5)     right(120)     backward(s) tree(15, n) backward(n/2)   For I in range (200): # circle the bottom decoration     a = 200 - 400 * r.random()     b = 10 - 20 * r.random()     up()     forward(b)     left(90)     forward(a)     down()     if r.randint(0, 1) == 0:             color('tomato')     else:         color('wheat')     circle(2)     up()     backward(a)     right(90)     backward(b)   t. Color ("dark red", "red") # defines the font color t. Write ("Merry Christmas", align = "center", font = ("Comic Sans MS", 40, "bold") # defines the text, position, font and size     Def drawsnow(): # defines the method of drawing snowflakes     t. HT () # hide pen head, HT = hideturn     t. Pensize (2) # defines the pen head size     For I in range (200): # draw how many snowflakes         t. Pencolor ("white") # defines the color of the brush as white, which actually means that the snowflake is white         t. Pu () # pen, Pu = penup         t. Setx (r.randint (- 350350)) # defines the X coordinate, which is randomly selected from - 350 to 350         t. Sety (r.randint (- 100350)) # defines the Y coordinate. Note that snowflakes generally do not fall on the ground, so they do not start from the too small longitudinal axis         t. PD () # pen down, PD = pendown         Dens = 6 # snowflake petals set to 6         Snowsize = r.randint (1,10) # defines the snowflake size         For J in range (DENS): # that's 6, that's 5 times, that's a snowflake pentagram             #t. Forward (int (snowsize)) #int() takes an integer             t.fd(int(snowsize))             t.backward(int(snowsize))             #t. BD (int (snowsize)) # note that there is no BD = backward, but there is FD = forward, a small bug             t. Right (int (360 / dens)) # rotation angle   Drawsnow() # calls the method of drawing snowflakes t. Done() # complete, otherwise it will be closed directly              

besserfolend.blogspot.com

Source: https://developpaper.com/three-code-examples-of-drawing-christmas-tree-in-python/

0 Response to "How to Draw a Simple Tree in Python Turtle"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel