1. 首页
  2. 编程语言
  3. Python
  4. Python实现表白神器三种画爱心方法

Python实现表白神器三种画爱心方法

上传者: 2023-03-12 08:35:12上传 RAR文件 3.51KB 热度 12次
import turtle

# 方法一:绘制心形函数
def heart(x, y, size):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()
    turtle.begin_fill()
    turtle.color('pink')
    turtle.left(45)
    turtle.forward(size)
    turtle.circle(size / 2, 180)
    turtle.right(90)
    turtle.circle(size / 2, 180)
    turtle.forward(size)
    turtle.end_fill()

# 方法二:贝塞尔曲线绘制心形
def draw_heart_bezier(size):
    turtle.penup()
    turtle.goto(-size, 0)
    turtle.pendown()
    turtle.color('red')
    turtle.begin_fill()
    for i in range(200):
        turtle.right(1)
        turtle.forward(size / 100 * 4 * (1 - 1.08 * abs(i - 100) / 100))
    turtle.end_fill()
    turtle.penup()
    turtle.goto(-size, 0)
    turtle.pendown()
    turtle.begin_fill()
    for i in range(200):
        turtle.left(1)
        turtle.forward(size / 100 * 4 * (1 - 1.08 * abs(i - 100) / 100))
    turtle.end_fill()

# 方法三:正弦曲线绘制心形
def draw_heart_sin(size):
    turtle.penup()
    turtle.goto(-size, 0)
    turtle.pendown()
    turtle.color('purple')
    turtle.begin_fill()
    for i in range(-180, 181):
        x = size * (2 * abs(i) / 180) * math.sin(i)
        y = size * (2 * abs(i) / 180) * math.cos(i)
        turtle.goto(x, y)
    turtle.end_fill()

# 调用方法绘制三种心形
heart(-200, 100, 100)
draw_heart_bezier(100)
draw_heart_sin(100)

# 隐藏画笔
turtle.hideturtle()
turtle.done()
用户评论