Jump to content
  • Advertisement
Sign in to follow this  
Crazylegs830

Need help with pygame images.

This topic is 2079 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I am having yet another problem. I have tried many different solutions but I get this weird error.

 

Traceback (most recent call last):
  File "C:\Users\user\Desktop\Python Programs\Examples\Pygame\Pygame_example2.py", line 9, in <module>
    background=pygame.image.load(bg1).convert()
error: Couldn't open C:\Users\user\Desktop      est.jpg

 

and my code,

 

bg1="C:\Users\user\Desktop\test.jpg"

mif="C:\Users\user\Desktop\back.png"

 

import pygame, sys
from pygame.locals import *

 

pygame.init()

screen=pygame.display.set_mode((800,600),0,32)
background=pygame.image.load(bg1).convert()

mouse_c=pygame.image.load(mif).convert_alpha()

 

x,y=0,0
movex, movey=0,0

 

while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type==KEYDOWN:
            if event.key==K_LEFT:
                movex=-1
            elif event.key==K_RIGHT:
                movex=+1
            elif event.key==K_UP:
                movey=-1
            elif event.key==K_DOWN:
                movey=+1
        if event.type==KEYUP:
            if event.key==K_LEFT:
                movex=0
            elif event.key==K_RIGHT:
                movex=0
            elif event.key==K_UP:
                movey=0
            elif event.key==K_DOWN:
                movey=0

    x+=movex
    y+=movey

    screen.blit(background,(0,0))
    screen.blit(mouse_c,(x,y))

    pygame.display.update()

 

Sorry If this is bad code or I am being stupid I am new to programming.

Edited by Crazylegs830

Share this post


Link to post
Share on other sites
Advertisement

It looks like the file path pygame is trying to open is different than the one you passed to it, according to the pasted error message.

 

Have you tried to open a file that resides on the same folder that your application?

Share this post


Link to post
Share on other sites

The \t is a tab character (you can see it in your error message). Be sure to escape your \ characters when using them in strings with \\ or you'll end up with weird errors like that.

 

An easier way to keep your image files would be to have a standard location for them, hardcode that path in, then just concatenate the strings when you reference a file, so you won't have to worry about remembering to escape those pesky \ characters as often.

 

i.e.

IMAGE_PATH = "C:\\User\\Documents\\python images\\"

background = pygame.image.load(IMAGE_PATH + "example.bmp").convert()

Share this post


Link to post
Share on other sites

It looks like the file path pygame is trying to open is different than the one you passed to it, according to the pasted error message.

 

Have you tried to open a file that resides on the same folder that your application?

Thanks for the answer, but I got this error:

 

Traceback (most recent call last):
  File "C:\Users\user\Desktop\Python Programs\Snake\snake_code1.py", line 10, in <module>
    background=pygame.image.load(bg1).convert()
error: Couldn't open C:\User\user\Destktop\back.png

Share this post


Link to post
Share on other sites

I'd like to re-ask what dejaime asked, have you tried to open a file that's in the same folder as your application? That would let us know that the code written does what you want it to do (once it successfully gets the image).

Share this post


Link to post
Share on other sites

error: Couldn't open C:\User\user\Destktop\back.png


The right file name is presumably C:\Users\user\Destktop\back.png, not C:\User\user\Destktop\back.png.

Share this post


Link to post
Share on other sites

I'd like to re-ask what dejaime asked, have you tried to open a file that's in the same folder as your application? That would let us know that the code written does what you want it to do (once it successfully gets the image).

Still not working sad.png .

 

Traceback (most recent call last):
  File "C:\Users\user\Desktop\Python Programs\Snake\snake_code1.py", line 11, in <module>
    mouse_c=pygame.image.load(snake).convert_alpha()
error: Couldn't open C:\Python27\include\dot.png

Share this post


Link to post
Share on other sites

Does the Pygame distribution you're using support .png file formats? Try saving it as a .bmp and open that file. .bmp images are always going to be supported (according to the people maintaining Pygame) but other file formats may not be, or may go in and out of support.

Share this post


Link to post
Share on other sites

Does the Pygame distribution you're using support .png file formats? Try saving it as a .bmp and open that file. .bmp images are always going to be supported (according to the people maintaining Pygame) but other file formats may not be, or may go in and out of support.

This and the other thing you suggested fixed it Thanks!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!