Python Forum
pygame problem - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: pygame problem (/thread-5010.html)



pygame problem - Yair - Sep-14-2017

this is my code:

import pygame



pygame.init()


display_width = 800
display_height = 600

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('A bit Racey')

black = (0,0,0)
white = (255,255,255)

clock = pygame.time.Clock()
crashed = False
carImg = pygame.image.load('racecar.png')

def car(x,y):
    gameDisplay.blit(carImg,(x,y))

x = (display_width * 0.45)
y = (display_height * 0.8)

while not crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

    gameDisplay.fill(white)
    car(x,y)

        
    pygame.display.update()
    clock.tick(60)

pygame.quit()
quit()
this is the error:
Error:
Traceback (most recent call last): File "C:\Users\G2\AppData\Local\Programs\Python\Python36-32\A bit racey.py", line 19, in <module> carImg = pygame.image.load('racecar.png') pygame.error: Couldn't open racecar.png
From some reason pygame cant load my image. the image is in the code folder and pygame is succesfully installed.
thank you.


RE: pygame problem - metulburr - Sep-14-2017

Quote:the image is in the code folder
It is in relation to the script that you posted. So if your directory structure is like this...

program/
    script.py
    code/
        racecar.png
then you would need to load the image as 'code/racecar.png'

Also you should do convert()
pygame.image.load('code/racecar.png').convert()
or better yet
pygame.image.load(os.path.join('code','racecar.png')).convert()



RE: pygame problem - nilamo - Sep-14-2017

Quote:
Error:
Traceback (most recent call last): File "C:\Users\G2\AppData\Local\Programs\Python\Python36-32\A bit racey.py", line 19, in <module>

There's no reason to save your files in such a wacky place (that looks like where python might store temporary compiled modules). Also, for the love of holy hand grenades, please don't have spaces in file names... use dashes or underscores instead.


RE: pygame problem - PySam - Sep-14-2017

+1

I did that exact tutorial a couple days ago.
Move your python code or even redo the installation to something simple like C:\Python\

I save my programs inside that directly directly or on my home PC, inside \Python\programs. IN either case I save any pictures inside with the scripts. Just to keep things simple and avoid errors like you're having above.

Also, +1 to not having spaces in file names, whether it's a script you're writing, or the name of the picture (especially).


RE: pygame problem - metulburr - Sep-14-2017

Can you link that tutorial? Does it actually suggest to name the script A bit racey.py? Or to put your images in a directory named code?

Quote:for the love of holy hand grenades
Good wording! I had to give you rep for that lol


RE: pygame problem - PySam - Sep-14-2017

(Sep-14-2017, 03:37 PM)metulburr Wrote: Can you link that tutorial? Does it actually suggest to name the script A bit racey.py? Or to put your images in a directory named code?

Quote:for the love of holy hand grenades
Good wording! I had to give you rep for that lol

Here's the link BTW, and no it doesn't state anywhere that I recall (too lazy to reread) to name the program that way or the picture. The author basically tells you to download the picture or make your own.
https://pythonprogramming.net/displaying-images-pygame/?completed=/pygame-python-3-part-1-intro/
The image loading portion from the tut:
carImg = pygame.image.load('racecar.png')
I think the OP is possibly new to programming so perhaps a little brushing up with or practice with some conventions would help out.


RE: pygame problem - Yair - Sep-17-2017

Thank you all!! I have changed the file location to somewhere else and it works now.
Also i have changed the file name to 'A_bit_racey'.


RE: pygame problem - sylas - Sep-17-2017

From sylas: can you tell me where I can upload yout racecar.png . There are so many Libraries. I am interested by your thread. Thanks