Why isnt the text showing up in pygame?

Viewed 60

I am trying to make a player counter to where if the player presses the up arrow on the keyboard i want players to go up by one and display the counter going up and the same with down. But the counter isn't showing

import pygame
pygame.init()
pygame.display.set_mode()
pygame.font.init()
Red = (247, 12, 12)
White = (255,255,255)
Blue = (0, 157, 255)
Black=(0,0,0)
X = 1400
Y = 1000
players = 0
display_surface = pygame.display.set_mode((X, Y))
font = pygame.font.Font('freesansbold.ttf', 20)
text = font.render("Player one is:", True, Black, White)
textRect = text.get_rect()
textRect.center = (X // 4.4, Y // 3.6)
str(players)
while True:
    display_surface.fill(White)
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit(); #sys.exit() if sys is imported
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_0:
                print("Hey, you pressed the key, '0'!")
            if event.key == pygame.K_1:
                print("Doing whatever")
            if event.key == pygame.K_UP:
                int(players)
                players = players+1
                str(players)
                text1 = font.render(players, True, Black, White)
                text1Rect = text1.get_rect()
                text1Rect.center = (X // 4.4, Y // 3)
                display_surface.blit(text, textRect)
                int(players)
pygame.display.update()
1 Answers

The str(x) converts x and returns a string:

str(players)

str(players)

However, you don't need it at all. You have to render the text after clearing the display and before updating the display. Initialize text1 before the application loop:

while True:
    # [...]

    # clear display
    display_surface.fill(White)
   
    # render secene
    display_surface.blit(text, textRect)
    display_surface.blit(text1, text1Rect)
    
    # [...]
    
    # update display
    pygame.display.update()

Change and rerender the text1 surface when UP is pressed:

if event.key == pygame.K_UP:
    players += 1
    text = font.render("Player one is: " + str(players), True, Black, White)
    textRect = text.get_rect(center = (X // 4.4, Y // 3))

Complete example:

import pygame
pygame.init()
pygame.font.init()
Red = (247, 12, 12)
White = (255,255,255)
Blue = (0, 157, 255)
Black=(0,0,0)
X = 1400
Y = 1000
players = 0
display_surface = pygame.display.set_mode((X, Y))
font = pygame.font.Font('freesansbold.ttf', 20)

text = font.render("Player one is:", True, Black, White)
textRect = text.get_rect(center = (X // 4.4, Y // 3.6))
text1 = font.render(str(players), True, Black, White)
text1Rect = text.get_rect(center = (X // 4.4, Y // 3))

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_0:
                print("Hey, you pressed the key, '0'!")
            if event.key == pygame.K_1:
                print("Doing whatever")
            if event.key == pygame.K_UP:
                players += 1
                text1 = font.render(str(players), True, Black, White)
                text1Rect = text.get_rect(center = (X // 4.4, Y // 3))

    display_surface.fill(White)
    display_surface.blit(text, textRect)
    display_surface.blit(text1, text1Rect)
    pygame.display.update()

pygame.quit()
exit()
Related