2

I'm having trouble opening a png in pygame. I found other ones which recommend using a relative path, but it's still not working. Is there something else I can try?

I'm using Python 3.7.9

Here is my code:

import pygame
import os


current_path = os.path.dirname(__file__)
resource_path = os.path.join(current_path, 'resources')
image_path = os.path.join(resource_path, 'images')

pygame.init()

screen = pygame.display.set_mode((800, 600))

pygame.display.set_caption('Space Invaders')
icon = pygame.image.load(os.path.join(image_path, 'ufo.png'))
pygame.display.set_icon(icon)

running = True

# Game Loop
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

Here is the Error message:

pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "D:\Github\PyGame Lessons\Space Invader\venv\main.py", line 15, in <module>
   icon = pygame.image.load(os.path.join(image_path, 'ufo.png'))
pygame.error: Couldn't open D:\Github\PyGame Lessons\Space Invader\venv\resources\images\ufo.png
YuZora
  • 21
  • 1

0 Answers0