【python】美丽的代码,心动得效果,送给美丽得她~

前言

嗨喽,大家好呀~这里是爱看美女的茜茜呐

今天,3月7日啦~更是个特别得你日子,你想起来了嘛

没错,今天就是女生节(明天就是妇女节啦,记得给老婆/母亲准备点小礼物噢~)。

三七女生节,种下小小的愿望,收获大大的梦想。

登上小小的舞台,成为大大的主角。

甩开小小,拥抱大大的未来。女生节,祝你美丽快乐、人生精彩!

进入主题:今天就就展示一些好看得代码给大家吧~


本文章完整源码直接在文末名片领取。点击此处跳转

一、永远为你心动

代码展示

✨ 愿你阳光下像个孩子,风雨里像个大人。

导入所需模块

此模块为内置模块,无需安装

import turtle as t

画背景框

t.title("Python学习交流Q群:261823976")
#t.screensize(1000, 800)
t.setup(startx=0, starty = 0, width=800, height = 600)
t.hideturtle()
print('画爱心')

画爱心

def heart(x, y):
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.color('pink')
    t.setheading(50)
    t.circle( -5, 180)
    t.circle( -45, 12)
    t.setheading(130)
    t.circle( -45, 12)
    t.circle( -5, 180)
heart(-30, 155)
heart(-220, 145)
heart(-210, 60)
heart(-100, 100)
heart(-20, 20)
heart(-70, 130)
heart(-140, -20)
heart(30, 100)
heart(-60, -20)
heart(10, 60)
heart(-100, -70)
heart(20, 145)
heart(-140, -20)
heart(-130, 130)
heart(-180, 20)
heart(-170, 155)
heart(-230, 100)
def write_mes(x, y, size, ss):
    t.hideturtle()
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.pencolor('black')
    t.write(ss, font=('Times New Roman', size, 'normal'))

画红心

print('画红心')
def heart_fill(x, y):
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.color('red', 'red')
    t.begin_fill()
    t.setheading(50)
    t.circle( -5, 180)
    t.circle( -45, 12)
    t.setheading(130)
    t.circle( -45, 12)
    t.circle( -5, 180)
    t.end_fill()
x = 90
y = 110

右边爱心

write_mes(x, y, 11, '超')
heart_fill(-100, 100)
heart_fill(-70, 130)
heart_fill(-30, 155)
heart_fill(20, 145)
heart_fill(30, 100)
write_mes(x, y-30, 11, '爱')
heart_fill(10, 60)
heart_fill(-20, 20)
heart_fill(-60, -20)
heart_fill(-100, -70)

左边爱心

write_mes(x, y-30*2, 11, '你')
heart_fill(-140, -20)
heart_fill(-180, 20)
heart_fill(-210, 60)
heart_fill(-230, 100)
write_mes(x, y-30*3, 11, '呀')
heart_fill(-220, 145)
heart_fill(-170, 155)
heart_fill(-130, 130)
write_mes(x, y-30*4, 11, '宝')
t.speed(200)
print('画心动线')
def heart_bit():

画心动线

    t.penup()
    t.goto(-170, 40)
    t.pendown()
    t.pencolor('red')
    t.setheading(0)
    t.pensize(2)
    t.forward(10)

第一个小波浪

    t.setheading(45)
    t.circle(50, 10)
    t.setheading(0)
    t.circle(-3,90)
    t.circle(50, 5)

横线

    t.setheading(0)
    t.forward(10)

第一个下尖峰

    t.setheading(-80)
    t.forward(7)
    t.setheading(70)
    t.forward(25)
    t.setheading(-85)
    t.forward(29)
    t.setheading(70)
    t.forward(13)
    t.setheading(0)
    t.forward(15)

画心

    t.setheading(150)
    t.circle(-20, 40)
    t.circle(-10, 170)
    t.setheading(70)
    t.circle(-10, 170)
    t.circle(-20, 40)
    t.setheading(0)
    t.forward(15)
    #2
    t.setheading(-80)
    t.forward(7)
    t.setheading(70)
    t.forward(25)
    t.setheading(-85)
    t.forward(29)
    t.setheading(70)
    t.forward(13)
    t.setheading(0)
    t.forward(15)
    t.setheading(0)
    t.forward(10)
    t.setheading(45)
    t.circle(50, 10)
    t.setheading(0)
    t.circle(-3,90)
    t.circle(50, 5)
    t.setheading(0)
    t.forward(10)
def write_name(x, y, size, ss):
    t.hideturtle()
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.pencolor('black')
    t.write(ss, font=('Times New Roman', size, 'normal'))
def undo_back():
    t.undo()
    t.undo()
    t.undo()
    t.undo()
    t.undo()
    t.undo()
    t.undo()
    t.undo()
    t.undo()
    t.undo()
def undo_back2():
    t.undo()
    t.undo()
def name_heart_bit():

写两个人的姓名

    write_name(-180, 70, 11, '笔芯')
    write_name(-180, 70, 11, '笔芯')
    write_name(-180, 70, 11, '笔芯')
    heart_bit()
    write_name(-60, 70, 11, 'Python')
    write_name(-60, 70, 11, 'Python')
    write_name(-60, 70, 11, 'Python')
    write_name(-60, 70, 11, 'Python')
    write_name(-60, 70, 11, 'Python')
    undo_back()
    undo_back()
    undo_back()
    undo_back()
    undo_back()
    undo_back()
    undo_back()
    undo_back()
    undo_back()
    undo_back2()
while 1:
    name_heart_bit()

二、为你跳动得心

代码展示

✨ 晚上星月争辉,美梦陪你入睡

模块导入

( 完整源码等直接点击找管理小姐姐免费领取呀~ )

import random
from math import sin, cos, pi, log
from tkinter import *

背景画布

CANVAS_WIDTH = 640  # 画布的宽
CANVAS_HEIGHT = 480  # 画布的高
CANVAS_CENTER_X = CANVAS_WIDTH / 2  # 画布中心的X轴坐标
CANVAS_CENTER_Y = CANVAS_HEIGHT / 2  # 画布中心的Y轴坐标
IMAGE_ENLARGE = 11  # 放大比例

心的颜色,这个是中国红

HEART_COLOR = "#ff8181" 

基础函数

def heart_function(t, shrink_ratio: float = IMAGE_ENLARGE):
    x = 16 * (sin(t) ** 3)
    y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))

放大

    x *= shrink_ratio
    y *= shrink_ratio

移到画布中央

    x += CANVAS_CENTER_X
    y += CANVAS_CENTER_Y

    return int(x), int(y)

随机内部扩散

def scatter_inside(x, y, beta=0.15):

    ratio_x = - beta * log(random.random())
    ratio_y = - beta * log(random.random())

    dx = ratio_x * (x - CANVAS_CENTER_X)
    dy = ratio_y * (y - CANVAS_CENTER_Y)

    return x - dx, y - dy

抖动

def shrink(x, y, ratio):

    force = -1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.6)  # 这个参数...
    dx = ratio * force * (x - CANVAS_CENTER_X)
    dy = ratio * force * (y - CANVAS_CENTER_Y)
    return x - dx, y - dy

自定义曲线函数,调整跳动周期

def curve(p):
    # 可以尝试换其他的动态函数,达到更有力量的效果(贝塞尔?)
    return 2 * (2 * sin(4 * p)) / (2 * pi)

爱心类

class Heart:
    def __init__(self, generate_frame=20):
        self._points = set()  # 原始爱心坐标集合
        self._edge_diffusion_points = set()  # 边缘扩散效果点坐标集合
        self._center_diffusion_points = set()  # 中心扩散效果点坐标集合
        self.all_points = {}  # 每帧动态点坐标
        self.build(2000)

        self.random_halo = 1000

        self.generate_frame = generate_frame
        for frame in range(generate_frame):
            self.calc(frame)

    def build(self, number):
        # 爱心
        for _ in range(number):
            t = random.uniform(0, 2 * pi)  # 随机不到的地方造成爱心有缺口
            x, y = heart_function(t)
            self._points.add((x, y))

        # 爱心内扩散
        for _x, _y in list(self._points):
            for _ in range(3):
                x, y = scatter_inside(_x, _y, 0.05)
                self._edge_diffusion_points.add((x, y))

        # 爱心内再次扩散
        point_list = list(self._points)
        for _ in range(4000):
            x, y = random.choice(point_list)
            x, y = scatter_inside(x, y, 0.17)
            self._center_diffusion_points.add((x, y))

    @staticmethod
    def calc_position(x, y, ratio):
        # 调整缩放比例
        force = 1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.520)  # 魔法参数

        dx = ratio * force * (x - CANVAS_CENTER_X) + random.randint(-1, 1)
        dy = ratio * force * (y - CANVAS_CENTER_Y) + random.randint(-1, 1)

        return x - dx, y - dy

    def calc(self, generate_frame):
        ratio = 10 * curve(generate_frame / 10 * pi)  # 圆滑的周期的缩放比例

        halo_radius = int(4 + 6 * (1 + curve(generate_frame / 10 * pi)))
        halo_number = int(3000 + 4000 * abs(curve(generate_frame / 10 * pi) ** 2))

        all_points = []

        # 光环
        heart_halo_point = set()  # 光环的点坐标集合
        for _ in range(halo_number):
            t = random.uniform(0, 2 * pi)  # 随机不到的地方造成爱心有缺口
            x, y = heart_function(t, shrink_ratio=11.6)  # 魔法参数
            x, y = shrink(x, y, halo_radius)
            if (x, y) not in heart_halo_point:
                # 处理新的点
                heart_halo_point.add((x, y))
                x += random.randint(-14, 14)
                y += random.randint(-14, 14)
                size = random.choice((1, 2, 2))
                all_points.append((x, y, size))

        # 轮廓
        for x, y in self._points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 3)
            all_points.append((x, y, size))

        # 内容
        for x, y in self._edge_diffusion_points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 2)
            all_points.append((x, y, size))

        for x, y in self._center_diffusion_points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 2)
            all_points.append((x, y, size))

        self.all_points[generate_frame] = all_points

    def render(self, render_canvas, render_frame):
        for x, y, size in self.all_points[render_frame % self.generate_frame]:
            render_canvas.create_rectangle(x, y, x + size, y + size, width=0, fill=HEART_COLOR)


def draw(main: Tk, render_canvas: Canvas, render_heart: Heart, render_frame=0):
    render_canvas.delete('all')
    render_heart.render(render_canvas, render_frame)
    main.after(160, draw, main, render_canvas, render_heart, render_frame + 1)


if __name__ == '__main__':
    root = Tk()  # 一个Tk
    canvas = Canvas(root, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH)
    canvas.pack()
    heart = Heart()  # 心
    draw(root, canvas, heart)  # 开始画画~
    root.mainloop()

三、专属你得玫瑰

代码展示

✨ 愿你自强到无需有人宠有人惯,却依然幸运到有人宠有人惯。

导入模块

import math
import time
import random
import turtle as t
list1=[]
for i in range(5):
    list1.append(int(random.uniform(-500,500)))
list2=[]
for i in range(5):
    list2.append(int(random.uniform(-200,-50)))
list3=[]
for i in range(8):
    list3.append(int(random.uniform(-400,400)))
list4=[]
for i in range(8):
    list4.append(int(random.uniform(-150,-50)))
list5=[]
for i in range(7):
    list5.append(int(random.uniform(-300,300)))
list6=[]
for i in range(7):
    list6.append(int(random.uniform(-200,-100)))
list7=[]
for i in range(18):
    list7.append(int(random.uniform(-500,500)))
list8=[]
for i in range(18):
    list8.append(int(random.uniform(-100,100)))
def move_pen_position(x, y):
    t.hideturtle()  # 隐藏画笔(先)
    t.up()  # 提笔
    t.goto(x, y)  # 移动画笔到指定起始坐标(窗口中心为0,0)
    t.down()  # 下笔
    t.showturtle()  # 显示画笔
love = input("请输入表白话语,默认为I LOVE U:")
signature = input("请签署你的名字:")
date=input("请写上日期:")
if love == '':
    love = 'I Love U'

初始化

t.setup(width=800, height=500)  # 窗口(画布)大小
t.color('red', 'pink')  # 画笔颜色
t.pensize(3)  # 画笔粗细
t.speed(0)  # 描绘速度

画大号爱心(位置随机)

for x,y in list(zip(list1,list2)):
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.fillcolor("#A020F0")
    t.begin_fill()
    t.pencolor("#A020F0")
    t.forward(40)
    t.circle(20, 180)
    t.right(90)
    t.circle(20, 180)
    t.forward(40)
    t.end_fill()
    t.penup()
    t.goto(x, y)

画中号爱心(位置随机)

for x,y in list(zip(list5,list6)):
    t.pendown()
    t.fillcolor("#DA70D6")
    t.begin_fill()
    t.pencolor("#DA70D6")
    t.forward(30)
    t.circle(15, 180)
    t.right(90)
    t.circle(15, 180)
    t.forward(30)
    t.end_fill()
    t.penup()
    t.goto(x, y)

画小号爱心(位置随机)

for x,y in list(zip(list3,list4)):
    t.pendown()
    t.fillcolor("#DDA0DD")
    t.begin_fill()
    t.pencolor("#DDA0DD")
    t.forward(20)
    t.circle(10,180)
    t.right(90)
    t.circle(10,180)
    t.forward(20)
    t.end_fill()
    t.penup()
    t.goto(x, y)

画点点(位置随机)

for x,y in list(zip(list7,list8)):
    t.pendown()
    t.fillcolor("#7B6BEE")
    t.begin_fill()
    t.pencolor("#7B6BEE")
    t.circle(3, 360)
    t.end_fill()
    t.penup()
    t.goto(x, y)
def DegreeCurve(n, r, d=1):
    for i in range(n):
        t.left(d)
        t.circle(r, abs(d))

初始位置设定

s = 0.2
# t.setup(450*5*s, 750*5*s)
t.pencolor("black")
t.fillcolor("#8A2BE2")
t.speed(100)
t.penup()
t.goto(0, 900*s)
t.pendown()

绘制花朵形状

t.begin_fill()
t.circle(200*s,30)
DegreeCurve(60, 50*s)
t.circle(200*s,30)
DegreeCurve(4, 100*s)
t.circle(200*s,50)
DegreeCurve(50, 50*s)
t.circle(350*s,65)
DegreeCurve(40, 70*s)
t.circle(150*s,50)
DegreeCurve(20, 50*s, -1)
t.circle(400*s,60)
DegreeCurve(18, 50*s)
t.fd(250*s)
t.right(150)
t.circle(-500*s,12)
t.left(140)
t.circle(550*s,110)
t.left(27)
t.circle(650*s,100)
t.left(130)
t.circle(-300*s,20)
t.right(123)
t.circle(220*s,57)
t.end_fill()

绘制花枝形状

t.left(120)
t.fd(280*s)
t.left(115)
t.circle(300*s,33)
t.left(180)
t.circle(-300*s,33)
DegreeCurve(70, 225*s, -1)
t.circle(350*s,104)
t.left(90)
t.circle(200*s,105)
t.circle(-500*s,63)
t.penup()
t.goto(170*s,-30*s)
t.pendown()
t.left(160)
DegreeCurve(20, 2500*s)
DegreeCurve(220, 250*s, -1)

绘制一个绿色叶子

t.fillcolor('#00CD00')
t.penup()
t.goto(670*s,-180*s)
t.pendown()
t.right(140)
t.begin_fill()
t.circle(300*s,120)
t.left(60)
t.circle(300*s,120)
t.end_fill()
t.penup()
t.goto(180*s,-550*s)
t.pendown()
t.right(85)
t.circle(600*s,40)

绘制另一个绿色叶子

t.penup()
t.goto(-150*s,-1000*s)
t.pendown()
t.begin_fill()
t.rt(120)
t.circle(300*s,115)
t.left(75)
t.circle(300*s,100)
t.end_fill()
t.penup()
t.goto(430*s,-1070*s)
t.pendown()
t.right(30)
t.circle(-600*s,35)
move_pen_position(300, -280)

font:设定字体、尺寸(电脑下存在的字体都可设置) align:中心对齐

t.write(love, font=('Arial', 20, 'bold'), align="center")

签写署名和日期

if (signature != '') & (date != ''):
    t.color('red', 'pink')
    time.sleep(2)
    move_pen_position(300, -320)
    t.hideturtle()  # 隐藏画笔
    t.write(signature, font=('Arial', 20), align="center")
    move_pen_position(300, -350)
    t.hideturtle()  # 隐藏画笔
    t.write(date, font=('Arial', 20), align="center")

1-5点击窗口关闭程序

window = t.Screen()
window.exitonclick()

四、幸运四叶草

代码展示

✨ 轻铺光阴如笺,拾一支流云廋笔,寻一盏韶华入墨,把情愫写满。

导入模块

import turtle

画布背景

turtle.title('四叶草') #画布标题
# turtle.bgcolor("green") #背景颜色
turtle.pensize(5) #画笔宽度
turtle.speed(2) #海龟移动速度
turtle.fillcolor("green") #填充的颜色
turtle.begin_fill() #开始填色

花瓣

for i in range(4):
    turtle.circle(80, 50)
    turtle.circle(20, 180)
    turtle.left(180)
    turtle.circle(20, 180)
    turtle.circle(80, 50)
    turtle.left(180)
turtle.end_fill() #结束填色

花心

turtle.penup()
turtle.goto(-15, 0)
turtle.pendown()
turtle.circle(1)

花枝

turtle.penup()
turtle.goto(-10, -10)
turtle.pendown()
turtle.right(90)
turtle.circle(-200, 80)

打印

turtle.penup()
turtle.goto(0, 100) #移动位置,改变write的位置
turtle.write(' 四叶草它的花语是幸运!', font = ('arial', 18, 'normal')) #打印内容
turtle.done() #手动关闭

尾语

感谢你观看我的文章呐~本次航班到这里就结束啦 🛬

希望本篇文章有对你带来帮助 🎉,有学习到一点知识~

躲起来的星星🍥也在努力发光,你也要努力加油(让我们一起努力叭)。

最后,宣传一下呀~👇👇👇更多源码、资料、素材、解答、交流皆点击下方名片获取呀👇👇👇

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值