Pygame image will not load. I have tried everything, ripping my hair out rn

Question:

So I used to make TONS of games in Pygame, but suddenly doing the most basic task is a problem and I cannot understand what the issue is. There is no error, and I’ve tried completing this task several different ways. Here is the final effort, someone PLEASE tell me why this code will not display a basic .png image at screen position (0,0).

Also, the other questions I found that are similar to this one were the result of drawing the image before filling the screen, but clearly this is not the issue here, as I am calling .blit() after .fill().

import pygame


class Game:

resolutions = [(640,360),(800,450),(1000,565)]

def set_resolution(self,res):
    self.current_res = Game.resolutions[res]
    
def __init__(self):
    self.run = True
    self.clock = pygame.time.Clock()
    
    self.set_resolution(0)
    self.fps = 60
    self.display = pygame.display.set_mode((self.current_res),pygame.NOFRAME)
    self.img = pygame.image.load('test_img.png').convert_alpha()

def update_display(self):
    self.display.fill('black')
    self.display.blit(self.img,(0,0))


def gameloop(self):
    
    while self.run:
        
        self.clock.tick(self.fps)
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.run = False
                
                
        self.update_display()
    
    pygame.quit()


if __name__ == '__main__':
    g = Game()
    g.gameloop()
Asked By: gagecode

||

Answers:

Looks like you are just missing "pygame.display.update()".

    def gameloop(self):
    
        while self.run:
        
            self.clock.tick(self.fps)
        
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.run = False
                
            self.update_display()
            pygame.display.update()   # Added this
    
        pygame.quit()

When I added that and ran the program, I got an image to appear.

Sample Image

I happened to have the king of hearts available, and I changed the background to gray.

Give that a try.

Answered By: NoDakker
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.