x青年欢乐多 2019-03-19
程序员的爱情,在Python中运用调用turtle库来根据函数画出爱心。
效果图如下:
from turtle import*
setup(750,500)
penup()
pensize(25)
pencolor("pink")
fd(-230)
seth(90)
pendown()
circle(-50,180)
circle(50,-180)
circle(75,-50)
circle(-190,-45)
penup()
fd(185)
seth(180)
fd(120)
seth(90)
pendown()
circle(-75,-50)
circle(190,-45)
penup()
fd(184)
seth(0)
fd(80)
seth(90)
pendown()
circle(-50,180)
circle(50,-180)
circle(75,-50)
circle(-190,-45)
penup()
fd(185)
seth(180)
fd(120)
seth(90)
pendown()
circle(-75,-50)
circle(190,-45)
penup()
fd(150)
seth(180)
fd(300)
pencolor("red")
pensize(10)
pendown()
fd(-500)
seth(90)
fd(30)
fd(-60)
seth(30)
fd(60)
seth(150)
fd(60)
done()
Python使用turtle库画一个爱心构成的爱心
#!/usr/bin/env python
# coding:utf-8
from turtle import *
import time
def setTurtle():
# 窗口大小
screensize(900, 700, 'pink')
# 颜色
color('red', 'pink')
# 笔粗细
pensize(3)
# 速度
speed(6)
# 提笔
penup()
def getStart(h):
# 去到的坐标,窗口中心为0,0
goto(0, -180)
r = h / 5
drawBigL(r, h)
drawBigArc(r, 140)
drawBigArc(r, 70)
drawBigR(r, h)
centerRange()
drawHope()
drawName()
def drawBigL(r, h):
colors = ['red', 'orange', 'yellow', '#87CEEB', 'violet', 'red']
for i in range(int(240 / h) + 1):
seth(0)
color(colors[i], colors[i + 1])
drawHeart(r)
seth(140)
fd(h)
def drawBigArc(r, rad):
colors = ['red', 'orange', 'yellow', 'SkyBlue', 'violet', 'red']
for i in range(50):
if (i % 10 == 0):
color(colors[int(i / 10)], colors[int(i / 10) + 1])
seth(0)
drawHeart(r)
seth(rad - (i + 1) * 4)
rt(4)
fd(10.5)
def drawBigR(r, h):
colors = ['red', 'orange', 'yellow', 'SkyBlue', 'violet', 'red']
for i in range(int(240 / h) + 1):
color(colors[i], colors[i + 1])
seth(0)
drawHeart(r)
setheading(220)
fd(h)
def drawHeart(r):
down()
begin_fill()
factor = 180
seth(45)
circle(-r, factor)
fd(2 * r)
right(90)
fd(2 * r)
circle(-r, factor)
end_fill()
up()
# 在心中写字
def centerRange():
for i in range(6):
drawCenter(i)
time.sleep(1)
def drawCenter(i):
goto(0, 0)
colors = ['red', 'orange', 'yellow', 'SkyBlue', 'violet', 'red']
pencolor(colors[i])
# 在心中写字 font可以设置字体自己电脑有的都可以设 align开始写字的位置
write('love SPY', font=('gungsuh', 30,), align="center")
up()
setTurtle()
getStart(80)
# 点击窗口关闭
window = Screen()
window.exitonclick()