PyGame: pygame.image.load() does not show anything
-
26-04-2021 - |
Question
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.
OTHER TIPS
Since you said in a comment you had no idea about programming, you probably don't know that indenting is important in python. Your while loop is running continuously, waiting for an exit signal. But the display is only performed after that...
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()
now the display code is inside the while loop
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow