Python 3.7.3+Pygame加载JPG图片报错原因咨询
Let's break down exactly why that line walkRight = [pygame.image.load('Person.JPG')] is causing you trouble—there are a few common, easy-to-fix culprits here:
Incorrect file path (the most likely cause)
When you use just a filename like'Person.JPG', Pygame looks for the image in the current working directory of your Python script. If your image isn't saved in the same folder as your.pyfile, or if IDLE's default working directory is set to something else (like your Documents folder instead of where your script lives), Pygame can't locate the file and throws aFileNotFoundError.- If the image is on your desktop, you'll need to use the full absolute path to it. For Windows, that might look like
'C:/Users/YourUsername/Desktop/Person.JPG'(use forward slashes, or double backslashes like'C:\\Users\\YourUsername\\Desktop\\Person.JPG'to avoid escape character issues). - A simpler fix: move the
Person.JPGfile into the same folder as your Python script, then the filename alone will work.
- If the image is on your desktop, you'll need to use the full absolute path to it. For Windows, that might look like
Filename typos or case sensitivity
Even on Windows (which is mostly case-insensitive), small typos or case mismatches can cause problems. Double-check that your image is exactly namedPerson.JPG—notperson.jpg,Person.jpg, or something with a misspelled word.Corrupted image file (rare but possible)
If the JPG itself is damaged, Pygame won't be able to load it. Try opening the image in a regular image viewer (like Paint or Photos) to confirm it works, or test loading a different JPG file to rule this out. Note that you already calledpygame.init()correctly, so Pygame's image handling should be initialized properly.
One quick side note: right now, your code loads the image but never uses it—you're drawing a red rectangle instead. Once you fix the loading issue, you can replace the pygame.draw.rect line with win.blit(walkRight[0], (x, y)) to display your character image instead of the rectangle.
内容的提问来源于stack exchange,提问作者Account_2

