程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[turtle library] Python drawing Christmas tree

編輯:Python

Write it at the front :

1、 Christmas is coming , Draw a Christmas tree for the person you like !

2、 This article is for learning only , If the code is not well written , I hope the boss can guide me to correct .

3、 The code is below , You need to take it yourself , Some code explanations will be attached , Simple and easy to understand , Welcome to decorate your Christmas tree according to your needs !

4、 Baidu online disk can directly access the packaged exe file , It can run directly on other people's computers .

        

 link :https://pan.baidu.com/s/1JwLDoq2Rl87JWSDjE2M0gg
Extraction code :1234

One 、 design sketch

Two 、 Code section

from turtle import *
from random import *
import math
# Drawing method
def Rightdraw(Range,Fd,Right):
for i in range(Range): # Range cycles
fd(Fd) # forward Fd Distance
right(Right) # Deflect to the right in the current direction of travel Right degree
def Leftdraw(Range,Fd,Left):
for i in range(Range): # Range cycles
fd(Fd) # forward Fd Distance
left(Left) # Deflect to the right in the current direction of travel Right degree
# Change the background to black
screensize(bg='black')
# Reset turtle position
def changeMypos(x,y,range=heading(),Fd=0):
penup()
goto(x, y)
seth(range)
fd(Fd)
pendown()
def drawBranch(x,y,size=1):
changeMypos(x,y)
Leftdraw(6,3,9)
seth(0)
Rightdraw(6,3,9)
seth(0)
fd(6)
# Draw five pointed stars
def drawStar(x,y,Range,size):
pensize(1)
color("red","yellow")
begin_fill()
changeMypos(x,y,Range)
for i in range(5): # Draw five pointed stars
forward(10*size)
right(144) # The angle of the pentagram
forward(10*size)
left(72) # Continue to change the angle
end_fill()
right(126)
# Draw snowflakes
def drawSnow():
hideturtle()
speed(0)
pencolor("white")
pensize(2)
for i in range(80): # The number of snowflakes
changeMypos(randint(-248,248),randint(-100,248))
petalNumber = 6 # The number of snowflake petals is 6
snowSize = int(randint(2,10))
for j in range(petalNumber):
fd(snowSize)
backward(snowSize)
right(360/petalNumber)
# Christmas socks
def drawSock(x,y,range,size=1):
# Draw the white edge of the sock
pensize(1)
changeMypos(x,y,range)
color("black","white")
begin_fill()
fd(20*size)
circle(3*size,180)
fd(20*size)
circle(3*size,180)
end_fill()
# Draw the bottom half of the sock
color("white","red")
begin_fill()
startx = x+2*size*math.cos(math.radians(range))
starty = y+2*size*math.sin(math.radians(range))
finalx = x+18*size*(math.cos(math.radians(range)))
finaly = y+18*size*(math.sin(math.radians(range)))
changeMypos(startx,starty,range-90)
fd(20*size) # Arc distance from white edge 40
seth(180+range)
fd(5*size) # Extend to the sock head 10
circle(7*size,180) # A semicircle at the end of a sock
fd(21*size) # Sock width 42
seth(90+range)
d = distance(finalx,finaly) # Find the distance between the bottom of the sock and the white edge
fd(d)
seth(range+180)
fd(16*size)
end_fill()
# Christmas hat
def drawHat(x,y,range,size=1):
# Draw the white edge of the cap
pensize(1)
changeMypos(x,y,range)
color("white","white")
begin_fill()
fd(20*size)
circle(-3*size,180)
fd(20*size)
circle(-3*size,180)
end_fill()
# Draw the top half of the hat
color("white","red")
begin_fill()
startx = x+2*size*math.cos(math.radians(range))
starty = y+2*size*math.sin(math.radians(range))
finalx = x+18*size*(math.cos(math.radians(range)))
finaly = y+18*size*(math.sin(math.radians(range)))
changeMypos(startx,starty,range+90)
Rightdraw(18,2*size,7)
seth(190)
Leftdraw(9,2*size,8)
goto(finalx,finaly)
goto(startx,starty)
end_fill()
# Draw the little ball on the Christmas hat
changeMypos(startx,starty,range+90)
Rightdraw(18,2*size,7)
begin_fill()
color("white","white")
circle(-2.5*size)
end_fill()
# Draw Ribbon
def drawRibbon(x,y,range,size):
begin_fill()
color("red","red")
seth(range+40)
fd(15*size*math.tan(math.radians(range+40)))
seth(range+90)
fd(20/3*size)
seth(range-140)
fd(15*size*math.tan(math.radians(range+40)))
seth(range-90)
fd(20/3*size)
end_fill()
# Christmas Candy
def drawCandy(x,y,range,size):
# Draw sugar body
pensize(1)
changeMypos(x,y,range)
color("white","white")
begin_fill()
startx = x+2*size*math.cos(math.radians(range))
starty = y+2*size*math.sin(math.radians(range))
finalx = x+8*size*(math.cos(math.radians(range)))
finaly = y+8*size*(math.sin(math.radians(range)))
changeMypos(startx,starty,range+90,40*size)
circle(-40/3*size,180)
circle(-8/3*size,180)
circle(22/3*size,180)
goto(finalx,finaly)
goto(startx,starty)
end_fill()
# Draw the following three ribbons
color("white")
changeMypos(startx,starty,range+90)
fd(10/3*size)
drawRibbon(xcor(),ycor(),range,size)
changeMypos(xcor(),ycor(),range+90,13.3*size)
drawRibbon(xcor(),ycor(),range,size)
changeMypos(xcor(),ycor(),range+90,13.3*size)
drawRibbon(xcor(),ycor(),range,size)
# Draw a ribbon of arc segments
changeMypos(startx,starty,range+90,40*size)
circle(-13.3*size,55)
x1 =xcor()
y1 =ycor()
begin_fill()
circle(-13.3*size,80)
right(75)
fd(6.3*size)
right(115)
circle(7*size,85)
goto(x1,y1)
end_fill()
setup(500,500,startx = None,starty = None)
title("Merry Christmas")
speed(0)
pencolor("pink")
pensize(10)
hideturtle()
changeMypos(0,185,0)
# shape(name= "classic")
# # Top of tree
seth(-120)
Rightdraw(10,12,2)
changeMypos(0,185,-60)
Leftdraw(10,12,2)
changeMypos(xcor(),ycor(),-150,10)
# # The waves of the first layer
for i in range(4):
Rightdraw(5,7,15)
seth(-150)
penup()
fd(2)
pendown()
# # The second floor of the tree
changeMypos(-55,70,-120)
Rightdraw(10,8,5)
changeMypos(50,73,-60)
Leftdraw(10,8,5)
changeMypos(xcor(),ycor(),-120,10)
seth(-145)
pendown()
# # The second layer of waves
for i in range(5):
Rightdraw(5,9,15)
seth(-152.5)
penup()
fd(3)
pendown()
# The tree has three levels
changeMypos(-100,0,-120)
Rightdraw(10,6.5,4.5)
changeMypos(80,0,-50)
Leftdraw(10,6,3)
changeMypos(xcor(),ycor(),-120,10)
seth(-145)
# # # The third wave
for i in range(6):
Rightdraw(5,9,15)
seth(-152)
penup()
fd(3)
pendown()
# # The tree has four floors
changeMypos(-120,-55,-130)
Rightdraw(7,10,4)
changeMypos(100,-55,-50)
Leftdraw(7,10,5)
changeMypos(xcor(),ycor(),-120,10)
seth(-155)
# # # The fourth layer of waves
for i in range(7):
Rightdraw(5,9,13)
seth(-155)
penup()
fd(3)
pendown()
# The root
changeMypos(-70,-120,-85)
Leftdraw(3,8,3)
changeMypos(70,-120,-95)
Rightdraw(3,8,3)
changeMypos(xcor(),ycor(),-170,10)
Rightdraw(10,12,2)
# Draw branches
drawBranch(45,-80)
drawBranch(-70,-25)
drawBranch(-20,40)
# Add pendant
drawHat(-25,175,-10,2.5)
drawCandy(-75,-50,-10,1) # -10 Don't move , This drawing is a little annoying , I didn't do the corner function
drawCandy(10,40,-10,1.2)
drawStar(110,-90,80,1)
drawStar(-120,-100,50,1)
drawStar(-90,-50,20,1)
drawStar(90,-25,30,1)
drawSock(10,-35,-10,2)
drawSock(-40,100,10,1)
drawStar(-20,40,30,1)
drawStar(10,120,90,1)
# Print a blessing
color("dark red","red")# Define font color
penup()
goto(0,-230)
write("Merry Christmas",align ="center",font=("Comic Sans MS",40,"bold"))# Define text 、 Location 、 typeface 、 size
# Call the snow function
drawSnow()
done()

Some necessary instructions :

1、 First of all, I apologize to you , How to draw candy , I have no better idea , Then the painting was very depressing , In the end, the whole function has not been improved , The placement position and size have been set , But angle because I press -10 To design the drawing method , So it can only draw -10 Degrees of candy , But it should be easy to adjust , I just have to add range Then adjust it again , The reader needs to revise it himself !

2、 All the pendants , Except candy , Coordinates are set 、 The placement angle and size are three , You can use this function to put the pendant in the right place , Decorate your own Christmas tree .

3、 Reference source : use Python Draw a Christmas tree , Referring to the author's tree drawing , And the code is optimized , Because the latter author's Pendant writing method is completely difficult for me to understand , Therefore, on this basis, it is modified into .

4、 Because my art level and coding level are limited , The picture of the pendant is not very beautiful , Let's make do with it , I also hope you can decorate our trees better !

5、turtle Library is learning and doing now , The algorithm is not very good , Please correct the imperfections under the guidance of the boss !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved