pygame.image.load 不工作 [英] pygame.image.load not working

查看:58
本文介绍了pygame.image.load 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为游戏创建世界地图,但是当我尝试将世界地图加载到屏幕时,命令行告诉我我无法这样做.代码如下:

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

推荐答案

错误信息很明确:pygame can't find your image in the place you say it.

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

您必须确保图像 WorldMap.jpg 存在于您的当前工作目录 (CWD) 中.

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

如果双击 .py 文件,CWD 是包含 .py 的目录.所以WorldMap.jpg 必须在那里.如果您从不同目录的命令行执行它,请为此添加代码或将图像放在另一个目录中.

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.

这篇关于pygame.image.load 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆