hi , Ancestors , It won't be Christmas. I'll be alone ? Today, let's talk about how to use python Let's draw a Christmas tree , Learn to send it to her soon , My circle of friends has let the Christmas tree brush the screen ! I'm here to teach you , After reading it, remember to give it to the third company !
Of course, they are all beautiful , But the code God mo , Is to make something different ,python Let's draw one ! Or get a front end to draw !
This time we used python Medium turtle( Sea turtle ) library , yes python A built-in Library in does not require additional installation
setup() function , The parameter is width,height,startx=None,starty=None, Represents the window width respectively , high , And , The abscissa and ordinate of the window on the computer screen .
turtle.setup(800,600)
Three attribute functions
pensize(<width>) # thickness
speed(speed) # Speed of motion
color(color) # Set brush color
This brings us to the coordinate system of sea turtles , Take the center of the window as the origin , Take the right as the positive direction , For the above y Affirmative direction .
It's similar to the movement above , All are 3 Item control
4. right(degree)# Turn right
5. left(degree)# Turn left
6. seth(degree)# Turn in a certain direction
This won't be used today , I don't think so , Welcome your loving ancestors , See the comments section !
from turtle import *
import random # Random , To make dots
import time
n = 100.0
speed("fastest") # Fast
screensize(bg='seashell')
left(90)
forward(3 * n)
color("orange", "yellow")
begin_fill()
left(126)
for i in range(5):
forward(n / 5)
right(144)
forward(n / 5)
left(72)
end_fill()
right(126)
color("dark green")
backward(n * 4.8)
def tree(d, s):
if d <= 0: return
forward(s)
tree(d - 1, s * .8)
right(120)
tree(d - 3, s * .5)
right(120)
tree(d - 3, s * .5)
right(120)
backward(s)
tree(15, n)
backward(n / 2)
for i in range(200):
a = 200 - 400 * random.random()
b = 10 - 20 * random.random()
up()
forward(b)
left(90)
forward(a)
down()
if random.randint(0, 1) == 0:
color('tomato')
else:
color('wheat')
circle(2)
up()
backward(a)
right(90)
backward(b)
time.sleep(60) # Process delay time
This article belongs to the title party , any similarity , It must not be a code God , Ha ha ha