自古真情留不住,唯有套路得人心。
今天是情人節,程式員如何撩動心儀女神的心?當然要用程式員的方法了!
這馬上給大家帶來一個Python的小套路:利用Python的pygame庫,生成一個套路神器。
無套路版本
無套路版本和抖音上的一些視訊差不多,就是點不了拒絕按鈕!是不是很實用捏?
詳細代碼如下:
import pygame
import random
import sys
# 根據背景圖大小,設定遊戲螢幕大小
WIDTH, HEIGHT = 1024, 576
# 不全屏
screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
# 全屏
# screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN, 32)
pygame.display.set_caption('小姐姐,你的快遞到了。')
# 添加文本資訊
def title(text, screen, scale, color=(0, 0, 0)):
font = pygame.font.SysFont('SimHei', 27)
textRender = font.render(text, True, color)
# 初始化文本的坐标
screen.blit(textRender, (WIDTH / scale[0], HEIGHT / scale[1]))
# 按鈕
def button(text, x, y, w, h, color, screen):
pygame.draw.rect(screen, color, (x, y, w, h))
font = pygame.font.SysFont('SimHei', 20)
textRender = font.render(text, True, (255, 255, 255))
textRect = textRender.get_rect()
textRect.center = ((x+w/2), (y+h/2))
screen.blit(textRender, textRect)
# 生成随機的位置坐标
def get_random_pos():
x, y = random.randint(10, 600), random.randint(20, 500)
return x, y
# 點選答應按鈕後顯示的頁面
def show_like_interface(screen):
screen.fill((255, 255, 255))
background1 = pygame.image.load('214_1.jpg').convert()
screen.blit(background1, (0, 0))
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
def main():
pygame.init()
clock = pygame.time.Clock()
# 添加背景音樂
pygame.mixer.music.load('214_1.mp3')
pygame.mixer.music.play(-1, 20)
pygame.mixer.music.set_volume(0.5)
# 設定不同意按鈕屬性
unlike_pos_x = 130
unlike_pos_y = 375
unlike_pos_width = 450
unlike_pos_height = 55
unlike_color = (115, 76, 243)
# 設定同意按鈕屬性
like_pos_x = 130
like_pos_y = 280
like_pos_width = 450
like_pos_height = 55
like_color = (115, 76, 243)
running = True
while running:
# 填充視窗
screen.fill((255, 255, 255))
# 添加背景圖
background = pygame.image.load('214_2.jpg').convert()
screen.blit(background, (0, 0))
# 擷取滑鼠坐标
pos = pygame.mouse.get_pos()
# 判斷滑鼠位置,不同意時,按鈕不斷變化
if pos[0] < unlike_pos_x + unlike_pos_width + 5 and pos[0] > unlike_pos_x - 5 and pos[1] < unlike_pos_y + unlike_pos_height + 5 and pos[1] > unlike_pos_y - 5:
while True:
unlike_pos_x, unlike_pos_y = get_random_pos()
if pos[0] < unlike_pos_x + unlike_pos_width + 5 and pos[0] > unlike_pos_x - 5 and pos[1] < unlike_pos_y + unlike_pos_height + 5 and pos[1] > unlike_pos_y - 5:
continue
break
# 設定标題及按鈕文本資訊
title('1.如果有一天我向你表白,你會怎麼樣?', screen, scale=[8, 3])
button('A.你小子終于開竅了,你敢表白我就敢答應!', like_pos_x, like_pos_y, like_pos_width, like_pos_height, like_color, screen)
button('B.我拿你當閨蜜,你居然想睡我!果斷拒絕!', unlike_pos_x, unlike_pos_y, unlike_pos_width, unlike_pos_height, unlike_color, screen)
# 設定關閉選項屬性
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# 當滑鼠點選同意按鈕後,跳轉結束頁面
if pos[0] < like_pos_x + like_pos_width + 5 and pos[0] > like_pos_x - 5 and pos[1] < like_pos_y + like_pos_height + 5 and pos[1] > like_pos_y - 5:
if event.type == pygame.MOUSEBUTTONDOWN:
show_like_interface(screen)
pygame.display.flip()
pygame.display.update()
clock.tick(60)
main()
代碼裡是設定有音樂的,是以運作代碼後,會有背景音樂。這裡因為我的電腦太渣,錄屏下的音質特别差,是以選擇錄制一個無聲的視訊,大家将就着看吧。
套路版本
或許上面的那個版本隻會讓小姐姐覺得你很可恨,畢竟強迫症患者有點多,愣是不讓人點,估摸着要挨小姐姐的揍。是以便有了下面這個套路版本,詳細代碼如下:
import pygame
import random
import sys
# 根據背景圖大小,設定遊戲螢幕大小
WIDTH, HEIGHT = 1024, 576
screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
pygame.display.set_caption('小姐姐,你的快遞到了。')
# 添加文本資訊
def title(text, screen, scale, color=(0, 0, 0)):
font = pygame.font.SysFont('SimHei', 27)
textRender = font.render(text, True, color)
# 初始化文字的坐标
screen.blit(textRender, (WIDTH / scale[0], HEIGHT / scale[1]))
# 按鈕
def button(text, x, y, w, h, color, screen, color_text):
pygame.draw.rect(screen, color, (x, y, w, h))
font = pygame.font.SysFont('SimHei', 20)
textRender = font.render(text, True, color_text)
textRect = textRender.get_rect()
textRect.center = ((x+w/2), (y+h/2))
screen.blit(textRender, textRect)
# 生成随機的位置坐标
def get_random_pos():
x, y = random.randint(20, 620), random.randint(20, 460)
return x, y
# 點選答應後顯示的頁面
def show_like_interface(screen):
screen.fill((255, 255, 255))
background1 = pygame.image.load('214_1.jpg').convert()
screen.blit(background1, (0, 0))
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# 點選不答應按鈕後顯示的頁面
def show_unlike_interface(screen):
screen.fill((255, 255, 255))
background_1 = pygame.image.load('214_3.jpg').convert()
screen.blit(background_1, (0, 0))
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
def main():
num = 0
pygame.init()
clock = pygame.time.Clock()
# 添加背景音樂
pygame.mixer.music.load('214_2.mp3')
pygame.mixer.music.play(-1, 40)
pygame.mixer.music.set_volume(0.5)
# 設定不同意按鈕屬性
unlike_pos_x = 130
unlike_pos_y = 375
unlike_pos_width = 450
unlike_pos_height = 55
unlike_color = (115, 76, 243)
# 設定同意按鈕屬性
like_pos_x = 130
like_pos_y = 280
like_pos_width = 450
like_pos_height = 55
like_color = (115, 76, 243)
running = True
while running:
# 填充視窗
screen.fill((255, 255, 255))
# 添加背景圖
background = pygame.image.load('214_2.jpg').convert()
screen.blit(background, (0, 0))
# 擷取滑鼠坐标
pos = pygame.mouse.get_pos()
if pos[0] < unlike_pos_x + unlike_pos_width + 5 and pos[0] > unlike_pos_x - 5 and pos[1] < unlike_pos_y + unlike_pos_height + 5 and pos[1] > unlike_pos_y - 5:
while True:
if num > 5:
break
num += 1
unlike_pos_x, unlike_pos_y = get_random_pos()
if pos[0] < unlike_pos_x + unlike_pos_width + 5 and pos[0] > unlike_pos_x - 5 and pos[1] < unlike_pos_y + unlike_pos_height + 5 and pos[1] > unlike_pos_y - 5:
continue
break
# 設定标題及按鈕文本資訊
title('1.如果有一天我向你表白,你會怎麼樣?', screen, scale=[8, 3])
button('A.你小子終于開竅了,你敢表白我就敢答應!', like_pos_x, like_pos_y, like_pos_width, like_pos_height, like_color, screen, (255, 255, 255))
# 設定小套路文本
if num < 6:
button('B.我拿你當閨蜜,你居然想睡我!果斷拒絕!', unlike_pos_x, unlike_pos_y, unlike_pos_width, unlike_pos_height, unlike_color, screen, (255, 255, 255))
if num > 5:
button('B. 我拿你當閨蜜,你居然想睡我!果斷答應!', unlike_pos_x, unlike_pos_y, unlike_pos_width, unlike_pos_height, unlike_color, screen, (255, 255, 255))
# 設定套路文本
if num == 1:
button('操作提示:請直接點選答案,切勿手抖!', unlike_pos_x, unlike_pos_y - 50, unlike_pos_width, unlike_pos_height, (255, 255, 255), screen, (192, 0, 0))
if num == 2:
button('咋又抖了?女神是不是哪裡不舒服?', unlike_pos_x, unlike_pos_y - 50, unlike_pos_width, unlike_pos_height, (255, 255, 255), screen, (192, 0, 0))
if num == 3:
button('呀!看來這病得還不輕啊!', unlike_pos_x, unlike_pos_y - 50, unlike_pos_width, unlike_pos_height, (255, 255, 255), screen, (192, 0, 0))
if num == 4:
button('生了病也沒人照料,心疼……', unlike_pos_x, unlike_pos_y - 50, unlike_pos_width, unlike_pos_height, (255, 255, 255), screen, (192, 0, 0))
if num == 5:
button('好險!差點兒就點到了呢!', unlike_pos_x, unlike_pos_y - 50, unlike_pos_width, unlike_pos_height, (255, 255, 255), screen, (192, 0, 0))
if num == 6:
button('哎,算了,不躲了,你選吧', unlike_pos_x, unlike_pos_y - 50, unlike_pos_width, unlike_pos_height, (255, 255, 255), screen, (192, 0, 0))
# 點選套路按鈕
if num > 5:
if pos[0] < unlike_pos_x + unlike_pos_width + 5 and pos[0] > unlike_pos_x - 5 and pos[1] < unlike_pos_y + unlike_pos_height + 5 and pos[1] > unlike_pos_y - 5:
if event.type == pygame.MOUSEBUTTONDOWN:
show_unlike_interface(screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# 點選同意按鈕
if pos[0] < like_pos_x + like_pos_width + 5 and pos[0] > like_pos_x - 5 and pos[1] < like_pos_y + like_pos_height + 5 and pos[1] > like_pos_y - 5:
if event.type == pygame.MOUSEBUTTONDOWN:
show_like_interface(screen)
pygame.display.flip()
pygame.display.update()
clock.tick(60)
main()
效果如下:
打包程式
看了上面一行行的代碼,我們能直接交給女神嗎?那樣無疑是自取滅亡。
是以使用pyinstaller庫将代碼、圖檔及音樂素材打包成exe檔案。
直接點選love.exe程式,即可運作。
小姐姐,你的快遞到了。
什麼快遞?
我的一廂情願。
相關的圖檔,音樂、代碼以及打包程式已上傳公衆号,菜單欄加微信即可擷取。
完整項目代碼擷取點這即可