您当前的位置:首页 > IT编程 > python
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch | 异常检测 | Transformers | 情感分类 | 知识图谱 |

自学教程:我在七夕佳节用Python制作的表白神器,程序员也应该拥有爱情!建议收藏

51自学网 2021-10-30 22:20:51
  python
这篇教程我在七夕佳节用Python制作的表白神器,程序员也应该拥有爱情!建议收藏写得很实用,希望能帮到您。

前言

七夕佳节又双叒叕来了!
七夕来了,指南也总得送点什么给大家表示一下,在这个洋溢着甜美爱情的节日里,程序员也应该拥有爱情!今天在这里就给大家分享一个Python仿制抖音表白小软件
废话不多说,让我们看似“愉快”地开始吧~

效果展示

普通人表白

程序员表白

开发工具

Python版本: 3.6.4

相关模块:

requests模块;

argparse模块;

pyquery模块;

jieba模块;

pyecharts模块;

wordcloud模块;

以及一些Python自带的模块。

原理简介

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

'''Function:  按钮类Initial Args:  --x, y: 按钮左上角坐标  --width, height: 按钮宽高  --text: 按钮显示的文字  --fontpath: 字体路径  --fontsize: 字体大小  --fontcolor: 字体颜色  --bgcolors: 按钮背景颜色  --is_want_to_be_selected: 按钮是否想被玩家选中  --screensize: 软件屏幕大小'''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 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)

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

到此这篇关于我在七夕佳节用Python制作的表白神器,程序员也应该拥有爱情!建议收藏的文章就介绍到这了,更多相关Python表白程序内容请搜索51zixue.net以前的文章或继续浏览下面的相关文章希望大家以后多多支持51zixue.net!


一篇文章带你了解python异常基础
pythotn条件分支与循环详解
万事OK自学网:51自学网_软件自学网_CAD自学网自学excel、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。