Python/Pygame, Label not showing up

782 views Asked by At

So basically I am just trying to do some stuff using pygame in python. This is part of the code, the rest of the code does not affect this so I

from pygame import *
from pygame.locals import *
import pygame
from sys import exit
from random import *
import time

pygame.init()
font.init()

screen = display.set_mode((1920, 1080), FULLSCREEN)
screen.fill((0, 0, 0))

countTime = 1
while countTime < 4:
    default_font = pygame.font.get_default_font()
    font_renderer = pygame.font.Font(default_font, 45)
    label = font_renderer.render(str(countTime).\
            encode('utf-8'), 1, (255, 255, 255))
    screen.blit(label, (1920/2, 1080/2))
    countTime += 1
    time.sleep(1)

So as you can see, what it is meant to do is create a full screen window that just has some letters going "3", "2", "1" before breaking out of the while and executing the rest of my code.

Everything looks fine, but the issue is that nothing shows up. I just get a black fullscreen window like I am meant to, but with no white text showing up. What am I doing wrong?

1

There are 1 answers

2
furas On BEST ANSWER

pygame.display creates screen which is surface in memory (buffer) and all blit draw on this surface. You have to use display.flip() or display.update() to send this surface/buffer on the screen/monitor.


EDIT: example code

import pygame

# --- constants --- (UPPER_CASE names)

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

# --- classes --- (CamelCase names)

# empty

# --- functions --- (lower_came names)

# empty

# --- main ---

# - init -

pygame.init()

screen = pygame.display.set_mode((800, 600))
screen_rect = screen.get_rect()

# - objects -

default_font = pygame.font.get_default_font()
font_renderer = pygame.font.Font(default_font, 45)

# - mainloop -

count_time = 1
running = True

while running:

    # --- events ---

    for event in pygame.event.get():
        # close window with button `X`
        if event.type == pygame.QUIT: 
            running = False

        # close window with key `ESC`
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False

    # --- updates (without draws) ---

    label = font_renderer.render(str(count_time), True, WHITE)
    label_rect = label.get_rect()
    # center on screen
    label_rect.center = screen_rect.center

    count_time += 1
    if count_time >= 4:
        running = False

    # --- draws (without updates) ---

    screen.fill(BLACK)
    screen.blit(label, label_rect)
    pygame.display.flip()

    # --- speed ---

    # 1000ms = 1s
    pygame.time.delay(1000) 

# - end -

pygame.quit()