关键词搜索

源码搜索 ×
×

❤️七夕佳节,用Python制作表白神器,程序员也应该拥有爱情!【附源码,建议收藏】❤️

发布2021-08-20浏览700次

详情内容

程序员表白

在这里插入图片描述

开发工具

Python版本: 3.6.4

相关模块:

requests模块;

argparse模块;

pyquery模块;

jieba模块;

pyecharts模块;

wordcloud模块;

以及一些Python自带的模块。

原理简介

具体而言,首先我们来定义一个按钮类,其功能是可以根据初始化参数生成一个界面上按钮,且这个按钮是否可以被点击到也由传入的初始化参数决定,具体而言代码实现如下:

  1. '''
  2. Function:
  3. 按钮类
  4. Initial Args:
  5. --x, y: 按钮左上角坐标
  6. --width, height: 按钮宽高
  7. --text: 按钮显示的文字
  8. --fontpath: 字体路径
  9. --fontsize: 字体大小
  10. --fontcolor: 字体颜色
  11. --bgcolors: 按钮背景颜色
  12. --is_want_to_be_selected: 按钮是否想被玩家选中
  13. --screensize: 软件屏幕大小
  14. '''
  15. class Button(pygame.sprite.Sprite):
  16. def __init__(self, x, y, width, height, text, fontpath, fontsize, fontcolor, bgcolors, edgecolor, edgesize=1, is_want_to_be_selected=True, screensize=None, **kwargs):
  17. pygame.sprite.Sprite.__init__(self)
  18. self.rect = pygame.Rect(x, y, width, height)
  19. self.text = text
  20. self.font = pygame.font.Font(fontpath, fontsize)
  21. self.fontcolor = fontcolor
  22. self.bgcolors = bgcolors
  23. self.edgecolor = edgecolor
  24. self.edgesize = edgesize
  25. self.is_want_tobe_selected = is_want_to_be_selected
  26. self.screensize = screensize
  27. '''自动根据各种情况将按钮绑定到屏幕'''
  28. def draw(self, screen, mouse_pos):
  29. # 鼠标在按钮范围内
  30. if self.rect.collidepoint(mouse_pos):
  31. # --不想被选中
  32. if not self.is_want_tobe_selected:
  33. while self.rect.collidepoint(mouse_pos):
  34. self.rect.left, self.rect.top = random.randint(0, self.screensize[0]-self.rect.width), random.randint(0, self.screensize[1]-self.rect.height)
  35. pygame.draw.rect(screen, self.bgcolors[0], self.rect, 0)
  36. pygame.draw.rect(screen, self.edgecolor, self.rect, self.edgesize)
  37. # 鼠标不在按钮范围内
  38. else:
  39. pygame.draw.rect(screen, self.bgcolors[1], self.rect, 0)
  40. pygame.draw.rect(screen, self.edgecolor, self.rect, self.edgesize)
  41. text_render = self.font.render(self.text, True, self.fontcolor)
  42. fontsize = self.font.size(self.text)
  43. screen.blit(text_render, (self.rect.x+(self.rect.width-fontsize[0])/2, self.rect.y+(self.rect.height-fontsize[1])/2))

其实就是看看鼠标的当前位置有没有在按钮所在的范围内,如果在且设置的不让用户可以点击到该按钮,就自动地移动按钮的位置,使鼠标位置不在移动后的按钮所在的范围内。
然后写个主循环,把界面大小,配色,布局啥的弄的稍微走心一点:

  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)

ps:记得设置个flag,对方没点击“好呀”按钮之前,不要让对方可以关闭这个小程序就好啦~

文章到这里就结束了,感谢你的观看,下篇文章python教程分享Python网络安全系列

为了感谢读者们,我想把我最近收藏的一些编程干货分享给大家,回馈每一个读者,希望能帮到你们。

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载