为什么我的 pygame 图像无法加载?
我在网上找到了一个教程(在 Youtube 上),它向我展示了使用 pygame 创建游戏的基础知识。我找到了他所说的那种图像以及他所拥有的东西。他没有说把它们保存到哪里,我想这可能是我的问题。在这里,这是我的程序中的内容以及错误中显示的内容的示例
bif="bg.jpg"
mif="images.png"
import pygame, sys
from pygame.locals import *
pygame.init()
screen=pygame.display.set_mode((420,315),0,32)
backround=pygame.image.load(bif).convert()
mouse_c=pygame.image.load(mif).convert_alpha()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(backround, (0,0))
x,y = pygame.mouse.get_pos()
x -= mouse_c.get_width()/2
y -+ mouse_c.get_height()/2
screen.blit(mouse_c, (x,y))
pygame.display.update()
,错误显示:
Traceback (most recent call last):
File "C:/Python26/pygame.games/pygame
1", line 10, in <module>
backround=pygame.image.load(bif).convert()
error: Couldn't open bg.jpg
请尽快回复。
I found a tutorial online (on Youtube) and it showed me the basics of creating a game with pygame. i found images of the kind he said and what he had. he didn't say where to save them to, i think that may be my problem. here, this is an example of what i have in my program and what shows up on the error
bif="bg.jpg"
mif="images.png"
import pygame, sys
from pygame.locals import *
pygame.init()
screen=pygame.display.set_mode((420,315),0,32)
backround=pygame.image.load(bif).convert()
mouse_c=pygame.image.load(mif).convert_alpha()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(backround, (0,0))
x,y = pygame.mouse.get_pos()
x -= mouse_c.get_width()/2
y -+ mouse_c.get_height()/2
screen.blit(mouse_c, (x,y))
pygame.display.update()
the error reads:
Traceback (most recent call last):
File "C:/Python26/pygame.games/pygame
1", line 10, in <module>
backround=pygame.image.load(bif).convert()
error: Couldn't open bg.jpg
please respond as soon as possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
根据该代码,您的图像文件需要保存在与游戏的 .py 文件相同的目录中。尝试将它们移到那里。
作为健全性检查(以确保图像将加载,实际上这是提供正确路径的问题),您可以临时在代码中指定图像的绝对路径,因此
您可以
但显然更改第二个位指向您的图像实际存储的位置。一旦您确定可以使用绝对路径加载图像,您应该使用相对路径,如果您可能有很多资产,您应该将它们与代码分开,例如将它们放在 < code>assets 文件夹与游戏代码位于同一文件夹或类似文件夹中。
Based on that code, your image files need to be saved in the same directory as your game's .py file. Try moving them there.
As a sanity check (to make sure the images will load and it is in fact an issue with supplying the correct path) you could temporarily specify an absolute path to the image in your code, so instead of
You would have
But obviously change the second bit to point to where your image is actually stored. Once you're sure that the image can be loaded using an absolute path, you should use a relative path, and if you're likely to have many assets you should keep them separate from the code, for example by putting them in an
assets
folder in the same folder as your game code, or similar.最好在运行时使用 os.getcwd() 检查当前工作目录。找到当前工作目录后,添加相对于该路径的资源。例如,您可以将 bg.jpg 放在
/python/game/assets/
并写入:bif=r"python/game/assets/bg.jpg"
It would be a good idea to check the current working directory during runtime using
os.getcwd()
. After finding the current working directory, add the assets relative to that path. For example, You could place bg.jpg at<cwd>/python/game/assets/
and write:bif=r"python/game/assets/bg.jpg"
您可能可以忽略所有这些,或者只阅读部分内容,具体取决于
输出的价值,可能会帮助您,具体取决于您得到的内容以及以下问题之一。
尝试将整个路径添加到包含文件的目录/文件夹。如果可行的话,要么Python不在路径上,要么你只需要找到该目录/引导Python到它。
尝试保存、读取和/或写入文件。如果保存时自动假定相同的驱动器,那么您可能很好,并且需要将 python 引导到它所在的文件夹/目录。如果您将 .py 文件保存在与图像相同的目录/文件中,它们通常不是问题。
做一个测试,谷歌读取和写入文件,并尝试创建一个简单的 .txt 文件。
您还可以使用 Tkinter 中的文件保存/打开/新建对话框。查看它假定将文件保存到/从何处打开。
如果 Python 不是您的 PATH 的一部分,那么有大量的教程和多种添加它的方法。其中一些包括使用内置的本机操作系统 GUI、命令提示符/Bash,甚至还有一些工具可以使其更容易,只需谷歌一下如何将 python 添加到 PATH 即可。您可以进行彻底卸载,这意味着删除并删除所有内容,然后再次安装。
拥有两个不同的驱动器会让事情变得很奇怪。根据上面的测试,您应该看到 Python 使用的驱动器。确保将当前文件以及图像保存到同一目录。
安装多个版本的 Python 会让事情变得很奇怪。即使您卸载了 Python,有时也不会在 Windows 中卸载所有内容,特别是 Windows 市场上的版本可能会把事情搞砸。老实说,如果是这种情况,我会卸载、删除并重新安装到路径。
有时使用第 3 方 IDLE 实际上可以使事情变得更容易。我认为最容易解决 PATH 类型问题的是 Pycharm。如果我没记错的话,它会让你创建虚拟环境,女巫是修复问题的另一种方法(谷歌venv)。如果一切都失败了,那么就会发生一些奇怪的事情,你可以随时删除、卸载和删除 Python 的所有痕迹,再次安装它,确保不留下任何痕迹,并将其添加到安装时的路径中。
关于将文件放入Python库文件夹中的答案是完全错误的。不要这样做,这与将其保存到 root 几乎相同。另外,如果需要整个 Python 库文件夹,你将如何发布任何东西,如果我是那个人,我现在会删除该评论,因为纯粹的尴尬。
我遇到过这个问题,是由于从 Windows Market 下载 3.x 版本造成的。不知何故,它设法将 Python .exe 直接保存在 windows 文件夹中,以及其他奇怪的事情。这使得它如此令人沮丧,并且很难修复,因为它安装得如此奇怪,而且我不记得我从安装程序获得的版本以及从市场获得的版本。我会避开市场,我已经得到了一些简陋的 Windows 子系统,还有“Linux”发行版。
You may be able to ignore all of this, or only read part depending on what
take the output for what it's worth, may help you depending on what you get, and one of the following problems.
Try adding the entire path to the directory/folder with the files. If that works either Python is not on Path or you just needed to find that directory/guide Python to it.
Try saving, reading and/or writing to a file. If when saving it automatically assumes the same drive, then your probably good, and need to just guide python to the folder/directory it is located. Their usually isn't a problem if you are saving the .py file in the same exact directory/file as the images.
Do a test, google reading and writing to files, and try to create a simple .txt file.
Also you can use the file save/open/new dialog in Tkinter. See where it assumes to save or Open files to/from.
If Python isn't part of your PATH then there are tons of tutorials and several ways to add it. Some include using the built in native OS GUI's, command prompt/Bash and there are even tools to make it easier, just google how to add python to PATH. You can do a clean uninstall, meaning removing and deleting everything, and then installing it again.
Having two different drives can make things weird. You should see what drive Python is using based on the tests above. Make sure you are saving the current file to the same directory as well as images.
Having multiple versions of Python installed can make things weird. Even if you uninstall Python, sometimes not everything uninstalls in windows, and specifically the versions on Windows market place can mess things up. I would honestly just uninstall, delete and re-install to path if that's the case.
Sometimes using a 3rd party IDLE can actually make things a lot easier. I think the easiest with the PATH type issues is Pycharm. If I am not mistaken it makes you create virtual environments, witch is another way of fixing things (google venv). If all fails then something weird is going on, and you can always remove and uninstall and delete all traces of Python, install it again making sure no traces are left, and it is added to path on install.
The answer about putting the files in the Python library folder is completely wrong. Don't do that, that's pretty much the same as saving it to root. Also how are you going to release anything if it needs you entire Python Library folder, I would have deleted that comment by now if I were that guy, due to sheer embarrassment.
I have had this problem and it was from downloading a version 3.x from the Windows Market place. Somehow it managed to save the Python .exe in the windows folder directly, and other weird things. This made it so frustrating, and hard to fix as it installed it so weird, and I couldn't remember what version I got from the installer and what version from the Marketplace. I would just avoid the Marketplace, I have gotten some janky Windows Sub System, 'Linux' distros also.
我也遇到了同样的问题。试图找出
与我修复它的方式相同的教程。
这是 pygame 的安装位置,直到我将图像放在那里才起作用。
bif = "C:/Python26/Lib/site-packages/bg.jpg"
以及图像文件的完整位置。请注意,斜杠是向前的,如“/”,
我尝试使用单反斜杠和双反斜杠,如“\”和“\”,但都不起作用。
尝试所有不同的方法,并确保所有内容都拼写正确,图像位于您所说的位置,并且您获得了正确的文件名(.jpg、.JPG 或 .jpeg)您可以只查看属性图像文件的。
I was just having the same problem. trying to figure out the same tutorial
The way I fixed it.
This is where pygame is installed, and it didn't work until I put the images there.
bif = "C:/Python26/Lib/site-packages/bg.jpg"
with the full location of the image file. NOTICE that the slashes are forwards like so "/"
I tried it with single and double backslashes, like so "\", and "\", but neither worked.
Try all the different ways, and make sure everything is spelled correctly, and you images are where you said they are, and that you got the correct file name (.jpg, .JPG, or .jpeg) You can just look at the properties of the image file.
也许您的图像太大了。首先使用其他图像处理应用程序缩小图像,然后重试。
Maybe your image is just too large. Make the image smaller first using other image processing application, and try again.