可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm trying to make the base for a map that I'm going to be using for a game, but I can't load the image onto the screen. I've tried sending in an absolute filepath to the image with likesame letter case, I've tried changing the name of the image, I've tried loading different images that worked in other programs I made, and I've tried putting the image in the same directory as the script itself. Nothing has worked yet. I looked at a few threads of people who were having the same problem, such as Why are my pygame images not loading? and I can't find the answer to my problem. The image will not load. Here's the code:
import sys, pygame from pygame.locals import * pygame.init() size = (600, 400) screen = pygame.display.set_mode(size) pygame.display.set_caption("Practice Map") walls = pygame.image.load("C:\Users\dylan\Desktop\Practice Game\brick.jpg") x = 0 y = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == KEYDOWN and event.key == K_ESCAPE: sys.exit() if event.type == KEYDOWN and event.key == K_UP: y -= 20 if event.type == KEYDOWN and event.key == K_DOWN: y += 20 if event.type == KEYDOWN and event.key == K_LEFT: x -= 20 if event.type == KEYDOWN and event.key == K_RIGHT: x += 20 screen.fill((0,0,0)) screen.blit(walls,(x, 330)) # more bricks to go here later pygame.display.flip() #end
and the error:
Traceback (most recent call last): File "C:\Users\dylan\Desktop\Practice Game\move.py", line 8, in walls = pygame.image.load("C:\Users\dylan\Desktop\Practice Game\brick.jpg") error: Couldn't open C:\Users\dylan\Desktop\Practice Gamerick.jpg
I'm using Python 2.6 with PyGame 1.9 for Python version 2.6 with IDLE as my editor.
回答1:
The problem here is that you're using \ as a pathname separator, but \ is also used as an escape character in Python strings. In particular, \b
means "backspace" (or '\x08'
). You get away with the other backslashes because of not-quite-documented-but-reliable behavior that unknown escape sequences like \X
are treated as a backslash followed by an X
.
There are three solutions:
- Use a raw string, which means Python string escapes are ignored:
r"C:\Users\dylan\Desktop\Practice Game\brick.jpg"
. - Escape your backslashes:
"C:\\Users\\dylan\\Desktop\\Practice Game\\brick.jpg"
. - Use forward slashes instead:
"C:/Users/dylan/Desktop/Practice Game/brick.jpg"
.
If you've memorized the list of Python escape sequences, and are willing to rely on functionality that may change but probably won't, you can get away with only escaping the \b
here, but it should be clear why the other three are better ideas in the long run.
While Windows pathnames do natively use backslash separators, all built-in and standard-library Python functions, and most functions in third-party libraries, are perfectly happy to let you use forward slashes instead. (This works because Windows doesn't allow forward slashes in paths at all.)
To understand how and why this works, you might want to try printing out the strings:
>>> print "C:\Users\dylan\Desktop\Practice Game\brick.jpg" C:\Users\dylan\Desktop\Practice Gamrick.jpg >>> print r"C:\Users\dylan\Desktop\Practice Game\brick.jpg" C:\Users\dylan\Desktop\Practice Game\brick.jpg >>> print "C:\\Users\\dylan\\Desktop\\Practice Game\\brick.jpg" C:\Users\dylan\Desktop\Practice Game\brick.jpg >>> print "C:/Users/dylan/Desktop/Practice Game/brick.jpg" C:/Users/dylan/Desktop/Practice Game/brick.jpg
回答2:
walls = pygame.image.load("C:\Users\dylan\Desktop\Practice Game\brick.jpg")
You need to escape the last \
walls = pygame.image.load("C:\Users\dylan\Desktop\Practice Game\\brick.jpg")
Edit: Explanation
You need to escape on the last \
because there is a b right after the \
, and \b
is an escape sequence for backspace. This deletes the character before the sequence. This is why you need to escape the last backslash.
回答3:
Note that Windows paths use the \
character as separators. Those also mean "escape" in a Python string.
Try using a raw string instead:
walls = pygame.image.load(r"C:\Users\dylan\Desktop\Practice Game\brick.jpg")
(Note the r"
in front of the string.)