【脚本项目源码】Python制作提升成功率90%的表白神器

打印 上一主题 下一主题

主题 1029|帖子 1029|积分 3087

前言

今天子川就给大家带来就是的利用Python制作表白神器,让这个寒冷的冬天变得格外温馨,到了年底依然能热情拥抱,也见证了两人情意如昔;记得发给自己的心仪对象,废话不多说直接开整~

开发工具

Python版本: 3.6
相关模块:
random模块
pygame模块
cfg模块
sys模块
tkinter模块
环境搭建

安装Python并添加到环境变量,pip安装需要的相关模块即可。
文中图片素材实战教程,评论留言获取。
.

代码实现
  1. import sys
  2. import cfg
  3. import random
  4. import pygame
  5. from tkinter import Tk, messagebox
  6. class Button(pygame.sprite.Sprite):
  7.         def __init__(self, x, y, width, height, text, fontpath, fontsize, fontcolor, bgcolors, edgecolor, edgesize=1, is_want_to_be_selected=True, screensize=None, **kwargs):
  8.                 pygame.sprite.Sprite.__init__(self)
  9.                 self.rect = pygame.Rect(x, y, width, height)
  10.                 self.text = text
  11.                 self.font = pygame.font.Font(fontpath, fontsize)
  12.                 self.fontcolor = fontcolor
  13.                 self.bgcolors = bgcolors
  14.                 self.edgecolor = edgecolor
  15.                 self.edgesize = edgesize
  16.                 self.is_want_tobe_selected = is_want_to_be_selected
  17.                 self.screensize = screensize
  18.         #自动根据各种情况将按钮绑定到屏幕
  19.         def draw(self, screen, mouse_pos):
  20.                 # 鼠标在按钮范围内
  21.                 if self.rect.collidepoint(mouse_pos):
  22.                         # --不想被选中
  23.                         if not self.is_want_tobe_selected:
  24.                                 while self.rect.collidepoint(mouse_pos):
  25.                                         self.rect.left, self.rect.top = random.randint(0, self.screensize[0]-self.rect.width), random.randint(0, self.screensize[1]-self.rect.height)
  26.                         pygame.draw.rect(screen, self.bgcolors[0], self.rect, 0)
  27.                         pygame.draw.rect(screen, self.edgecolor, self.rect, self.edgesize)
  28.                 # 鼠标不在按钮范围内
  29.                 else:
  30.                         pygame.draw.rect(screen, self.bgcolors[1], self.rect, 0)
  31.                         pygame.draw.rect(screen, self.edgecolor, self.rect, self.edgesize)
  32.                 text_render = self.font.render(self.text, True, self.fontcolor)
  33.                 fontsize = self.font.size(self.text)
  34.                 screen.blit(text_render, (self.rect.x+(self.rect.width-fontsize[0])/2, self.rect.y+(self.rect.height-fontsize[1])/2))
  35. #在指定位置显示文字'
  36. def showText(screen, text, position, fontpath, fontsize, fontcolor, is_bold=False):
  37.         font = pygame.font.Font(fontpath, fontsize)
  38.         font.set_bold(is_bold)
  39.         text_render = font.render(text, True, fontcolor)
  40.         screen.blit(text_render, position)
复制代码
剩余代码
  1. '''主函数'''
  2. def main():
  3.         # 初始化
  4.         pygame.init()
  5.         screen = pygame.display.set_mode(cfg.SCREENSIZE, 0, 32)
  6.         pygame.display.set_icon(pygame.image.load(cfg.ICON_IMAGE_PATH))
  7.         pygame.display.set_caption('来自一位喜欢你的小哥哥')
  8.         # 背景音乐
  9.         pygame.mixer.music.load(cfg.BGM_PATH)
  10.         pygame.mixer.music.play(-1, 30.0)
  11.         # biu爱心那个背景图片
  12.         bg_image = pygame.image.load(cfg.BG_IMAGE_PATH)
  13.         bg_image = pygame.transform.smoothscale(bg_image, (150, 150))
  14.         # 实例化两个按钮
  15.         button_yes = Button(x=20, y=cfg.SCREENSIZE[1]-70, width=120, height=35,
  16.                                                 text='好呀', fontpath=cfg.FONT_PATH, fontsize=15, fontcolor=cfg.BLACK, edgecolor=cfg.SKYBLUE,
  17.                                                 edgesize=2, bgcolors=[cfg.DARKGRAY, cfg.GAINSBORO], is_want_to_be_selected=True, screensize=cfg.SCREENSIZE)
  18.         button_no = Button(x=cfg.SCREENSIZE[0]-140, y=cfg.SCREENSIZE[1]-70, width=120, height=35,
  19.                                            text='算了吧', fontpath=cfg.FONT_PATH, fontsize=15, fontcolor=cfg.BLACK, edgecolor=cfg.DARKGRAY,
  20.                                            edgesize=1, bgcolors=[cfg.DARKGRAY, cfg.GAINSBORO], is_want_to_be_selected=False, screensize=cfg.SCREENSIZE)
  21.         # 是否点击了好呀按钮
  22.         is_agree = False
  23.         # 主循环
  24.         clock = pygame.time.Clock()
  25.         while True:
  26.                 # --背景图片
  27.                 screen.fill(cfg.WHITE)
  28.                 screen.blit(bg_image, (cfg.SCREENSIZE[0]-bg_image.get_height(), 0))
  29.                 # --鼠标事件捕获
  30.                 for event in pygame.event.get():
  31.                         if event.type == pygame.QUIT:
  32.                                 # ----没有点击好呀按钮之前不许退出程序
  33.                                 if is_agree:
  34.                                         pygame.quit()
  35.                                         sys.exit()
  36.                         elif event.type == pygame.MOUSEBUTTONDOWN and event.button:
  37.                                 if button_yes.rect.collidepoint(pygame.mouse.get_pos()):
  38.                                         button_yes.is_selected = True
  39.                                         root = Tk()
  40.                                         root.withdraw()
  41.                                         messagebox.showinfo('', '❤❤❤么么哒❤❤❤')
  42.                                         root.destroy()
  43.                                         is_agree = True
  44.                 # --显示文字
  45.                 showText(screen=screen, text='小姐姐, 我观察你很久了', position=(40, 50),
  46.                                  fontpath=cfg.FONT_PATH, fontsize=25, fontcolor=cfg.BLACK, is_bold=False)
  47.                 showText(screen=screen, text='做我女朋友好不好?', position=(40, 100),
  48.                                  fontpath=cfg.FONT_PATH, fontsize=25, fontcolor=cfg.BLACK, is_bold=True)
  49.                 # --显示按钮
  50.                 button_yes.draw(screen, pygame.mouse.get_pos())
  51.                 button_no.draw(screen, pygame.mouse.get_pos())
  52.                 # --刷新
  53.                 pygame.display.update()
  54.                 clock.tick(60)
  55. #run
  56. if __name__ == '__main__':
  57.         main()
复制代码
效果展示


最后

今天的分享到这里就结束了 ,感兴趣的朋友也可以去试试哈
对文章有问题的,或者有其他关于python的问题,可以在评论区留言或者私信我哦
觉得我分享的文章不错的话,可以关注一下我,或者给文章点赞(/≧▽≦)/

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

老婆出轨

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表