Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pygame problem
#1
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.
Reply
#2
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()
Recommended Tutorials:
Reply
#3
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.
Reply
#4
+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).
Reply
#5
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
Recommended Tutorials:
Reply
#6
(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...t-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.
Reply
#7
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'.
Reply
#8
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pygame*import pygame ImportError: No module named pygame CASPERHANISCH 1 9,708 Jun-05-2017, 09:50 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020