前言
明天就是擁抱情人節,情侶們會在公開的場合擁抱,向世人宣告你倆的愛意,也讓這個寒冷的冬天變得格外溫馨,到了年底依然能熱情擁抱,也見證了兩人情意如昔,
今天子川就給大家帶來就是的利用Python制作表白神器,記得發給自己的心儀物件,廢話不多說直接開整~

開發工具
Python版本: 3.6
相關模塊:
random模塊
pygame模塊
cfg模塊
sys模塊
tkinter模塊
環境搭建
安裝Python并添加到環境變數,pip安裝需要的相關模塊即可,
文中圖片素材實戰教程,評論留言獲取,

代碼實作
import sys
import cfg
import random
import pygame
from tkinter import Tk, messagebox
class Button(pygame.sprite.Sprite):
def __init__(self, x, y, width, height, text, fontpath, fontsize, fontcolor, bgcolors, edgecolor, edgesize=1, is_want_to_be_selected=True, screensize=None, **kwargs):
pygame.sprite.Sprite.__init__(self)
self.rect = pygame.Rect(x, y, width, height)
self.text = text
self.font = pygame.font.Font(fontpath, fontsize)
self.fontcolor = fontcolor
self.bgcolors = bgcolors
self.edgecolor = edgecolor
self.edgesize = edgesize
self.is_want_tobe_selected = is_want_to_be_selected
self.screensize = screensize
#自動根據各種情況將按鈕系結到螢屏
def draw(self, screen, mouse_pos):
# 滑鼠在按鈕范圍內
if self.rect.collidepoint(mouse_pos):
# --不想被選中
if not self.is_want_tobe_selected:
while self.rect.collidepoint(mouse_pos):
self.rect.left, self.rect.top = random.randint(0, self.screensize[0]-self.rect.width), random.randint(0, self.screensize[1]-self.rect.height)
pygame.draw.rect(screen, self.bgcolors[0], self.rect, 0)
pygame.draw.rect(screen, self.edgecolor, self.rect, self.edgesize)
# 滑鼠不在按鈕范圍內
else:
pygame.draw.rect(screen, self.bgcolors[1], self.rect, 0)
pygame.draw.rect(screen, self.edgecolor, self.rect, self.edgesize)
text_render = self.font.render(self.text, True, self.fontcolor)
fontsize = self.font.size(self.text)
screen.blit(text_render, (self.rect.x+(self.rect.width-fontsize[0])/2, self.rect.y+(self.rect.height-fontsize[1])/2))
#在指定位置顯示文字'
def showText(screen, text, position, fontpath, fontsize, fontcolor, is_bold=False):
font = pygame.font.Font(fontpath, fontsize)
font.set_bold(is_bold)
text_render = font.render(text, True, fontcolor)
screen.blit(text_render, position)
剩余代碼
'''主函式'''
def main():
# 初始化
pygame.init()
screen = pygame.display.set_mode(cfg.SCREENSIZE, 0, 32)
pygame.display.set_icon(pygame.image.load(cfg.ICON_IMAGE_PATH))
pygame.display.set_caption('來自一位喜歡你的小哥哥')
# 背景音樂
pygame.mixer.music.load(cfg.BGM_PATH)
pygame.mixer.music.play(-1, 30.0)
# biu愛心那個背景圖片
bg_image = pygame.image.load(cfg.BG_IMAGE_PATH)
bg_image = pygame.transform.smoothscale(bg_image, (150, 150))
# 實體化兩個按鈕
button_yes = Button(x=20, y=cfg.SCREENSIZE[1]-70, width=120, height=35,
text='好呀', fontpath=cfg.FONT_PATH, fontsize=15, fontcolor=cfg.BLACK, edgecolor=cfg.SKYBLUE,
edgesize=2, bgcolors=[cfg.DARKGRAY, cfg.GAINSBORO], is_want_to_be_selected=True, screensize=cfg.SCREENSIZE)
button_no = Button(x=cfg.SCREENSIZE[0]-140, y=cfg.SCREENSIZE[1]-70, width=120, height=35,
text='算了吧', fontpath=cfg.FONT_PATH, fontsize=15, fontcolor=cfg.BLACK, edgecolor=cfg.DARKGRAY,
edgesize=1, bgcolors=[cfg.DARKGRAY, cfg.GAINSBORO], is_want_to_be_selected=False, screensize=cfg.SCREENSIZE)
# 是否點擊了好呀按鈕
is_agree = False
# 主回圈
clock = pygame.time.Clock()
while True:
# --背景圖片
screen.fill(cfg.WHITE)
screen.blit(bg_image, (cfg.SCREENSIZE[0]-bg_image.get_height(), 0))
# --滑鼠事件捕獲
for event in pygame.event.get():
if event.type == pygame.QUIT:
# ----沒有點擊好呀按鈕之前不許退出程式
if is_agree:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button:
if button_yes.rect.collidepoint(pygame.mouse.get_pos()):
button_yes.is_selected = True
root = Tk()
root.withdraw()
messagebox.showinfo('', '???么么噠???')
root.destroy()
is_agree = True
# --顯示文字
showText(screen=screen, text='小姐姐, 我觀察你很久了', position=(40, 50),
fontpath=cfg.FONT_PATH, fontsize=25, fontcolor=cfg.BLACK, is_bold=False)
showText(screen=screen, text='做我女朋友好不好?', position=(40, 100),
fontpath=cfg.FONT_PATH, fontsize=25, fontcolor=cfg.BLACK, is_bold=True)
# --顯示按鈕
button_yes.draw(screen, pygame.mouse.get_pos())
button_no.draw(screen, pygame.mouse.get_pos())
# --重繪
pygame.display.update()
clock.tick(60)
#run
if __name__ == '__main__':
main()
效果展示

最后
今天的分享到這里就結束了 ,感興趣的朋友也可以去試試哈
對文章有問題的,或者有其他關于python的問題,可以在評論區留言或者私信我哦
覺得我分享的文章不錯的話,可以關注一下我,或者給文章點贊(/≧▽≦)/
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/540109.html
標籤:Python
下一篇:python模擬股票的資料分析
