濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > 我在七夕佳節(jié)用Python制作的表白神器,程序員也應(yīng)該擁有愛情!建議收藏

我在七夕佳節(jié)用Python制作的表白神器,程序員也應(yīng)該擁有愛情!建議收藏

熱門標(biāo)簽:外呼并發(fā)線路 長(zhǎng)沙高頻外呼系統(tǒng)原理是什么 西藏房產(chǎn)智能外呼系統(tǒng)要多少錢 ai電話機(jī)器人哪里好 宿遷星美防封電銷卡 湛江智能外呼系統(tǒng)廠家 ai電銷機(jī)器人源碼 地圖標(biāo)注審核表 百度地圖標(biāo)注沒有了

前言

七夕佳節(jié)又雙叒叕來了!
七夕來了,指南也總得送點(diǎn)什么給大家表示一下,在這個(gè)洋溢著甜美愛情的節(jié)日里,程序員也應(yīng)該擁有愛情!今天在這里就給大家分享一個(gè)Python仿制抖音表白小軟件
廢話不多說,讓我們看似“愉快”地開始吧~

效果展示

普通人表白

程序員表白

開發(fā)工具

Python版本: 3.6.4

相關(guān)模塊:

requests模塊;

argparse模塊;

pyquery模塊;

jieba模塊;

pyecharts模塊;

wordcloud模塊;

以及一些Python自帶的模塊。

原理簡(jiǎn)介

具體而言,首先我們來定義一個(gè)按鈕類,其功能是可以根據(jù)初始化參數(shù)生成一個(gè)界面上按鈕,且這個(gè)按鈕是否可以被點(diǎn)擊到也由傳入的初始化參數(shù)決定,具體而言代碼實(shí)現(xiàn)如下:

'''
Function:
  按鈕類
Initial Args:
  --x, y: 按鈕左上角坐標(biāo)
  --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
  '''自動(dòng)根據(jù)各種情況將按鈕綁定到屏幕'''
  def draw(self, screen, mouse_pos):
    # 鼠標(biāo)在按鈕范圍內(nèi)
    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)
    # 鼠標(biāo)不在按鈕范圍內(nèi)
    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))

其實(shí)就是看看鼠標(biāo)的當(dāng)前位置有沒有在按鈕所在的范圍內(nèi),如果在且設(shè)置的不讓用戶可以點(diǎn)擊到該按鈕,就自動(dòng)地移動(dòng)按鈕的位置,使鼠標(biāo)位置不在移動(dòng)后的按鈕所在的范圍內(nèi)。
然后寫個(gè)主循環(huán),把界面大小,配色,布局啥的弄的稍微走心一點(diǎn):

'''主函數(shù)'''
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愛心那個(gè)背景圖片
  bg_image = pygame.image.load(cfg.BG_IMAGE_PATH)
  bg_image = pygame.transform.smoothscale(bg_image, (150, 150))
  # 實(shí)例化兩個(gè)按鈕
  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)
  # 是否點(diǎn)擊了好呀按鈕
  is_agree = False
  # 主循環(huán)
  clock = pygame.time.Clock()
  while True:
    # --背景圖片
    screen.fill(cfg.WHITE)
    screen.blit(bg_image, (cfg.SCREENSIZE[0]-bg_image.get_height(), 0))
    # --鼠標(biāo)事件捕獲
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
        # ----沒有點(diǎn)擊好呀按鈕之前不許退出程序
        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:記得設(shè)置個(gè)flag,對(duì)方?jīng)]點(diǎn)擊“好呀”按鈕之前,不要讓對(duì)方可以關(guān)閉這個(gè)小程序就好啦~

到此這篇關(guān)于我在七夕佳節(jié)用Python制作的表白神器,程序員也應(yīng)該擁有愛情!建議收藏的文章就介紹到這了,更多相關(guān)Python表白程序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • python練習(xí)之曾經(jīng)很火的小人畫愛心表白代碼
  • Python浪漫玫瑰盛開表白源代碼

標(biāo)簽:盤錦 南平 普洱 林芝 大同 寧夏 漯河 海南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《我在七夕佳節(jié)用Python制作的表白神器,程序員也應(yīng)該擁有愛情!建議收藏》,本文關(guān)鍵詞  我,在,七夕,佳,節(jié)用,Python,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《我在七夕佳節(jié)用Python制作的表白神器,程序員也應(yīng)該擁有愛情!建議收藏》相關(guān)的同類信息!
  • 本頁收集關(guān)于我在七夕佳節(jié)用Python制作的表白神器,程序員也應(yīng)該擁有愛情!建議收藏的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    陵川县| 石嘴山市| 绿春县| 平潭县| 虞城县| 华容县| 定陶县| 门头沟区| 会泽县| 宣恩县| 耿马| 卢龙县| 浑源县| 陇南市| 襄汾县| 新蔡县| 黎平县| 龙泉市| 仁化县| 曲松县| 葵青区| 津南区| 马边| 综艺| 安宁市| 平舆县| 上思县| 永吉县| 萨迦县| 贵溪市| 古交市| 五峰| 额敏县| 客服| 霍邱县| 沈阳市| 安龙县| 铁力市| 八宿县| 鄂伦春自治旗| 遵化市|