源代码:drawCardioid.py
import turtle
import math
'''
水平方向: ρ=a(1-cosθ) 或 ρ=a(1+cosθ) (a>0)
垂直方向: ρ=a(1-sinθ) 或 ρ=a(1+sinθ) (a>0)
'''
turtle.setup(800, 600)
turtle.penup()
turtle.speed(0)
turtle.pencolor("red")
turtle.begin_fill()
turtle.color("red")
turtle.pensize(10)
theta = 0.0
delta = 0.01
a = 100
rho = 0
x, y = 0,0
while theta <= 2 * math.pi + delta:
rho = a * (1 - math.sin(theta))
x, y = rho * math.cos(theta), rho * math.sin(theta)
turtle.goto(x,y)
turtle.pendown()
theta += delta
turtle.end_fill()
turtle.hideturtle()
turtle.mainloop()
# print("###",math.pi,math.pi*2)
运行效果截图:
源代码:drawHeart.py
import turtle
import math
'''
水平方向: ρ=a(1-cosθ) 或 ρ=a(1+cosθ) (a>0)
垂直方向: ρ=a(1-sinθ) 或 ρ=a(1+sinθ) (a>0)
'''
turtle.setup(800, 800)
turtle.penup()
turtle.speed(0)
turtle.pencolor("red")
turtle.pensize(5)
turtle.begin_fill()
turtle.color("red")
scale = 100
delta = 0.01
x = -2.0
while x <= 2:
if x >= 2 - delta:
x = 2.0
y = math.sqrt(1.0 - pow(abs(x)-1,2.0))
turtle.goto(scale*x,scale*y)
turtle.pendown()
# print(x)
x += delta
turtle.penup()
x = -2.0
while x <= 2:
if x >= 2 - delta:
x = 2.0
y = math.acos(1.0 - abs(x)) - math.pi
turtle.goto(scale*x,scale*y)
turtle.pendown()
# print(x)
x += delta
turtle.end_fill()
turtle.hideturtle()
turtle.mainloop()
# print("###",math.pi,math.pi*2)
运行效果截图:
源代码effect_1.py:
# -*- coding:utf-8 -*-
import turtle
import time
# 画爱心的顶部
def LittleHeart():
for i in range(200):
turtle.right(1)
turtle.forward(2)
# 输入表白的语句,默认I Love you
love = input('请输入表白语句,默认为输入为"I Love you": ')
# 输入署名或者赠谁,没有不执行
me = input('请输入您心上人的姓名或者昵称: ')
if love == '':
love = 'I Love you'
# 窗口大小
turtle.setup(width=800, height=500)
# 颜色
turtle.color('red', 'pink')
# 笔粗细
turtle.pensize(5)
# 速度
turtle.speed(1)
# 提笔
turtle.up()
# 隐藏笔
turtle.hideturtle()
# 去到的坐标,窗口中心为0,0
turtle.goto(0, -180)
turtle.showturtle()
# 画上线
turtle.down()
turtle.speed(1)
turtle.begin_fill()
turtle.left(140)
turtle.forward(224)
# 调用画爱心左边的顶部
LittleHeart()
# 调用画爱右边的顶部
turtle.left(120)
LittleHeart()
# 画下线
turtle.forward(224)
turtle.end_fill()
turtle.pensize(5)
turtle.up()
turtle.hideturtle()
# 在心中写字 一次
turtle.goto(0, 0)
turtle.showturtle()
turtle.color('#CD5C5C', 'pink')
# 在心中写字 font可以设置字体自己电脑有的都可以设 align开始写字的位置
turtle.write(love, font=('gungsuh', 30,), align="center")
turtle.up()
turtle.hideturtle()
time.sleep(2)
# 在心中写字 二次
turtle.goto(0, 0)
turtle.showturtle()
turtle.color('red', 'pink')
turtle.write(love, font=('gungsuh', 30,), align="center")
turtle.up()
turtle.hideturtle()
# 写署名
if me != '':
turtle.color('black', 'pink')
time.sleep(2)
turtle.goto(180, -180)
turtle.showturtle()
turtle.write(me, font=(20,), align="center", move=True)
# 点击窗口关闭
window = turtle.Screen()
window.exitonclick()
运行效果截图: