Why are my pygame images not loading?

跟風遠走 提交于 2019-12-17 22:59:21

问题


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.


回答1:


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"



回答2:


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"




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/5188670/why-are-my-pygame-images-not-loading

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!