Search code examples
pythonpygamegeometry-surfaceblit

PyGame: pygame.image.load() does not show anything


I am doing an assignment with pyGame, a very simple one, we have to load an image in a window, but it does not load! And the IDLE does not show any errors..I tried relative paths to my image (room.png) also absolute paths (C:...\room.png) but nothing. Here is my code.

import pygame, sys      #import pygame and system library
from pygame import *    #import all pygame sublibs

pygame.init()
screen = display.set_mode((385,384))    #set screen size
display.set_caption("Blit example")
                        #this one gets the current working directory
background_file_name = "room.png"           #import bg
background_surface = pygame.image.load(background_file_name)    #use bg

while True:
        for e in pygame.event.get():
                if e.type==QUIT:        #break the loop and quit
                        pygame.quit()
                        sys.exit()
                        break


screen.fill((255,0,255))            #fill the screen with magenta
screen.blit(background_surface, (0,0))
display.update()

Solution

  • Your indentation is messed up.

    You only start drawing after the application has ended. Try putting the drawing code into your main loop.