开发者

Why are my pygame images not loading? [duplicate]

This question already has answers here: pygame.error "couldn't open image.png" only in command prompt (3 answers) Closed 2 years ago.

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>
    backr开发者_高级运维ound=pygame.image.load(bif).convert()
error: Couldn't open bg.jpg

please respond as soon as possible.


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

bif = "bg.jpg"

You would have

bif = "C:/My_game/images/bg.jpg"

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.

bif = "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"


You may be able to ignore all of this, or only read part depending on what

print pygame.image.get_extended()

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.


Maybe your image is just too large. Make the image smaller first using other image processing application, and try again.


I was just having the same problem. trying to figure out the same tutorial

The way I fixed it.

  1. Put you images in C:\Python26\Lib\site-packages

This is where pygame is installed, and it didn't work until I put the images there.

  1. when declaring your variable for bif and mif, you should write like this;

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜