Every week and every day, we share Python code, introduction materials, advanced materials, basic syntax, crawlers, data analysis, web sites, machine learning, deep learning, etc.
Public numbers reply [communication], QQ scan code into group learning.
WeChat groupQQ group
1. Draw Christmas tree
import turtle screen = turtle.Screen() screen.setup(800,600) 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, 17): 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 += 2 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(17,20): y = 30*i for j in range(3): x = 30*j square.goto(x,-y+280) square.stamp() square.goto(-x,-y+280) square.stamp() turtle.exitonclick()
2. cherry blossoms
import turtle as T import random import time # Draw the trunk of cherry blossom (60,t) def Tree(branch, t): time.sleep(0.0005) if branch > 3: if 8 <= branch <= 12: if random.randint(0, 2) == 0: t.color('snow') # white else: t.color('lightcoral') # Pale coral t.pensize(branch / 3) elif branch < 8: if random.randint(0, 1) == 0: t.color('snow') else: t.color('lightcoral') # Pale coral t.pensize(branch / 2) else: t.color('sienna') # Color of zh (ochre) t.pensize(branch / 10) # 6 t.forward(branch) a = 1.5 * random.random() t.right(20 * a) b = 1.5 * random.random() Tree(branch - 10 * b, t) t.left(40 * a) Tree(branch - 10 * b, t) t.right(20 * a) t.up() t.backward(branch) t.down() # Fallen petals def Petal(m, t): for i in range(m): a = 200 - 400 * random.random() b = 10 - 20 * random.random() t.up() t.forward(b) t.left(90) t.forward(a) t.down() t.color('lightcoral') # Pale coral t.circle(1) t.up() t.backward(a) t.right(90) t.backward(b) # Drawing area t = T.Turtle() # canvas size w = T.Screen() t.hideturtle() # Hidden brush t.getscreen().tracer(5, 0) w.screensize(bg='white') # Wheat wheat t.left(90) t.up() t.backward(150) t.down() t.color('sienna') # Painting the trunk of Cherry Blossom Tree(60, t) # Fallen petals Petal(200, t) w.exitonclick()
3. Draw an arrow through the heart
from turtle import* color ("black","red") pensize(5) begin_fill() penup() goto(50,50) pendown() right(45) goto(100,0) left(90) fd(120) circle(50,225) penup() goto(0,0) pendown() left(135) fd(120) circle(50,225) seth(90) circle(50,225) fd(121) end_fill() left(56) penup() goto(-210,40) pendown() goto(0,80) penup() goto(160,110) pendown() goto(320,140) done()
4. paintings of love
# -*- coding:utf-8 -*- import turtle import time # Draw a cardioid arc def hart_arc(): for i in range(200): turtle.right(1) turtle.forward(2) def move_pen_position(x, y): turtle.hideturtle() # Hide brush (first) turtle.up() # write turtle.goto(x, y) # Move the brush to the specified starting coordinate (window center is 0,0) turtle.down() # Write down turtle.showturtle() # Show Brushes # Initialization turtle.setup(width=800, height=500) # Window (canvas) size turtle.color('red', 'pink') # stroke color turtle.pensize(2) # Brush thickness turtle.speed(0.5) # Depiction speed # Initialize brush start coordinates move_pen_position(x=0,y=-180) # Move brush position turtle.left(140) # Rotate left 140 degrees turtle.begin_fill() # Mark background fill position # Draw a cardioid line (bottom left) turtle.forward(224) # Move the brush forward, length 224 # Draw love arc hart_arc() # Left arc turtle.left(120) # Adjust brush angle hart_arc() # Right arc # Draw a cardioid line (bottom right) turtle.forward(224) turtle.end_fill() # Mark end of background fill # Click window to close the program window = turtle.Screen() window.exitonclick()
5. Painting roses
''' Created on Nov 18, 2017 @author: QiZhao ''' import turtle # Set initial position turtle.penup() turtle.left(90) turtle.fd(200) turtle.pendown() turtle.right(90) # Stamen turtle.fillcolor("red") turtle.begin_fill() turtle.circle(10,180) turtle.circle(25,110) turtle.left(50) turtle.circle(60,45) turtle.circle(20,170) turtle.right(24) turtle.fd(30) turtle.left(10) turtle.circle(30,110) turtle.fd(20) turtle.left(40) turtle.circle(90,70) turtle.circle(30,150) turtle.right(30) turtle.fd(15) turtle.circle(80,90) turtle.left(15) turtle.fd(45) turtle.right(165) turtle.fd(20) turtle.left(155) turtle.circle(150,80) turtle.left(50) turtle.circle(150,90) turtle.end_fill() # Petal 1 turtle.left(150) turtle.circle(-90,70) turtle.left(20) turtle.circle(75,105) turtle.setheading(60) turtle.circle(80,98) turtle.circle(-90,40) # Petal 2 turtle.left(180) turtle.circle(90,40) turtle.circle(-80,98) turtle.setheading(-83) # Leaf 1 turtle.fd(30) turtle.left(90) turtle.fd(25) turtle.left(45) turtle.fillcolor("green") turtle.begin_fill() turtle.circle(-80,90) turtle.right(90) turtle.circle(-80,90) turtle.end_fill() turtle.right(135) turtle.fd(60) turtle.left(180) turtle.fd(85) turtle.left(90) turtle.fd(80) # Leaf 2 turtle.right(90) turtle.right(45) turtle.fillcolor("green") turtle.begin_fill() turtle.circle(80,90) turtle.left(90) turtle.circle(80,90) turtle.end_fill() turtle.left(135) turtle.fd(60) turtle.left(180) turtle.fd(60) turtle.right(90) turtle.circle(200,60) # time.sleep(3)
6. painting Tai Chi
#Introducing the library of Turbo function from turtle import * #The first parameter radius is the radius of the circle, #color1 and color2 are two filling colors respectively, corresponding to black and white filling in the figure def draw(radius, color1,color2): #Set brush thickness width(3) #Set brush and fill colors color("black",color1) #Ready to start filling the drawing begin_fill() #First, draw a semicircle with radius/2 and radian of 180, and draw the semicircle shown by the red line circle(radius/2,180) #Draw a semicircle with radius and 180 radians. Draw a semicircle as shown by the yellow line circle(radius,180) #Rotate the brush 180 degrees left(180) #Draw a semicircle with radius/2 and radian of 180. At this time, the radius value is negative, #The center of the circle is on the right side of the brush. It's the semicircle shown by the green line circle(-radius/2,180) #End fill end_fill() #Rotate the brush 90 degrees to the left, right above the palette left(90) #Lift up the brush and leave no trace when it moves again up() #Move radius*0.35 forward, so that the small circle edge is 0.35 away from the big circle edge, #The radius of a small circle is radius*0.15 forward(radius*0.35) #Rotate the brush 90 degrees to the right, pointing to the right side of the palette right(90) #Put down the brush. down() color(color2,color2) #Start drawing the embedded small circle, as shown by the blue line begin_fill() circle(radius*0.15) end_fill() #Rotate the brush 90 degrees to point above the palette left(90) up() #Back radius*0.35 backward(radius*0.35) down() #Rotate the brush 90 degrees to the left of the palette left(90) #Define main function def main(): #Set window or palette size setup(500,500) #Draw black half, white inner circle draw(200,"black","white") #Draw white half, black inner circle draw(200,"white","black") #Hidden brush ht() main()
It's that simple~
Every week and every day, we share Python code, introduction materials, advanced materials, basic syntax, crawlers, data analysis, web sites, machine learning, deep learning, etc.
Wechat group (pay attention to "Python family" and learn Python easily)
QQ ② group (983031854)