输入文本上的Pygame Backspace键未清除。改为重叠/

时间:2017-03-15 19:14:03

标签: python pygame

Pic以更清晰地解决问题: http://imgur.com/a/sbHax

如图所示,我输入了一些文字。决定退格,而我输入的新文本与前一个文本重叠。为什么会这样?

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

1 个答案:

答案 0 :(得分: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)

但是你没有清除里面的文本框。问题是您正在绘制一个未填充的矩形。绘制矩形的最后一个参数是宽度。你把它设置为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,宽度和高度以适应稍微小一点的矩形以适应外盒