曾梦想仗剑走天涯,如今只做了个苦逼的程序猿,今天是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