Kelan Westwood
Kelan Westwood

Reputation: 75

I am trying to update the score that has been blitted but it doesnt change

class Score:
    def __init__(self):
        self.Colour = (230, 230, 230)
        self.x = 25
        self.y = 0
        self.Shape = pygame.Rect((self.x, self.y), (200, 100))
        self.Team1Score = 0
        self.Team2Score = 0
        self.Title = f"{self.Team1Score}  -  {self.Team2Score}"
        self.text_type = pygame.font.SysFont('arialunicode', 40).render(self.Title, True, (0, 0, 0))
        self.text_rect = self.text_type.get_rect(center=self.Shape.center)
        self.Score = False

    def DrawScore(self, window):
        pygame.draw.rect(window, self.Colour, self.Shape)
        window.blit(self.text_type, self.text_rect)

    def AddScore(self, score, p1, window):
        self.Score = score
        if p1.Shooting:
            if self.Score:
                if p1.Colour == (255, 0, 0):
                    self.Team1Score += 1
                    self.text_type = pygame.font.SysFont('arialunicode', 40).render(self.Title, True, (0, 0, 0))
                    window.blit(self.text_type, self.text_rect)
                else:
                    self.Team2Score += 1
                    self.text_type = pygame.font.SysFont('arialunicode', 40).render(self.Title, True, (0, 0, 0))

I am trying to update the score in my game, however the variable for some reason does not change. How do you update the variable so that the text will be updated on the screen. Do I need to make an update function for the text?

Upvotes: 0

Views: 28

Answers (1)

furas
furas

Reputation: 142651

After changing Team1Score or Team2Score you have to create again

self.Title = f"{self.Team1Score}  -  {self.Team2Score}"

to get new string


if p1.Colour == (255, 0, 0):
    self.Team1Score += 1
else:
    self.Team2Score += 1

self.Title = f"{self.Team1Score}  -  {self.Team2Score}"

self.text_type = pygame.font.SysFont('arialunicode', 40).render(self.Title, True, (0, 0, 0))

#window.blit(self.text_type, self.text_rect)

Upvotes: 1

Related Questions