如何使用PyGame更改标签文本而不重叠?

时间:2018-10-14 22:13:30

标签: python python-3.x pygame

当我使用font.render和screen.blit显示新分数时,新分数与旧分数重叠,即使搜索了类似问题,我也找不到解决方案。可能有一个不错的解决方案?感谢您的任何建议。

enter image description here

import pygame
import sys

WIDTH = 400
HEIGHT = 400
GREY_COLOUR = [150, 150, 150]
WHITE_COLOUR = [255,255,255]
center = (int(WIDTH / 2), int(HEIGHT / 2))
CIRCLE_RADIUS = 100


class Main:
    count = 0

    def __init__(self, screen):
        self.screen = screen
        self.running = True
        self.main_loop()

    def render(self):
        pygame.display.flip()


    def handle_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_SPACE:
                    self.count+=1
                    self.showScore()

    def showScore(self):
        font = pygame.font.SysFont('Comic Sans MS', 30)
        scoretext = font.render("Score:" + str(self.count), 1, (0, 0, 0))
        screen.blit(scoretext, (center[0], center[1]-200))


    def main_loop(self):
        while self.running:
            self.handle_events()
            self.render()


pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
screen.fill(WHITE_COLOUR)
pygame.draw.circle(screen, GREY_COLOUR, center, CIRCLE_RADIUS, 2)
pygame.display.update()
game = Main(screen)

1 个答案:

答案 0 :(得分:2)

我认为您可以为render函数提供背景,所以它不是透明的。

例如,对于白色:

scoretext = font.render("Score:" + str(self.count), True, (0, 0, 0), (255, 255, 255))

如果我没记错,第二个参数antialias是一个布尔值,因此应该为True