曾夢想仗劍走天涯,如今只做了個(gè)苦逼的程序猿,今天是520,又到了遍地狗糧、單身狗哀嚎的日子了,在公眾號(一行數(shù)據(jù))上看到一篇文章教人用python表白,看到之后小弟立刻激動(dòng)了,看誰還敢說程序猿沒情調(diào)?!廢話不多說,跟隨大佬操作一番。
一行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)]))
單純是用來表白的話可以直接復(fù)制代碼到python環(huán)境中點(diǎn)擊enter鍵就好了呦!效果如下:

一箭穿心
當(dāng)然我們也可以使用python的turtle工具包來畫出丘比特的一箭穿心
import turtle as t
def go_to(x, y):
t.up()
t.goto(x, y)
t.down()
def big_Circle(size): # 函數(shù)用于繪制心的大圓
for i in range(150):
t.forward(size)
t.right(0.3)
def small_Circle(size): # 函數(shù)用于繪制心的小圓
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) # 畫出第一顆心,前面兩個(gè)參數(shù)控制心的位置,函數(shù)最后一個(gè)參數(shù)可控制心的大小
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()
效果圖如下:

愛情命運(yùn)選擇題
import sys
import random
import pygame
from pygame.locals import *
# 背景大小、顏色
WIDTH, HEIGHT = 640, 360
BACKGROUND = (255, 255, 255)
button_text_list = ['你再想想', '我養(yǎng)你', '好吃的都給你', '保大', '房產(chǎn)證給你', '我媽會(huì)游泳', '我會(huì)修電腦', '我會(huì)寫代碼']
# 按鈕
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)
# 標(biāo)題
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)
# 生成隨機(jī)的位置坐標(biāo)
def get_random_pos():
x, y = random.randint(20, WIDTH - 20), random.randint(20, HEIGHT - 20)
return x, y
# 點(diǎn)擊喜歡按鈕后顯示的頁面
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()
# 主函數(shù)
def main():
text = '算了吧'
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
pygame.display.set_caption('FROM一個(gè)喜歡你很久的小哥哥')
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)
# 若不點(diǎn)擊喜歡按鈕,就一直運(yùn)行
while running:
screen.fill(BACKGROUND)
# 加載圖片
img = pygame.image.load("./imgs/3.jpg")
imgRect = img.get_rect()
# 圖片位置
imgRect.midtop = 80, 10
screen.blit(img, imgRect)
# 監(jiān)聽事件
for event in pygame.event.get():
# 檢測到鼠標(biāo)
if event.type == pygame.MOUSEBUTTONDOWN:
# 獲取鼠標(biāo)位置
mouse_pos = pygame.mouse.get_pos()
# 若點(diǎn)擊了喜歡按鈕,停止 while 循環(huán)
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
# 獲取鼠標(biāo)位置
# 若鼠標(biāo)位置位于按鈕區(qū)域內(nèi)
# 則隨機(jī)生成按鈕位置進(jìn)行顯示
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()
效果圖如下:

這個(gè)時(shí)候我們一串代碼發(fā)過去,估計(jì)最大的可能性直接忽略或者以為是病毒把它刪了,所以讓小姐姐能夠立馬上手運(yùn)行至關(guān)重要,同時(shí)也會(huì)帶著一份神秘感,可以選擇將這個(gè)選擇題制作成exe讓小姐姐們使用,過程很簡單在cmd里輸入這一行代碼即可轉(zhuǎn)成可執(zhí)行文件,讓小姐姐感受不可違背的命運(yùn)抉擇
pyinstaller -F -i tubiao.ico main.py -n 命運(yùn)的抉擇 --noconsole
其中使用的圖片3.jpg

當(dāng)然還有些字體文件,請關(guān)注大佬的公眾號(一行代碼)
注:
1、本文代碼非本人原創(chuàng),原創(chuàng)為公眾號(一行代碼)
2、本文及代碼僅用于自我學(xué)習(xí)
如果第三段代碼打包后點(diǎn)擊運(yùn)行文件失敗,可以在打包時(shí)去掉 -w 以方便查看報(bào)錯(cuò)問題,如果是圖片字體加載失敗的問題,可以選擇使用絕對路徑,并重新打包以查看效果。
好啦就到這里了,小哥哥已經(jīng)去表白啦
https://github.com/super-GY/WeChatOfficialAccount/tree/master/20191108%22%E8%A1%A8%E7%99%BD%E7%A5%9E%E5%99%A8%22