title: Pygame Hangman Easier Way To Generate New Words? Instead Of Making Another Main Loop
body: <p>so I am working on this hangman game and I am trying to make new generated fruit names instead of copying the first main loop and pasting it again I feel like that would be to much code and I will have a lot of issues is there a way  <a href="https://gyazo.com/a22a5453a44f23d1f935b460208dad67" rel="nofollow noreferrer">VIDEO</a> &lt; this is my fruit11() right now it only works for the word Apple is there a way I could make it so when I finish the word apple it resets and generates a new word instead of copying the whole main loop and pasting it again that would be so much code and will be alot of things to change thank YOU!</p>
  <p>if I copy this and make a new loop for my second word like my first word right now is &quot;apple&quot; if I copy this main loop and make a new one then I would have thousands of lines of code I just want to know if there is an easy way to make new words for my hangman</p>
  <pre><code>
  def lose_screen():
      
  
      timer = 0
      # the background image
      bg = pygame.image.load(&quot;lose.png&quot;)
  
  
      FPS = 50
      clock = pygame.time.Clock()
      
      def redraw():
          window.blit(bg,(0,0))
  
  
                  
  
                      
  
  
   
      intro = True
      while intro:
          clock.tick(FPS)
          for event in pygame.event.get():
              if event.type == pygame.QUIT:
                  intro = False
                  pygame.quit()
          timer += 1
          if timer &gt;= 120:
              fruit11()
  
  
  
          redraw()
          clock.tick(FPS)
  
  
          pygame.display.update()
  
  
  
  
  
  
  def player1_win():
      
  
      timer = 0
      # the background image
      bg = pygame.image.load(&quot;player1win.png&quot;)
  
  
      FPS = 50
      clock = pygame.time.Clock()
      
      def redraw():
          window.blit(bg,(0,0))
  
  
                  
  
                      
  
  
   
      intro = True
      while intro:
          clock.tick(FPS)
          for event in pygame.event.get():
              if event.type == pygame.QUIT:
                  intro = False
                  pygame.quit()
          timer += 1
          if timer &gt;= 120:
              fruit11()
  
  
  
          redraw()
          clock.tick(FPS)
  
  
          pygame.display.update()
  
  
  
  
  def player2_win():
      
  
      timer = 0
      # the background image
      bg = pygame.image.load(&quot;player2win.png&quot;)
  
  
      FPS = 50
      clock = pygame.time.Clock()
      
      def redraw():
          window.blit(bg,(0,0))
  
  
                  
  
                      
  
  
   
      intro = True
      while intro:
          clock.tick(FPS)
          for event in pygame.event.get():
              if event.type == pygame.QUIT:
                  intro = False
                  pygame.quit()
          timer += 1
          if timer &gt;= 120:
              fruit11()
  
  
  
          redraw()
          clock.tick(FPS)
  
  
          pygame.display.update()
  
  
  
  
  def fruit11():
  
      class partic:
          def __init__(self,x,y,height,width,color):
              self.x = x
              self.y = y
              self.height = height
              self.width = width
              self.color = color
              self.rect = pygame.Rect(x,y,height,width)
          def draw(self,DrawX,DrawY):
              self.rect.topleft = (DrawX,DrawY)
              pygame.draw.rect(window,self.color,self.rect,2)
  
      white = (255,255,255)            
      partic1 = partic(500,500,50,50,white)
  
  
      colors = (0, 0, 0)
      greenbutton0 = button((colors),60,600,50,50, 'A')
      greenbutton1 = button((colors),140,600,50,50, 'C')
      greenbutton2 = button((colors),220,600,50,50, 'F')
      greenbutton3 = button((colors),300,600,50,50, 'P')
      greenbutton4 = button((colors),380,600,50,50, 'D')
      greenbutton5 = button((colors),60,660,50,50, 'U')
      greenbutton6 = button((colors),140,660,50,50, 'L')
      greenbutton7 = button((colors),220,660,50,50, 'P')
      greenbutton8 = button((colors),300,660,50,50, 'N')
      greenbutton9 = button((colors),380,660,50,50, 'H')
  
      greenbutton10 = button((colors),60,720,50,50, 'X')
      greenbutton11 = button((colors),140,720,50,50, 'R')
      greenbutton12 = button((colors),220,720,50,50, 'D')
      greenbutton13 = button((colors),300,720,50,50, 'E')
      greenbutton14 = button((colors),380,720,50,50, 'O')
  
      menu_button = button((colors),390,210,150,50, &quot;&quot;)
      buttons = [greenbutton0,greenbutton1,greenbutton2,greenbutton3,greenbutton4,greenbutton5,greenbutton6,greenbutton7,greenbutton8,greenbutton9,greenbutton10,greenbutton11,greenbutton12,greenbutton13,greenbutton14]
  
  
  
  
              
      font = pygame.font.Font(&quot;times.ttf&quot;, 40)
      scor = 0
      scoretext = font.render(&quot;&quot; + str(scor), True, (255,255,255))
      scorerect = scoretext.get_rect()
      scorerect.center = ((450,180))
  
  
      font = pygame.font.Font(&quot;times.ttf&quot;, 40)
      sco = 0
      scoretex = font.render(&quot;&quot; + str(sco), True, (255,255,255))
      scorerec = scoretex.get_rect()
      scorerec.center = ((50,180))
  
      nor = pygame.image.load(&quot;lose.png&quot;)
  
  
      bg = pygame.image.load(&quot;player1.png&quot;)
      bg2 = pygame.image.load(&quot;player2.png&quot;)
  
      stickhead = pygame.image.load(&quot;nos.png&quot;)
      stickbody = pygame.image.load(&quot;bod.png&quot;)
      stickleg = pygame.image.load(&quot;leg2.png&quot;)
      stickleg2 = pygame.image.load(&quot;leg1.png&quot;)
      arm = pygame.image.load(&quot;bod2.png&quot;)
      arm2 = pygame.image.load(&quot;bod3.png&quot;)
      score = 0
      score2 = 0
      particles = []
      def redraw():
          
  
          window.blit(bg,(0,0))
  
  
          if score == 1 or score == 3 or score == 5 or score == 7 or score == 9 or score == 11 or score == 14 or score == 15:
              window.blit(bg2,(0,0))
  
          if score == 2 or score == 4 or score == 6 or score == 8 or score == 10 or score == 12 or score == 14 or score == 16:
              window.blit(bg,(0,0))
  
  
          
          
          for button in buttons:
              button.draw(window)
  
  
          pos = pygame.mouse.get_pos()
          for button in buttons:
              if button.isOver(pos):
                  partic1.draw(button.x,button.y)
  
  
  
          if score2 &gt;= 1:
              for button in buttons:
                  if button.x == 1040:
                      window.blit(stickhead,(170,240))
  
          if score2 &gt;= 2:
              for button in buttons:
                  if button.x == 1040:
                      window.blit(stickbody,(180,300))
  
          if score2 &gt;= 3:
              for button in buttons:
                  if button.x == 1040:
                      window.blit(stickleg,(180,360))
  
          if score2 &gt;= 4:
              for button in buttons:
                  if button.x == 1040:
                      window.blit(stickleg2,(150,360))
  
          if score2 &gt;= 5:
              for button in buttons:
                  if button.x == 1040:
                      window.blit(arm,(170,280))
  
          if score2 &gt;= 6:
              for button in buttons:
                  if button.x == 1040:
                      window.blit(arm2,(150,280))
  
          window.blit(scoretext,scorerect)
          window.blit(scoretex,scorerec)
      # our main loop
  
  
              
                      
      run = True
      while run:
          for event in pygame.event.get():
              if event.type == pygame.QUIT:
                  run = False
  
          # what happens if our mouse clicks the button
              if event.type == pygame.MOUSEBUTTONDOWN:
                  pos = pygame.mouse.get_pos()
  
  
      
                      
                  # back to game
                  if menu_button.isOver(pos):
                      game_intro()
  
              
                  
                  if greenbutton0.isOver(pos) and greenbutton0.y == 600:
                      greenbutton0.y = 490
                      greenbutton0.x = 34
                      score += 1
  
                      
                      if score == 1 or score == 3 or score == 5 or score == 7 or score == 9 or score == 11 or score == 14 or score == 15:
                          scor += 1
                          scoretext = font.render(&quot;&quot; + str(scor), True, (255,255,255))
                          scorerect.center = ((450,180))
  
  
                      if score == 2 or score == 4 or score == 6 or score == 8 or score == 10 or score == 12 or score == 14 or score == 16:
                          sco += 1
                          scoretex = font.render(&quot;&quot; + str(sco), True, (255,255,255))
                          scorerec.center = ((50,180))
      
                      
                  if greenbutton7.isOver(pos) and greenbutton7.y == 660:
                      greenbutton7.y = 490
                      greenbutton7.x = 120
                      score += 1
  
                      if score == 1 or score == 3 or score == 5 or score == 7 or score == 9 or score == 11 or score == 14 or score == 15:
                          scor += 1
                          scoretext = font.render(&quot;&quot; + str(scor), True, (255,255,255))
                          scorerect.center = ((450,180))
  
  
                      if score == 2 or score == 4 or score == 6 or score == 8 or score == 10 or score == 12 or score == 14 or score == 16:
                          sco += 1
                          scoretex = font.render(&quot;&quot; + str(sco), True, (255,255,255))
                          scorerec.center = ((50,180))
  
                     
                      
                  if greenbutton3.isOver(pos) and greenbutton3.y == 600:
                      greenbutton3.y = 490
                      greenbutton3.x = 200
                      score += 1
  
                      if score == 1 or score == 3 or score == 5 or score == 7 or score == 9 or score == 11 or score == 14 or score == 15:
                          scor += 1
                          scoretext = font.render(&quot;&quot; + str(scor), True, (255,255,255))
                          scorerect.center = ((450,180))
  
  
                      if score == 2 or score == 4 or score == 6 or score == 8 or score == 10 or score == 12 or score == 14 or score == 16:
                          sco += 1
                          scoretex = font.render(&quot;&quot; + str(sco), True, (255,255,255))
                          scorerec.center = ((50,180))
  
       
                  if greenbutton6.isOver(pos) and greenbutton6.y == 660:
                      greenbutton6.y = 490
                      greenbutton6.x = 300
                      score += 1
  
                      if score == 1 or score == 3 or score == 5 or score == 7 or score == 9 or score == 11 or score == 14 or score == 15:
                          scor += 1
                          scoretext = font.render(&quot;&quot; + str(scor), True, (255,255,255))
                          scorerect.center = ((450,180))
  
  
                      if score == 2 or score == 4 or score == 6 or score == 8 or score == 10 or score == 12 or score == 14 or score == 16:
                          sco += 1
                          scoretex = font.render(&quot;&quot; + str(sco), True, (255,255,255))
                          scorerec.center = ((50,180))
  
                      
  
                  if greenbutton13.isOver(pos) and greenbutton13.y == 720:
                      greenbutton13.y = 490
                      greenbutton13.x = 380
                      score += 1
  
                      if score == 1 or score == 3 or score == 5 or score == 7 or score == 9 or score == 11 or score == 14 or score == 15:
                          scor += 1
                          scoretext = font.render(&quot;&quot; + str(scor), True, (255,255,255))
                          scorerect.center = ((450,180))
  
  
                      if score == 2 or score == 4 or score == 6 or score == 8 or score == 10 or score == 12 or score == 14 or score == 16:
                          sco += 1
                          scoretex = font.render(&quot;&quot; + str(sco), True, (255,255,255))
                          scorerec.center = ((50,180))                    
  
   
  
                       
  
                  if greenbutton1.isOver(pos):
                      score2 += 1
                      score +=1 
                      greenbutton1.x = 1040
                      
                  if greenbutton2.isOver(pos):
                      score2 += 1
                      score +=1 
                      greenbutton2.x = 1040
                      
                  if greenbutton4.isOver(pos):
                      score2 += 1
                      score +=1 
                      greenbutton4.x = 1040
                      
                  if greenbutton5.isOver(pos):
                      score2 += 1
                      score +=1 
                      greenbutton5.x = 1040
                      
                      
                  if greenbutton8.isOver(pos):
                      score2 += 1
                      score +=1 
                      greenbutton8.x = 1040
                      
                  if greenbutton9.isOver(pos):
                      score2 += 1
                      score +=1 
                      greenbutton9.x = 1040
                      
                  if greenbutton10.isOver(pos):
                      score2 += 1
                      score +=1 
                      greenbutton10.x = 1040
                      
                  if greenbutton11.isOver(pos):
                      score2 += 1
                      score +=1 
                      greenbutton11.x = 1040
                      
                  if greenbutton12.isOver(pos):
                      score2 += 1
                      score +=1 
                      greenbutton12.x = 1040
                      
                      
                  if greenbutton14.isOver(pos):
                      score2 += 1
                      score += 1 
                      greenbutton14.x = 1040
  
  
          if score2 == 6:
              lose_screen()
  
  
          if greenbutton0.y == 490 and greenbutton7.y == 490 and greenbutton3.y == 490 and greenbutton6.y == 490 and greenbutton13.y == 490:
              if sco &gt; scor:
                  player1_win()
                  
          if greenbutton0.y == 490 and greenbutton7.y == 490 and greenbutton3.y == 490 and greenbutton6.y == 490 and greenbutton13.y == 490:
              if sco &lt; scor:
                  player2_win()
                          
  
  
          redraw()
          pygame.display.update()
          
      pygame.quit()
  
  
  </code></pre>