2017-03-15 80 views
-1

產品圖更多,更清晰的問題: http://imgur.com/a/sbHaxPygame退格鍵輸入的文字未被清除。重疊而不是/

如圖所示,我輸入了一些文字。決定退格,而不是我輸入的新文本重疊前一個。這是爲什麼發生?

新來的Pygame並堅持了這個問題時間過長已經:(

請幫助!我應該做的

if not cheat: 
     WIN = pygame.image.load("Assets/winBox.png") 
     self.SCREEN.blit(WIN, (175, 230)) 
     winRect = pygame.Rect(175, 230, 250, 164) 
     pygame.display.update(winRect) 
     done = False 

     while not done: 
      self.CLOCK.tick(self.FPS) 
      mouse = pygame.mouse.get_pos() 
      for event in pygame.event.get(): 
       if event.type == pygame.QUIT: 
        self.STATE = 0 
        done = True 

       if 219 <= mouse[0] <= 269 and 366 <= mouse[1] <= 378 and pygame.mouse.get_pressed()[0] == 1: 
        # RESTART 
        self.STATE = 6 
        done = True 

       elif 311 <= mouse[0] <= 375 and 366 <= mouse[1] <= 378 and pygame.mouse.get_pressed()[0] == 1: 
        # MAIN MENU 
        self.STATE = 2 
        done = True 


    else: 
     WIN = pygame.image.load("Assets/winBoxWithScore.png") 
     self.SCREEN.blit(WIN, (175, 230)) 
     winRect = pygame.Rect(175, 230, 250, 164) 


     pygame.display.update(winRect) 
     done = False 

     while not done: 
      self.CLOCK.tick(self.FPS) 
      mouse = pygame.mouse.get_pos() 
      for event in pygame.event.get(): 
       if event.type == pygame.QUIT: 
        self.STATE = 0 
        done = True 

       if 219 <= mouse[0] <= 269 and 366 <= mouse[1] <= 378 and pygame.mouse.get_pressed()[0] == 1: 
        # RESTART 
        self.STATE = 6 
        done = True 

       if 311 <= mouse[0] <= 375 and 366 <= mouse[1] <= 378 and pygame.mouse.get_pressed()[0] == 1: 
        # MAIN MENU 
        self.STATE = 2 
        done = True 

       elif 290 <= mouse[0] <= 345 and 275 <= mouse[1] <= 325 and pygame.mouse.get_pressed()[0] == 1: 

        WIN = pygame.image.load("Assets/winBoxBlank.png") 
        self.SCREEN.blit(WIN, (175, 230)) 
        winRect = pygame.Rect(175, 230, 250, 164) 

        pygame.display.update(winRect) 
        name = "" 
        done = False 
        while not done: 
         self.CLOCK.tick(self.FPS) 
         mouse = pygame.mouse.get_pos() 

         self.SCREEN.blit(
          pygame.font.SysFont('comicsansms', 22).render("Enter your name :", True, Color.black), 
          (210, 250)) 
         # Textbox 
         pygame.draw.rect(self.SCREEN, Color.gray, (210, 310, 180, 35), 2) 

         for event in pygame.event.get(): 
          if event.type == pygame.QUIT: 
           self.STATE = 0 
           done = True 

          # Keyboard events 
          if event.type == pygame.KEYDOWN: 
           if event.key == pygame.K_BACKSPACE: 
            name = name[:-1] # remove last char 


           elif event.key == pygame.K_RETURN or event.key == pygame.K_KP_ENTER: 
            done = True # break out of the while loop to return name 
           elif event.key == pygame.K_ESCAPE: 
            self.menu() 
            done = True 

           else: 
            try: 
             name += event.unicode # Append name 
            except: 
             pass 
         # Limit name to 10 characters 
         name = name[:10] 
         # Display characters typed 
         if len(name) != 0: 
          self.SCREEN.blit(pygame.font.SysFont('comicsansms', 22).render(name, True, Color.black),(230, 310)) 

         pygame.display.update() 

        # Store score to the current textfile 
        p = Player(name, self.numdisks, str(len(self.moveList))) 
        with open("score.txt", "a") as sfile: 
         sfile.write("\n" + p.getName() + "\t" + str(p.getLevel()) + "\t" + str(p.getMove())) 
        # Navigate to the next page 
        self.STATE = 11 
+0

內如果舊文本沒有清除,那麼我想它不是」不需要更新。你是否在刪除字符時更新了文本框? – The4thIceman

回答

1

什麼在你的代碼被重新繪製文本標籤,文本框,並在每次更新名稱:

self.SCREEN.blit(pygame.font.SysFont('comicsansms', 22).render("Enter your name :", True, Color.black), (210, 250)) 
# Textbox 
pygame.draw.rect(self.SCREEN, Color.gray, (210, 310, 180, 35), 2) 

但你不清除裏面的文本框的問題是要繪製未填充矩形繪製一個矩形的最後一個參數爲t。他的寬度。你有它設置爲2.所以它正在繪製一個矩形,但沒有填充。

https://www.pygame.org/docs/ref/draw.html#pygame.draw.rect

如果添加填充矩形,以及(在文本框內),它會在每次清除名,僅與當前名稱填充它。

pygame.draw.rect(self.SCREEN, color_of_your_choice, (212, 312, 176, 31)) 

你必須調整X,Y,寬度和高度,以佔略小的矩形,以適應外箱