Need help with pygame images.

Started by
7 comments, last by Crazylegs830 10 years, 5 months ago

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.

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?

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

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

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

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.

Omae Wa Mou Shindeiru

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

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.

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!

This topic is closed to new replies.

Advertisement