简体   繁体   中英

Loading images in pygame causes “pygame.error”

Whenever i try and run my pygame program in which I'm trying to load some images I always get plain "pygame.error" and nothing else.

I have imported pygame aswell as pygame.locals. At first i thought that it was because it couldnt find my files but whenever I change the name of a file to something thats incorrect it says "couldnt open image" so i guess that it really does find the file but fails when trying to load it. the startup() function is the first thing that is run from main. and i've also tried to do a simple grass=pygame.image.load('grass.png') with the grass.png file in the same directory as the python file

thankful for all the help i can get

..... line 41, in startup surroundingsdict = {'gräs': pygame.image.load(os.path.join('images', 'grass.png')).convert_alpha(), pygame.error

def startup():
    pygame.init()
    p1=Player.Player(int(random.random()*mapstorlek),int(random.random()*mapstorlek))
    vh=ViewHandler.ViewHandler()
    surroundingssdict = {'grass': pygame.image.load(os.path.join('images','grass.png')).convert(),
          'mount': pygame.image.load(os.path.join('images', 'mount.png').convert)()}
    playerdict={'p1': pygame.image.load(os.path.join('images','player.png')).convert()}

pygame.error is usually accompanied by a message with more details about what went wrong, like the 'couldn't open image' you get when you try a non-existant image. Are you sure you get no more info? Can't be sure what's wrong, but I'll throw out two suggestions:

Do you call pygame.display.set_mode to set a display surface before loading the images? You might be doing that in your ViewHandler class, but if not the convert() will fail since it changes the pixel format of the surface to match the display, which it of course can't do if there is no display active.

Another possibility is that you don't have full image support in pygame and can't load .png. Have you tried loading other formats? Try calling pygame.image.get_extended() to see if your pygame is built with extended image support. Even if it returns True there is no guarantee that you can load .png, but at least it's more likely to be something else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM