Public Group

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

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

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 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?

##### 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\\"



##### 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>
error: Couldn't open C:\User\user\Destktop\back.png

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

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

##### 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 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!

• 21
• 13
• 9
• 17
• 13