用python来表白

曾梦想仗剑走天涯,如今只做了个苦逼的程序猿,今天是520,又到了遍地狗粮、单身狗哀嚎的日子了,在公众号(一行数据)上看到一篇文章教人用python表白,看到之后小弟立刻激动了,看谁还敢说程序猿没情调?!废话不多说,跟随大佬操作一番。

一行python代码绘制爱心

print('\n'.join([''.join([('Love'[(x-y) % len('Love')] if ((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3 <= 0 else ' ') for x in range(-30, 30)]) for y in range(30, -30, -1)]))

单纯是用来表白的话可以直接复制代码到python环境中点击enter键就好了呦!效果如下:


love.png

一箭穿心

当然我们也可以使用python的turtle工具包来画出丘比特的一箭穿心

import turtle as t


def go_to(x, y):
    t.up()
    t.goto(x, y)
    t.down()


def big_Circle(size):  # 函数用于绘制心的大圆
    for i in range(150):
        t.forward(size)
        t.right(0.3)


def small_Circle(size):  # 函数用于绘制心的小圆
    for i in range(210):
        t.forward(size)
        t.right(0.786)


def line(size):
    t.forward(51 * size)


def heart(x, y, size):
    go_to(x, y)
    t.left(150)
    t.begin_fill()
    line(size)
    big_Circle(size)
    small_Circle(size)
    t.left(120)
    small_Circle(size)
    big_Circle(size)
    line(size)
    t.end_fill()


def arrow():
    t.pensize(10)
    t.setheading(0)
    go_to(-400, 0)
    t.left(15)
    t.forward(150)
    go_to(339, 178)
    t.forward(150)


def arrowHead():
    t.pensize(1)
    t.color('red', 'red')
    t.begin_fill()
    t.left(120)
    t.forward(20)
    t.right(150)
    t.forward(35)
    t.right(120)
    t.forward(35)
    t.right(150)
    t.forward(20)
    t.end_fill()


def main():
    t.setup()
    t.screensize(1000, 600, )
    t.pensize(2)
    t.color('red', 'pink')
    t.getscreen().tracer(30, 0)  # 取消注释后,快速显示图案
    heart(200, 0, 1)  # 画出第一颗心,前面两个参数控制心的位置,函数最后一个参数可控制心的大小
    t.setheading(0)  # 使画笔的方向朝向x轴正方向
    heart(-80, -100, 1.5)  # 画出第二颗心
    arrow()  # 画出穿过两颗心的直线
    arrowHead()  # 画出箭的箭头
    go_to(100, -300)
    t.write("author:小哥哥❤️爱你呦", move=True, align="left", font=(None, 30, "normal"))
    t.done()


if __name__ == '__main__':
    main()

效果图如下:


image.png

爱情命运选择题

import sys
import random
import pygame
from pygame.locals import *

# 背景大小、颜色
WIDTH, HEIGHT = 640, 360
BACKGROUND = (255, 255, 255)
button_text_list = ['你再想想', '我养你', '好吃的都给你', '保大', '房产证给你', '我妈会游泳', '我会修电脑', '我会写代码']


# 按钮
def button(text, x, y, w, h, color, screen):
    pygame.draw.rect(screen, color, (x, y, w, h))
    font = pygame.font.Font('./font/ziti.ttf', 20)
    textRender = font.render(text, True, (0, 0, 0))
    textRect = textRender.get_rect()
    textRect.center = ((x + w / 2), (y + h / 2))
    screen.blit(textRender, textRect)


# 标题
def title(text, screen, scale, color=(0, 0, 0)):
    font = pygame.font.Font('./font/ziti.ttf', WIDTH // (len(text) * 2))
    textRender = font.render(text, True, color)
    textRect = textRender.get_rect()
    textRect.midtop = (WIDTH / scale[0], HEIGHT / scale[1])
    screen.blit(textRender, textRect)


# 生成随机的位置坐标
def get_random_pos():
    x, y = random.randint(20, WIDTH - 20), random.randint(20, HEIGHT - 20)
    return x, y


# 点击喜欢按钮后显示的页面
def show_like_interface(text, screen, color=(255, 0, 0)):
    screen.fill(BACKGROUND)
    font = pygame.font.Font('./font/simkai.ttf', WIDTH // (len(text)))
    textRender = font.render(text, True, color)
    textRect = textRender.get_rect()
    textRect.midtop = (WIDTH / 2, HEIGHT / 2)
    screen.blit(textRender, textRect)
    pygame.display.update()
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()


# 主函数
def main():
    text = '算了吧'
    pygame.init()
    screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
    pygame.display.set_caption('FROM一个喜欢你很久的小哥哥')
    clock = pygame.time.Clock()

    # 不喜欢按钮的初始位置和大小
    unlike_pos_x = 330
    unlike_pos_y = 250
    unlike_pos_width = 100
    unlike_pos_height = 50

    # 喜欢按钮的初始位置和大小
    like_pos_x = 180
    like_pos_y = 250
    like_pos_width = 100
    like_pos_height = 50
    running = True

    # 按钮颜色
    like_color = (216, 191, 216)

    # 若不点击喜欢按钮,就一直运行
    while running:
        screen.fill(BACKGROUND)

        # 加载图片
        img = pygame.image.load("./imgs/3.jpg")
        imgRect = img.get_rect()
        # 图片位置
        imgRect.midtop = 80, 10
        screen.blit(img, imgRect)

        # 监听事件
        for event in pygame.event.get():
            # 检测到鼠标
            if event.type == pygame.MOUSEBUTTONDOWN:
                # 获取鼠标位置
                mouse_pos = pygame.mouse.get_pos()
                # 若点击了喜欢按钮,停止 while 循环
                if like_pos_x + like_pos_width > mouse_pos[0] > like_pos_x and \
                        like_pos_y + like_pos_height > mouse_pos[1] > like_pos_y:
                    like_color = BACKGROUND
                    running = False
        # 获取鼠标位置
        # 若鼠标位置位于按钮区域内
        # 则随机生成按钮位置进行显示
        mouse_pos = pygame.mouse.get_pos()
        if unlike_pos_x + unlike_pos_width > mouse_pos[0] > unlike_pos_x and \
                unlike_pos_y + unlike_pos_height > mouse_pos[1] > unlike_pos_y:
            while True:
                unlike_pos_x, unlike_pos_y = get_random_pos()
                text = button_text_list[random.randint(0, len(button_text_list) - 1)]
                if unlike_pos_x + unlike_pos_width > mouse_pos[0] > unlike_pos_x and \
                        unlike_pos_y + unlike_pos_height > mouse_pos[1] > unlike_pos_y:
                    continue
                break
        title('小姐姐,我观察你很久了', screen, scale=[1.8, 10])
        title('做我女朋友好不好呀?', screen, scale=[1.8, 3])
        button('好呀', like_pos_x, like_pos_y, like_pos_width,
               like_pos_height, like_color, screen)
        button(text, unlike_pos_x, unlike_pos_y, unlike_pos_width,
               unlike_pos_height, (216, 191, 216), screen)
        pygame.display.flip()
        pygame.display.update()
        clock.tick(60)
    show_like_interface('我就知道小姐姐你也喜欢我~', screen, color=(0, 0, 0))


if __name__ == '__main__':
    main()

效果图如下:


image.png

这个时候我们一串代码发过去,估计最大的可能性直接忽略或者以为是病毒把它删了,所以让小姐姐能够立马上手运行至关重要,同时也会带着一份神秘感,可以选择将这个选择题制作成exe让小姐姐们使用,过程很简单在cmd里输入这一行代码即可转成可执行文件,让小姐姐感受不可违背的命运抉择

pyinstaller -F -i tubiao.ico main.py -n 命运的抉择 --noconsole

其中使用的图片3.jpg


image.png

当然还有些字体文件,请关注大佬的公众号(一行代码)

注:
1、本文代码非本人原创,原创为公众号(一行代码)
2、本文及代码仅用于自我学习

如果第三段代码打包后点击运行文件失败,可以在打包时去掉 -w 以方便查看报错问题,如果是图片字体加载失败的问题,可以选择使用绝对路径,并重新打包以查看效果。

好啦就到这里了,小哥哥已经去表白啦
https://github.com/super-GY/WeChatOfficialAccount/tree/master/20191108%22%E8%A1%A8%E7%99%BD%E7%A5%9E%E5%99%A8%22

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335