This is the fifth day of my August challenge

❤️ Tanabata festival, using Python to make confession artifact, programmers should also have love! [Attached source code, suggested collection] ❤️

preface

Tanabata festival and double 叒 yi! Tanabata came, the guide also must send something to show you, in this festival permeated with sweet love, programmers should also have love! Today, here is a Python imitation of Douyin confession small software to share without further ado, let us seem “happy” to begin ~

Results show

Ordinary people’s confession

Programmer confession

The development tools

Python version: 3.6.4

Related modules:

Requests module;

The argparse module;

Pyquery module;

Jieba module;

Pyecharts module;

Wordcloud module;

And some modules that come with Python.

Introduction of the principle

To be specific, first we define a button class whose function is to generate a button on the interface according to the initialization parameter, and whether the button can be clicked is also determined by the initialization parameter passed in. Specifically, the code is implemented as follows:

"' Function: button class Initial Args: - x, y: button on the upper left corner coordinates, width, height: button wide high - text: button to display the text - fontpath: font path - fontsize: Font size --fontcolor: fontcolor --bgcolors: button background color --is_want_to_be_selected: whether the button wants to be selected by players --screensize: software 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
  Automatically bind buttons to the screen according to various conditions.
  def draw(self, screen, mouse_pos) :
    The mouse is within the range of the button
    if self.rect.collidepoint(mouse_pos):
      # -- Don't want to be chosen
      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)
    # The mouse is not in the range of the button
    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))
Copy the code

In fact, it is to see if the current position of the mouse is in the range of the button. If it is in and the setting does not allow the user to click the button, it will automatically move the position of the button, so that the mouse position is not in the range of the button after the move. Then write a main loop to make the screen size, color scheme and layout a little more precise:

Principal function
def main() :
  # initialization
  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('From a little brother who likes you.')
  # Background music
  pygame.mixer.music.load(cfg.BGM_PATH)
  pygame.mixer.music.play(-1.30.0)
  # biu love that background picture
  bg_image = pygame.image.load(cfg.BG_IMAGE_PATH)
  bg_image = pygame.transform.smoothscale(bg_image, (150.150))
  # instantiate two buttons
  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='Forget it.', 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)
  # Did you click the yes button
  is_agree = False
  # main loop
  clock = pygame.time.Clock()
  while True:
    # -- Background image
    screen.fill(cfg.WHITE)
    screen.blit(bg_image, (cfg.SCREENSIZE[0]-bg_image.get_height(), 0))
    # -- Mouse event capture
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
        # ---- Do not exit the program without clicking the "Yes" button
        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(' '.'❤❤❤ ❤ mada ❤❤❤')
          root.destroy()
          is_agree = True
    # -- Display text
    showText(screen=screen, text='Little sister, I've been watching you for a long time.', position=(40.50), 
         fontpath=cfg.FONT_PATH, fontsize=25, fontcolor=cfg.BLACK, is_bold=False)
    showText(screen=screen, text='Will you be my girlfriend? ', position=(40.100), 
         fontpath=cfg.FONT_PATH, fontsize=25, fontcolor=cfg.BLACK, is_bold=True)
    # -- Display button
    button_yes.draw(screen, pygame.mouse.get_pos())
    button_no.draw(screen, pygame.mouse.get_pos())
    # - refresh
    pygame.display.update()
    clock.tick(60)
Copy the code

Ps: Remember to set a flag, do not let the other side can close the small program before clicking the “yes” button

That concludes the article. Thank you for watching. In the next article, share the Python network security series

To thank you readers, I’d like to share some of my recent programming favorites to give back to each and every one of you in the hope that they can help you.

Dry goods mainly include:

① Over 2000 Python ebooks (both mainstream and classic books should be available)

②Python Standard Library (Most Complete Chinese version)

③ project source code (forty or fifty interesting and classic practice projects and source code)

④Python basic introduction, crawler, Web development, big data analysis video (suitable for small white learning)

⑤ A Roadmap for Learning Python

All done~ see personal homepage or private letter for complete source code.