Search code examples
pythonpygamepygame-surface

pygame surface isn't visible


I'm currently trying to follow the Introduction to Pygame tutorial and I'm stuck on one step where the speaker makes the surface. His surface is bright red while my surface isn't visible at all. Here's the code:

import pygame
from sys import exit

pygame.init()
screen = pygame.display.set_mode((800, 400))
pygame.display.set_caption('Runner')
clock = pygame.time.Clock()

test_surface = pygame.Surface((100, 200))
test_surface.fill('Red')

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

            screen.blit(test_surface, (0, 0))

            pygame.display.update()
            clock.tick(60)

I've already tried starting from the very beginning, my code and the steps I make are identical to his. I've also tried deleting python and pygame. I've installed Python version that the speaker has (3.9), but nothing helps.


Solution

  • It is a matter of indentation. You have draw the scene and update the display in the application loop:

    import pygame
    from sys import exit
    
    pygame.init()
    screen = pygame.display.set_mode((800, 400))
    pygame.display.set_caption('Runner')
    clock = pygame.time.Clock()
    
    test_surface = pygame.Surface((100, 200))
    test_surface.fill('Red')
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()
    
                # INDENTATION
        #<------|
    
        screen.blit(test_surface, (0, 0))
    
        pygame.display.update()
        clock.tick(60)