最强Python表白代码

简介: 最强Python表白代码

玫瑰

毫无疑问,玫瑰一直都是七夕、520......这类节日的专属,带文字的玫瑰花,文字可以根据节日自行更改。

参考代码:

import turtle

turtle.speed(0)
turtle.delay(10)
turtle.penup()
turtle.left(90)
turtle.fd(200)
turtle.pendown()
turtle.right(90)
turtle.fillcolor('red')
turtle.begin_fill()
turtle.circle(10, 180)
turtle.circle(25, 110)
# 花瓣
turtle.left(150)
turtle.circle(-90, 70)
turtle.left(20)
turtle.circle(75, 105)
turtle.setheading(60)
turtle.circle(80, 98)
turtle.circle(-90, 40)
# 文字
turtle.color('red')
turtle.pu()
turtle.goto(-210,80)
turtle.pd()
turtle.write('520 Happy', move=False, align='center',font=("Times", 18, "bold"))
turtle.pu()
turtle.goto(210,80)
turtle.pd()
turtle.write('I LOVE YOU', move=False, align='center',font=("Times", 18, "bold"))
turtle.pu()
turtle.pu()
turtle.hideturtle()
turtle.done()

效果:
640.gif

心连心

丘比特爱心之箭,把你的心我的心串一串......

参考代码:

import turtle as t

t.color('red','pink')
t.begin_fill()
t.width(5)
t.left(135)
t.fd(100)
t.right(180)
t.circle(50,-180)
t.left(90)
t.circle(50,-180)
t.right(180)
t.fd(100)
t.pu()
t.goto(50,-30)
t.pd()
t.right(90)
t.fd(100)
t.right(180)
t.circle(50,-180)
t.left(90)
t.circle(50,-180)
t.right(180)
t.fd(100)
t.end_fill()
t.hideturtle()
t.pu()
t.goto(250,-70)
t.pd()

效果:

640 (1).gif

动态爱心

一颗跳动的爱心,见之心动.....

参考代码:

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

for _ in range(520):
 x, y = random.choice(point_list)
 x, y = scatter_inside(x, y, 0.17)
 self._center_diffusion_points.add((x, y))
heart_halo_point = set()
for _ in range(halo_number):
 t = random.uniform(0, 2 * pi)
 x, y = heart(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))
self.all_points[generate_frame] = all_points
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

效果:
640 (2).gif

爱心biu

一个小可爱伸手发射爱心+文字,文字可根据节日自行更改。

参考代码:

from turtle import *

color('black')
go_to(-228, 72)
pensize(3)
left(150)
ring(350,1,0.8,'right')
left(150)
forward(70)
left(90)
forward(10)
ring(200,0.1,0.9,'right')
forward(10)
left(90)
forward(20)
ring(200,0.1,0.9,'right')
forward(10)
left(90)
ring(200,0.2,0.9,'right')
left(100)
left
forward(80)
go_to(-228, 72)
left(40)
forward(40)
ring(120,0.2,0.9,'left')
go_to(-219,52)
right(95)
forward(80)
right(85)
ring(205,0.1,0.9,'left')
forward(40)
left(90)
forward(10)
ring(200,0.1,0.9,'right')
forward(10)
left(90)
forward(40)
ring(205,0.1,0.9,'left')
right(92)
forward(90)

效果:
640 (3).gif

爱心树

一棵长满爱心果实的爱心树,祝你们修成正果.....

参考代码:

import turtle, random

# 画爱心
def love(x, y):
    lv = turtle.Turtle()
    lv.hideturtle()
    lv.up()
    # 定位
    lv.goto(x, y)
    # 画圆弧
    def curvemove():
        for i in range(20):
            lv.right(10)
            lv.forward(2)

    lv.color('red', 'pink')
    lv.speed(10000000)
    lv.pensize(1)
    lv.down()
    lv.begin_fill()
    lv.left(140)
    lv.forward(22)
    curvemove()
    lv.left(120)
    curvemove()
    lv.forward(22)
    # 画完复位
    lv.left(140)
    lv.end_fill()

# 画树
def tree(branchLen, t):
    # 剩余树枝太少要结束递归
    if branchLen > 5:
        # 如果树枝剩余长度较短则变绿
        if branchLen < 20:
            t.color("green")
            t.pensize(random.uniform((branchLen + 5) / 4 - 2, (branchLen + 6) / 4 + 5))
            t.down()
            t.forward(branchLen)
            love(t.xcor(), t.ycor())
            t.up()
            t.backward(branchLen)
            t.color("brown")
            return
        t.pensize(random.uniform((branchLen + 5) / 4 - 2, (branchLen + 6) / 4 + 5))
        t.down()
        t.forward(branchLen)
        # 以下递归
        ang = random.uniform(15, 45)
        t.right(ang)
        # 随机决定减小长度
        tree(branchLen - random.uniform(12, 16), t)
        t.left(2 * ang)
        # 随机决定减小长度
        tree(branchLen - random.uniform(12, 16), t)
        t.right(ang)
        t.up()
        t.backward(branchLen)

效果:
640 (4).gif

告白气球

五颜六色动态向上漂浮的气球,鼠标点击可击破气球。

参考代码:


from turtle import *
from random import randrange, choice

# 气球
balloons = []
# 颜色
color_option = ["red", "blue", "green", "purple", "pink", "yellow", "orange"]
# 气球大小
size = 50
# 气球线
def line(x, y, a, b, line_width=1, color_name="black"):
    up()
    goto(x, y)
    down()
    color(color_name)
    width(line_width)
    goto(a, b)

def distance(x, y, a, b):
    # 判断鼠标点击位置和气球坐标的距离
    return ((a - x) ** 2 + (b - y) ** 2) ** 0.5
def tap(x, y):
    for i in range(len(balloons)):
        # 判断是否点击气球队列中的其中一个
        if distance(x, y, balloons[i][0], balloons[i][1]) < (size / 2):
            # 删除气球
            balloons.pop(i)
            return

效果:
640 (5).gif

告白墙

一幅寂静优美的画面配上优美的文字,一眼万年.....

参考代码:


import cv2
import numpy as np
from PIL import Image
from wordcloud import WordCloud

img = cv2.imread('test.png')
mask = np.zeros(img.shape[:2], np.uint8)
size = (1, 65)
bgd = np.zeros(size, np.float64)
fgd = np.zeros(size, np.float64)
rect = (1, 1, img.shape[1], img.shape[0])
cv2.grabCut(img, mask, rect, bgd, fgd, 10, cv2.GC_INIT_WITH_RECT)
mask2 = np.where((mask == 2) | (mask == 0), 1, 255)
img = img.astype(np.int32)
img *= mask2[:, :, np.newaxis]
img[img>255] = 255
img =img.astype(np.uint8)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = Image.fromarray(img, 'RGB')
img.save('test1.jpg')

效果:
image.png

相关文章
|
5天前
|
人工智能 数据挖掘 数据处理
揭秘Python编程之美:从基础到进阶的代码实践之旅
【9月更文挑战第14天】本文将带领读者深入探索Python编程语言的魅力所在。通过简明扼要的示例,我们将揭示Python如何简化复杂问题,提升编程效率。无论你是初学者还是有一定经验的开发者,这篇文章都将为你打开一扇通往高效编码世界的大门。让我们开始这段充满智慧和乐趣的Python编程之旅吧!
|
3天前
|
设计模式 开发框架 缓存
探索Python中的装饰器:简化代码,增强功能
【9月更文挑战第16天】在Python的世界里,装饰器宛如一位巧手魔术师,轻轻一挥魔杖,便能让我们的函数和类焕发新生。本文将带你领略装饰器的魔力,从基础概念到实战应用,一步步解锁装饰器的强大潜能。让我们一起踏上这段奇妙的旅程,探索如何用装饰器简化代码,增强功能。
|
5天前
|
XML 数据格式 Python
Python技巧:将HTML实体代码转换为文本的方法
在选择方法时,考虑到实际的应用场景和需求是很重要的。通常,使用标准库的 `html`模块就足以满足大多数基本需求。对于复杂的HTML文档处理,则可能需要 `BeautifulSoup`。而在特殊场合,或者为了最大限度的控制和定制化,可以考虑正则表达式。
21 12
|
5天前
|
测试技术 开发者 Python
探索Python中的装饰器:简化代码,增强功能
【9月更文挑战第14天】在编程世界中,我们总是寻找使代码更简洁、更强大的方法。Python的装饰器正是这样一项工具,它允许我们在不修改原有函数代码的情况下,增加额外的功能。本文将通过实际示例,引导你理解装饰器的基本概念,展示如何创建和应用它们,以及如何利用装饰器简化日常编程任务。无论你是初学者还是有经验的开发者,这篇文章都将为你提供新的视角和技巧,让你的代码更加高效和优雅。
21 12
|
6天前
|
缓存 开发者 Python
探索Python中的装饰器:简化代码,增强功能
【9月更文挑战第13天】本文深入探讨了Python中一个强大而常被误解的特性——装饰器。我们将从基础概念入手,逐步揭示其背后的原理,并通过实际示例展示如何利用装饰器来简化代码和扩展函数功能。文章不仅为初学者提供了清晰的入门指南,还为有经验的开发者展示了高级用法,旨在帮助读者更好地理解和运用装饰器,以提升编码效率和程序的可维护性。
25 10
|
3天前
|
测试技术 Python
Python中的装饰器:简化代码的魔法
【9月更文挑战第16天】在Python编程的世界里,装饰器就像是一把瑞士军刀,它们为函数和类赋予了额外的超能力。本文将带你探索装饰器的秘密,了解如何利用这一工具来简化代码、增强可读性并提升效率。从基础概念到实际案例,我们将一步步揭示装饰器的神秘面纱,让你的代码更加优雅和强大。
|
3天前
|
设计模式 缓存 开发者
探索Python中的装饰器:提升代码复用性的利器
本文深入探讨了Python中强大的装饰器功能,揭示了其如何通过元编程和闭包等技术手段,优雅地实现代码的复用与扩展。从基本概念到高级应用,我们将一步步揭开装饰器背后的奥秘,并通过实例展示其在实际项目开发中的巨大价值。无论是想要简化函数调用流程、增强函数功能,还是实现AOP(面向切面编程),掌握装饰器都是每位Python开发者必备的技能。
|
4天前
|
缓存 开发者 Python
探索Python中的装饰器:简化代码,增强功能
【9月更文挑战第15天】本文将深入探讨Python中一个强大但常被误解的特性——装饰器。我们将从基础概念出发,逐步揭示装饰器如何简化代码结构,增加函数功能而无需修改其核心逻辑。通过具体示例,你将学会如何创建自定义装饰器,以及如何利用它们来管理权限、记录日志等。无论你是初学者还是有经验的开发者,这篇文章都将为你打开一扇提高代码效率和可维护性的新窗口。
|
2天前
|
缓存 监控 测试技术
探索Python中的装饰器:提升代码的灵活性和可维护性
本文深入探讨Python装饰器的概念、用法及优势。通过实例讲解如何利用装饰器增强函数功能、日志记录及性能测试,旨在帮助读者掌握这一强大的工具,提升编程效率与代码质量。
|
3天前
|
缓存 开发者 Python
探索Python中的装饰器:提升代码复用性与可读性
本文旨在深入探讨Python装饰器的概念、实现及其应用。通过实例分析,本文展示了如何利用装饰器提高代码的模块化和重用性,从而优化开发流程。我们将从装饰器的基本定义入手,逐步解析其工作机制,并通过案例展示如何在实际项目中有效利用装饰器。
9 0