想要别出心裁给女朋友准备一个惊喜,就用我们所学过的python代码来写一个表白程序。

  • 代码如下
# -*- coding:utf-8 -*-
# @Time : 2023/2/14 13:52
# @Author: Azure
# @File: Valentines_day.py






import turtle as t
from time import sleep
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def init():
    t.speed(2)
    t.pensize(2)
    t.screensize(480, 360)
    t.color('red', 'red')
    # 输出文字
    printer = t.Turtle()
    printer.hideturtle()
    printer.penup()
    printer.back(200)
    printer.write("送给我的宝贝\n\n", align="right", font=("楷体", 16, "bold"))
    printer.write("from xxx", align="center", font=("楷体", 12, "normal"))



def draw_heart_right():
    t.up()
    t.goto(50, 50)
    t.pendown()
    t.right(45)
    t.goto(100, 0)
    t.seth(45)
    t.fd(120)
    t.circle(50, 225)


def draw_heart_left():
    t.up()
    t.goto(0, 0)
    t.down()
    t.seth(45)
    t.fd(120)
    t.circle(50, 225)
    t.seth(90)
    t.circle(50, 225)
    t.fd(120)


def draw_arrow():
    t.up()
    t.seth(0)
    # 羽毛
    t.goto(-210, 40)
    t.pendown()
    t.goto(-215, 44)
    t.goto(-190, 49)
    t.goto(-175, 46)
    t.up()

    t.goto(-210, 40)
    t.pendown()
    t.goto(-213, 34)
    t.goto(-185, 39)
    t.goto(-175, 46)
    t.up()

    # 箭杆
    t.pendown()
    t.goto(0, 80)
    t.penup()
    t.goto(160, 110)
    t.pendown()
    t.goto(320, 140)

    # 箭羽
    t.left(160)
    t.begin_fill()
    t.fd(10)
    t.left(120)
    t.fd(10)
    t.left(120)
    t.fd(10)
    t.end_fill()

def mgh():
    fig = plt.figure()
    ax = fig.add_subplot(projection = '3d')
    # 将相位向后移动了6*pi
    [x, t] = np.meshgrid(np.array(range(25)) / 24.0, np.arange(0, 575.5, 0.5) / 575 * 20 * np.pi + 4*np.pi)
    p = (np.pi / 2) * np.exp(-t / (8 * np.pi))
    # 添加边缘扰动
    change = np.sin(15*t)/150
    # 将t的参数减少,使花瓣的角度变大
    u = 1 - (1 - np.mod(3.3 * t, 2 * np.pi) / np.pi) ** 4 / 2 + change
    y = 2 * (x ** 2 - x) ** 2 * np.sin(p)
    r = u * (x * np.sin(p) + y * np.cos(p))
    h = u * (x * np.cos(p) - y * np.sin(p))
    c= plt.get_cmap('Reds')
    surf = ax.plot_surface(r * np.cos(t), r * np.sin(t), h, rstride=1, cstride=1,
                           cmap= c, linewidth=0, antialiased=True)
    plt.show()


if __name__ == '__main__':
    init()
    draw_heart_right()
    draw_heart_left()
    draw_arrow()
    t.hideturtle()
    # t.done()
    sleep(1)
    mgh()
    # zp()