Python draws heart shape through turtle library

Environment: python3.7 turtle library
python3.7 free to go to the official website to download transmission
Secondly, we want to install turtle library on your computer:
Open a terminal, enter pip install turtleto start the download.
After downloading both, open and IDLEcreate a new python file.
Before starting to write the code, let’s briefly talk about the functions in the turtle library.

turtle.setup(width,height,startx,starty)   --创建一个可视化屏幕,width表示宽,height表示高,startx表示左边框距离屏幕左边距离,starty表示上边框距离屏幕上边距离
turtle.pendown()       --落笔,即画笔移动会有痕迹
turtle.penup()         --抬笔,即画笔移动不再有痕迹
turtle.pensize(数值)   -- ?的粗细
turtle.pencolor(颜色)  --?的颜色  
turtle.circle(x,y)     --该函数可绘制圆,有两个参数,x代表半径,y代表角度,默认画笔处左侧水平距离x处为原点,y可不写,默认360(整个圆)
turtlr.goto(x,y)       --画笔从当前位置到坐标为(x,y)处
turtle.left(angle)
turtle.right(angle)     --向左或者向右转角度,即调整画笔的方向 
坐标说明:可视化屏幕的左上角代表(原点0,0)
这里只是简单的说明一些用到的函数,具体学习请自行搜索。

Ready to work, start writing code below

import turtle    # 引用turtle库

turtle.setup(500,500)   #创建一个500X500的可视化窗口
turtle.pencolor('red')  #将画笔颜色设为红色
turtle.pensize(2)       #将画笔粗细设为2
turtle.penup()          
turtle.goto(0,50)       #这是为了让心形在屏幕的正中间,根据自己创建的窗口调整
turtle.pendown()       

At this time, the operation will display as follows. If you want to draw the upper half of the circle (semi-circle), you need to adjust the direction of the brush so that it faces upward.
Insert picture description here

import turtle

turtle.setup(500,500,100,200)
turtle.pencolor('red')
turtle.pensize(2)
turtle.penup()
turtle.goto(0,50)
turtle.pendown()
turtle.left(90)
turtle.circle(100,180)

After adjusting the brush direction as follows, call the turtle.circle() function to draw a semicircle.
Insert picture description here
In order to make the heart shape more beautiful, the lower part here is composed of two parts, a 60° curve and a straight line

import turtle

turtle.setup(500,500,100,200)
turtle.pencolor('red')
turtle.pensize(2)
turtle.penup()
turtle.goto(0,50)
turtle.pendown()
turtle.left(90)
turtle.circle(100,180)
turtle.circle(200,60)
turtle.goto(0,-180)

Next, return the pen to the initial position, and draw a complete heart shape in reverse.
Tip: turtle.circle(x,y)When the value of x is negative, the circle will be drawn in reverse. The angle is still controlled by y, and y is not the default
Post all codes directly below 360°

import turtle

turtle.setup(500,500,100,200)
turtle.pencolor('red')
turtle.pensize(2)
turtle.penup()
turtle.goto(0,50)
turtle.pendown()
turtle.left(90)
turtle.circle(100,180)
turtle.circle(200,60)
turtle.goto(0,-180)
turtle.penup()
turtle.goto(0,50)
turtle.left(120)
turtle.pendown()
turtle.circle(-100,180)
turtle.circle(-200,60)
turtle.goto(0,-180)
turtle.penup()
turtle.goto(-250,250)

Final renderings?:
Insert picture description here
It’s true that this heart is a little fatter. Finally, I drew a better-looking one under the tips of my friends. I
Insert picture description here
am interested in exploring on my own... Post the code directly.

import turtle as p    #引用turtle库并重命名为p
#下面的代码就不添加注释了,上面介绍的有用法,我会在下面贴一下写代码之前的画的草图。
p.setup(500,500)   
p.pencolor('red')
p.pensize(2)
p.penup()
p.goto(0,60)
p.begin_fill()
p.fillcolor('pink')
p.pendown()
p.left(135)
p.circle(42.3,180)
p.goto(0,-60)
p.left(90)
p.goto(60,0)
p.circle(42.3,180)
p.end_fill()
p.penup()
p.goto(250,250)

Insert picture description here
Mathematics is omnipotent! ! !

Guess you like

Origin blog.csdn.net/weixin_43716048/article/details/97385969