This article will use Python to draw a variety of different professions, attached source code, professions code to read this article enough, there are always you like, like the words don’t forget three links.
1. Show love
Effect:
Reference code:
# -*- coding:utf-8 -*-
import turtle
import time
# Draw the top of the heart
def LittleHeart() :
for i in range(200):
turtle.right(1)
turtle.forward(2)
# Input the statement, default I Love you
love = input('Please enter a statement, default is to enter "I Love you":')
# Enter signature or gift, no execution
me = input('Please enter your sweetheart's name or nickname:')
if love == ' ':
love = 'I Love you'
# window size
turtle.setup(width=800, height=500)
# color
turtle.color('red'.'pink')
# pen thickness
turtle.pensize(5)
# speed
turtle.speed(1)
# start
turtle.up()
# hidden pen
turtle.hideturtle()
The window center is 0,0
turtle.goto(0, -180)
turtle.showturtle()
# drawing
turtle.down()
turtle.speed(1)
turtle.begin_fill()
turtle.left(140)
turtle.forward(224)
# Call draw the top left side of the heart
LittleHeart()
# Call draw love on the top right
turtle.left(120)
LittleHeart()
# painting offline
turtle.forward(224)
turtle.end_fill()
turtle.pensize(5)
turtle.up()
turtle.hideturtle()
# Write once in your heart
turtle.goto(0.0)
turtle.showturtle()
turtle.color('#CD5C5C'.'pink')
Font-size: 16px; line-height: 20px; "> < span style =" font-size: 16px
turtle.write(love, font=('gungsuh'.30,), align="center")
turtle.up()
turtle.hideturtle()
time.sleep(2)
# Write twice in your heart
turtle.goto(0.0)
turtle.showturtle()
turtle.color('red'.'pink')
turtle.write(love, font=('gungsuh'.30,), align="center")
turtle.up()
turtle.hideturtle()
# write signature
ifme ! =' ':
turtle.color('black'.'pink')
time.sleep(2)
turtle.goto(180, -180)
turtle.showturtle()
turtle.write(me, font=(20,), align="center", move=True)
Click window to close
window = turtle.Screen()
window.exitonclick()
Copy the code
2. Three-dimensional love
Effect:
First, install Matplotlib
Reference code:
#! /usr/bin/env python3
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np
def heart_3d(x,y,z) :
return (x**2+ (9/4)*y**2+z**2-1) * *3-x**2*z**3- (9/80)*y**2*z**3
def plot_implicit(fn, bbox=(-1.5.1.5)) :
''' create a plot of an implicit function
fn ...implicit function (plot where fn==0)
bbox ..the x,y,and z limits of plotted interval'''
xmin, xmax, ymin, ymax, zmin, zmax = bbox*3
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
A = np.linspace(xmin, xmax, 100) # resolution of the contour
B = np.linspace(xmin, xmax, 40) # number of slices
A1, A2 = np.meshgrid(A, A) # grid on which the contour is plotted
for z in B: # plot contours in the XY plane
X, Y = A1, A2
Z = fn(X, Y, z)
cset = ax.contour(X, Y, Z+z, [z], zdir='z', colors=('r')),# [z] defines the only level to plot
# for this contour for this value of z
for y in B: # plot contours in the XZ plane
X, Z = A1, A2
Y = fn(X, y, Z)
cset = ax.contour(X, Y+y, Z, [y], zdir='y', colors=('red')),for x in B: # plot contours in the YZ plane
Y, Z = A1, A2
X = fn(x, Y, Z)
cset = ax.contour(X+x, Y, Z, [x], zdir='x',colors=('red')),# must set plot limits because the contour will likely extend
# way beyond the displayed level. Otherwise matplotlib extends the plot limits
# to encompass all values in the contour.
ax.set_zlim3d(zmin, zmax)
ax.set_xlim3d(xmin, xmax)
ax.set_ylim3d(ymin, ymax)
plt.show()
if __name__ == '__main__':
plot_implicit(heart_3d)
#! /usr/bin/env python3
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np
def heart_3d(x,y,z) :
return (x**2+ (9/4)*y**2+z**2-1) * *3-x**2*z**3- (9/80)*y**2*z**3
def plot_implicit(fn, bbox=(-1.5.1.5)) :
''' create a plot of an implicit function
fn ...implicit function (plot where fn==0)
bbox ..the x,y,and z limits of plotted interval'''
xmin, xmax, ymin, ymax, zmin, zmax = bbox*3
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
A = np.linspace(xmin, xmax, 100) # resolution of the contour
B = np.linspace(xmin, xmax, 40) # number of slices
A1, A2 = np.meshgrid(A, A) # grid on which the contour is plotted
for z in B: # plot contours in the XY plane
X, Y = A1, A2
Z = fn(X, Y, z)
cset = ax.contour(X, Y, Z+z, [z], zdir='z', colors=('r')),# [z] defines the only level to plot
# for this contour for this value of z
for y in B: # plot contours in the XZ plane
X, Z = A1, A2
Y = fn(X, y, Z)
cset = ax.contour(X, Y+y, Z, [y], zdir='y', colors=('red')),for x in B: # plot contours in the YZ plane
Y, Z = A1, A2
X = fn(x, Y, Z)
cset = ax.contour(X+x, Y, Z, [x], zdir='x',colors=('red')),# must set plot limits because the contour will likely extend
# way beyond the displayed level. Otherwise matplotlib extends the plot limits
# to encompass all values in the contour.
ax.set_zlim3d(zmin, zmax)
ax.set_xlim3d(xmin, xmax)
ax.set_ylim3d(ymin, ymax)
plt.show()
if __name__ == '__main__':
plot_implicit(heart_3d)
Copy the code
3. Red heart
Effect:
Reference code:
import turtle
turtle.bgcolor("black")
turtle.pensize(2)
sizeh = 1.2
def curve() :
for ii in range(200):
turtle.right(1)
turtle.forward(1 * sizeh)
turtle.speed(0)
turtle.color("red"."red")
turtle.begin_fill()
turtle.left(140)
turtle.forward(111.65 * sizeh)
curve()
turtle.left(120)
curve()
turtle.forward(111.65 * sizeh)
turtle.end_fill()
turtle.hideturtle()
Copy the code
4, Love
Effect:
Reference code:
import time
words = input('Please print the text you want to express :')
Example: Words = "Dear Lili, Happy Valentine's Day! Lyon Will Always Love You Till The End! Has Forever! Has"
for item in words.split():
# to print space between characters, add: item = item+' '
letterlist = []# letterList is the total list of all printed characters, which contains the y list list_X
for y in range(12, -12, -1):
list_X = []#list_X is a list of printed characters on the X-axis that hold letters of the String class
letters = ' '#letters is the string inside list_X, which is actually all the characters to be printed on this line
for x in range(-30.30) :#* is multiplication, ** is power
expression = ((x*0.05) * *2+(y*0.1) * *2-1) * *3-(x*0.05) * *2*(y*0.1) * *3
if expression <= 0:
letters += item[(x-y) % len(item)]
else:
letters += ' '
list_X.append(letters)
letterlist += list_X
print('\n'.join(letterlist))
time.sleep(1.5);
Copy the code
But, a little too monotonous, to the code simple transformation, to achieve a dynamic output heart, the code is as follows:
import time
words = input('Please print the text you want to express :')
for item in words.split():
print('\n'.join([' '.join([(item[(x-y) % len(item)] if ((x*0.05) * *2+(y*0.1) * *2-1) * *3-(x*0.05) * *2*(y*0.1) * *3< =0 else ' ') for x in range(-30.30)]) for y in range(12, -12, -1)]))
time.sleep(1.5)
Copy the code
Heart to 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()
Copy the code
Match man love
Effect:
Reference code:
# 2.14
from turtle import *
from time import sleep
def go_to(x, y) :
up()
goto(x, y)
down()
def head(x,y,r) :
go_to(x,y)
speed(1)
circle(r)
leg(x,y)
def leg(x,y) :
right(90)
forward(180)
right(30)
forward(100)
left(120)
go_to(x,y-180)
forward(100)
right(120)
forward(100)
left(120)
hand(x,y)
def hand(x,y) :
go_to(x,y-60)
forward(100)
left(60)
forward(100)
go_to(x, y - 90)
right(60)
forward(100)
right(60)
forward(100)
left(60)
eye(x,y)
def eye(x,y) :
go_to(x-50,y+130)
right(90)
forward(50)
go_to(x+40,y+130)
forward(50)
left(90)
def big_Circle(size) :
speed(20)
for i in range(150):
forward(size)
right(0.3)
def line(size) :
speed(1)
forward(51*size)
def small_Circle(size) :
speed(10)
for i in range(210):
forward(size)
right(0.786)
def heart(x, y, size) :
go_to(x, y)
left(150)
begin_fill()
line(size)
big_Circle(size)
small_Circle(size)
left(120)
small_Circle(size)
big_Circle(size)
line(size)
end_fill()
def main() :
pensize(2)
color('red'.'pink')
head(-120.100.100)
heart(250, -80.1)
go_to(200, -300)
write("To: intelligent and beautiful.", move=True, align="left", font=("Regular script".20."normal"))
done()
main()
Copy the code
7. Roses
rendering
Reference code
from turtle import *
import time
setup(1000.800.0.0)
speed(0)
penup()
seth(90)
fd(340)
seth(0)
pendown()
speed(5)
begin_fill()
fillcolor('red')
circle(50.30)
for i in range(10):
fd(1)
left(10)
circle(40.40)
for i in range(6):
fd(1)
left(3)
circle(80.40)
for i in range(20):
fd(0.5)
left(5)
circle(80.45)
for i in range(10):
fd(2)
left(1)
circle(80.25)
for i in range(20):
fd(1)
left(4)
circle(50.50)
time.sleep(0.1)
circle(120.55)
speed(0)
seth(-90)
fd(70)
right(150)
fd(20)
left(140)
circle(140.90)
left(30)
circle(160.100)
left(130)
fd(25)
penup()
right(150)
circle(40.80)
pendown()
left(115)
fd(60)
penup()
left(180)
fd(60)
pendown()
end_fill()
right(120)
circle(-50.50)
circle(-20.90)
speed(1)
fd(75)
speed(0)
circle(90.110)
penup()
left(162)
fd(185)
left(170)
pendown()
circle(200.10)
circle(100.40)
circle(-52.115)
left(20)
circle(100.20)
circle(300.20)
speed(1)
fd(250)
penup()
speed(0)
left(180)
fd(250)
circle(-300.7)
right(80)
circle(200.5)
pendown()
left(60)
begin_fill()
fillcolor('green')
circle(-80.100)
right(90)
fd(10)
left(20)
circle(-63.127)
end_fill()
penup()
left(50)
fd(20)
left(180)
pendown()
circle(200.25)
penup()
right(150)
fd(180)
right(40)
pendown()
begin_fill()
fillcolor('green')
circle(-100.80)
right(150)
fd(10)
left(60)
circle(-80.98)
end_fill()
penup()
left(60)
fd(13)
left(180)
pendown()
speed(1)
circle(-200.23)
exitonclick()
Copy the code
8. Simple love
Effect:
Reference code:
#! /usr/bin/env python
# -*- coding:utf-8 -*-
import turtle
import time
# Draw heart-shaped arcs
def hart_arc() :
for i in range(200):
turtle.right(1)
turtle.forward(2)
def move_pen_position(x, y) :
turtle.hideturtle() # Hide brushes (first)
turtle.up() # start
turtle.goto(x, y) # move the brush to the specified starting coordinate (window center is 0,0)
turtle.down() # writing
turtle.showturtle() # display brush
# initialization
turtle.setup(width=800, height=500) # Window (canvas) size
turtle.color('red'.'pink') # Brush color
turtle.pensize(3) # Brush thickness
turtle.speed(1) # Describe speed
Initialize the brush start coordinates
move_pen_position(x=0,y=-180) # Move brush position
turtle.left(140) # Rotate 140 degrees left
turtle.begin_fill() # Mark the background fill position
# Draw a heart-shaped line (bottom left)
turtle.forward(224) # Move the brush forward with length 224
# Draw heart arcs
hart_arc() # Left arc
turtle.left(120) Adjust the brush Angle
hart_arc() # Right arc
# Draw a heart-shaped line (bottom right)
turtle.forward(224)
turtle.end_fill() # Mark the end position of the background fill
Click window to close the program
window = turtle.Screen()
window.exitonclick()
Copy the code
One arrow two hearts
Effect:
Reference code:
from turtle import *
from time import sleep
def go_to(x, y) :
up()
goto(x, y)
down()
def big_Circle(size) : The # function is used to draw the large circle of the heart
speed(1)
for i in range(150):
forward(size)
right(0.3)
def small_Circle(size) : The # function is used to draw a small circle of the heart
speed(1)
for i in range(210):
forward(size)
right(0.786)
def line(size) :
speed(1)
forward(51*size)
def heart( x, y, size) :
go_to(x, y)
left(150)
begin_fill()
line(size)
big_Circle(size)
small_Circle(size)
left(120)
small_Circle(size)
big_Circle(size)
line(size)
end_fill()
def arrow() :
pensize(10)
setheading(0)
go_to(-400.0)
left(15)
forward(150)
go_to(339.178)
forward(150)
def arrowHead() :
pensize(1)
speed(1)
color('red'.'red')
begin_fill()
left(120)
forward(20)
right(150)
forward(35)
right(120)
forward(35)
right(150)
forward(20)
end_fill()
def main() :
pensize(2)
color('red'.'pink')
Tracer (30, 0) # getScreen ().tracer(30, 0
heart(200.0.1) The first two parameters control the position of the heart, and the last parameter controls the size of the heart
setheading(0) # Set the brush direction toward the positive x axis
heart(-80, -100.1.5) # Draw a second heart
arrow() # Draw a straight line through both hearts
arrowHead() # Draw the arrow of the arrow
go_to(400, -300)
write("The author: 520 python", move=True, align="left", font=("宋体".30."normal"))
done()
main()
Copy the code
Python game source, draw Pikachu, Doraemon source share click to receive. A lot of benefits
More than 300 Python ebooks are available, including: (1) Python400, (2) Python400, (3) Python400, (4) Python400, (5) Python400