Pygame从文件导入图像错误:“无法打开

时间:2011-07-24 00:25:13

标签: python

嗨,我们在将图像导入我的pygames脚本时遇到问题。

我的代码:

from gasp import*

begin_graphics(800,600,"My Game",color.GREEN)

Image("C:\Users\Yuvinng\Downloads\982960f3d1joong.jpg",(400,300), None, None)

end_graphics()

错误:

Traceback (most recent call last):
  File "C:\Python32\testing idle codes\GASP", line 5, in <module>
    Image("C:\Users\Yuvinng\Downloads\982960f3d1joong.jpg",(400,300), None, None)
  File "C:\Python27\lib\site-packages\gasp\api.py", line 267, in __init__
    backend.create_image(self)
  File "C:\Python27\lib\site-packages\gasp\backend.py", line 302, in create_image
    obj.sprite.image = pygame.image.load(obj.path_name).convert()
error: Couldn't open C:\Users\Yuvinng\Downloads\982960f3d1joong.jpg

任何人都可以解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

文件是否首先存在于指定位置?错误消息说它无法在那里找到它。其中一个好的做法是使用raw string为路径添加r作为前缀,以防止无意中的转义。

Image(r"C:\Users\Yuvinng\Downloads\982960f3d1joong.jpg",(400,300), None, None)