1

I am programming a mini game and I need to load some images but when I loaded images sone unexpected error raised can someone please help me to get out of it Here is this code

#-*-coding:utf8;-*-
#qpy:pygame

import sys
import pygame
from pygame.locals import *

pygame.init()
surface = pygame.display.set_mode((640, 480))
image = pygame.image.load("Audi.png").convert()
clock = pygame.time.Clock()

while True:
    for ev in pygame.event.get():
         if ev.type == QUIT:
            pygame.quit()

   clock.tick(60)
   surface.fill((0, 0, 0))
   surface.blit(label, (0, 0))
   pygame.display.flip()

And error which I am getting is in this image

1 Answers1

1

The image filepath has to be relative to the current working directory. The working directory is possibly different to the directory of the python file.

The name and path of the file can be get by __file__. The current working directory can be get by os.getcwd().

If the image is in the same folder as the python file, then you cab get the directory of the file and concatenate the image filename. e.g.:

import sys
import pygame
from pygame.locals import *
import os

# get the directory of this file
sourceFileDir = os.path.dirname(os.path.abspath(__file__))

# [...]

image = pygame.image.load( os.path.join(sourceFileDir, "Audi.png") ).convert()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174