Igor czerbniak
Igor czerbniak

Reputation: 9

Displayed score not updating even tho score variable changes

import pygame
from sys import exit

pygame.init() ### pozwala wykonywać funkcje pygame-a po tym jak jest wywołana

screen = pygame.display.set_mode((800, 400)) ### ustalamy rozdzielczość okna gry

pygame.display.set_caption("Balls") ###zmienia tytół okienka gry

clock = pygame.time.Clock() ### tworzy "zegar", który pomaga na kontrolować fps-y itd

bck = pygame.Surface((800, 400))
bck.fill("White")

#pygame.font.Font(rodzaj czcionki, rozmiar czcionki)
test_font = pygame.font.Font(None, 60)
#text_surface = test_font.render("text", wygładzanie krawędzi textu, "color")
    

score = 0
score_surf = test_font.render(str(score), True, "Black")
score_rect = score_surf.get_rect(center = (400, 150))
sh = 0

while True:
    ### tutaj będzie wszystko wyświetlane, oraz klatki będą aktualizowane
    for event in pygame.event.get(): ### sprawdzanie inputu użytkownika

        if event.type == pygame.QUIT: ### wykrywanie czy użytkownik zamyka okno gry
            pygame.quit() ###przeciwieństwo inicjalizacji pygame, kończy działanie modułu
            exit() ### kończy wykonywanie załego programu


    screen.blit(bck, (0, 0))

    
    screen.blit(score_surf, score_rect)
    sh += 1
    if sh == 60:
        sh = 0
        score += 1
        print(score)

    
    clock.tick(60)
    pygame.display.update() ### generuje klatkę

If you run the code diplayed score is 0, but the score variable changes.

Upvotes: 0

Views: 67

Answers (1)

Luka
Luka

Reputation: 83

In your code you're only changing the score variable which as you said is updating. On the other hand you never update the output that you render out to your screen. So what is happening is that score_surf and score_rect never change after the initialization. What you want to do is place

score_surf = test_font.render(str(score), True, "Black")
score_rect = score_surf.get_rect(center = (400, 150))

inside the while loop so that they update every frame. Because you're not displaying the score variable but the score_surf.

I hope this helps

Upvotes: 1

Related Questions