Jump to content

  • Log In with Google      Sign In   
  • Create Account


Need help with pygame images.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Crazylegs830   Members   -  Reputation: 226

Like
0Likes
Like

Posted 05 November 2013 - 06:17 PM

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, 05 November 2013 - 06:20 PM.


Sponsor:

#2 dejaime   Crossbones+   -  Reputation: 3715

Like
1Likes
Like

Posted 05 November 2013 - 06:46 PM

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?



#3 PeterF   Members   -  Reputation: 568

Like
1Likes
Like

Posted 05 November 2013 - 07:19 PM

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()


#4 Crazylegs830   Members   -  Reputation: 226

Like
0Likes
Like

Posted 05 November 2013 - 07:44 PM

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



#5 j-locke   Members   -  Reputation: 811

Like
0Likes
Like

Posted 05 November 2013 - 09:20 PM

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).



#6 LorenzoGatti   Crossbones+   -  Reputation: 2513

Like
0Likes
Like

Posted 06 November 2013 - 02:14 AM

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.
Produci, consuma, crepa

#7 Crazylegs830   Members   -  Reputation: 226

Like
0Likes
Like

Posted 06 November 2013 - 07:11 AM

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



#8 PeterF   Members   -  Reputation: 568

Like
1Likes
Like

Posted 06 November 2013 - 08:40 AM

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.



#9 Crazylegs830   Members   -  Reputation: 226

Like
0Likes
Like

Posted 06 November 2013 - 01:26 PM

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!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS