pygame.image.load not working

时光毁灭记忆、已成空白 提交于 2019-12-23 20:08:15

问题


I'm trying to create a world map for a game, but when I try to load the world map to the screen, the command line tells me I'm unable to do so. Here is the code:

import sys 
import pygame
from pygame.locals import * 
pygame.init ()

Surface = pygame.display.set_mode ((1000, 775), 0, 32)
pygame.display.set_caption('World Map')

WorldMap = pygame.image.load('WorldMap.jpg')
white = (255, 255, 255)
while True:
    Surface.fill (white)
    Surface.blit (WorldMap, (900, 675))
    for event in pygame.event.get():
       if event.type == QUIT:
            pygame.quit ()
            sys.exit (0)
    pygame.display.update()

The error looks something like this ----

Traceback (most recent call last)
  File "C:\Users\The Folder\WorldMap.py", line 9, in <module> 
    WorldMap = pygame.image.load ('WorldMap.jpg')
  pygame.error: Couldn't open WorldMap.jpg

回答1:


The error message is pretty clear: pygame can't find your image in the place you said it would be.

You have to make sure that the image WorldMap.jpg exists in your current working directory (CWD).

If you double-click your .py file, the CWD is the directory containing the .py. So WorldMap.jpg must be there. If you execute it from the command line from a different directory, add code for that or put the image in the other directory.




回答2:


I had same problem. I started my .py not from the working directory of the pogram. After I used cd to get to the right directory all was fine. So first make sure to get your current working directory the same as the working directory and start your program.



来源:https://stackoverflow.com/questions/11389768/pygame-image-load-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!