5

Not sure what I did wrong, I am following a tutorial on how to make a game with Python and PyGame and I get the error:

 pygame.error: Couldn't open resources/images/dude.png    

My code is as follows:

import pygame
from pygame.locals import *


pygame.init()
width, height = 640, 480
screen=pygame.display.set_mode((width,height))

player = pygame.image.load("resources/images/dude.png")

while 1:

    screen.fill(0)

    screen.blit(player, (100,100))

    pygame.display.flip()

    for event in pygame.event.get():


        if event.type==pygame.QUIT:
            pygame.quit()
            exit(0)

The full error message is:

ALSA lib confmisc.c:768:(parse_card) cannot find card '0'
ALSA lib conf.c:4292:(_snd_config_evaluate) function
snd_func_card_driver  returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4292:(_snd_config_evaluate) function snd_func_concat
returned error: No such file or directory
ALSA lib confmisc.c:1251:(snd_func_refer) error evaluating name
ALSA lib conf.c:4292:(_snd_config_evaluate) function snd_func_refer
returned error: No such file or directory
ALSA lib conf.c:4771:(snd_config_expand) Evaluate error: No such file  or
directory
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM default
Traceback (most recent call last):
  File "/root/Documents/PyGame/game.py", line 9, in <module>
    player = pygame.image.load("resources/images/dude.png")
pygame.error: Couldn't open resources/images/dude.png
Jacob Day
  • 53
  • 1
  • 1
  • 3

4 Answers4

6

Use relative paths instead (it's always better to do so) :

import os

current_path = os.path.dirname(__file__) # Where your .py file is located
resource_path = os.path.join(current_path, 'resources') # The resource folder path
image_path = os.path.join(resource_path, 'images') # The image folder path

By doing this, wherever you move the folder containing your .py file, its subdirectories (and therefore whatever they contain) can still be accessed without you having to modify your code.


Final code :

import pygame
import os
from pygame.locals import *


pygame.init()

width, height = 640, 480
screen = pygame.display.set_mode((width, height))

current_path = os.path.dirname(__file__) # Where your .py file is located
resource_path = os.path.join(current_path, 'resources') # The resource folder path
image_path = os.path.join(resource_path, 'images') # The image folder path

player_image = pygame.image.load(os.path.join(image_path, 'dude.png'))

while 1:

    screen.fill(0)

    screen.blit(player, (100,100))

    pygame.display.flip()

    for event in pygame.event.get():


        if event.type==pygame.QUIT:
            pygame.quit()
            exit(0)

Use this accessing method for all your other files, and you'll avoid a bunch of problems.

underscoreC
  • 729
  • 6
  • 13
0

To make it work without the relative path, you need to place the .py file that you created in the pygame folder. The resources folder should also be there.

-3

Python looks for the files in the same folder in which the program is. That code you wrote say that in the same folder that you have your program, there is a folder called resources, and in that, there is a folder called images.If this is the case, I can't help you, but if it isn't, then use the complete file location.

Phonzi
  • 144
  • 3
  • 9
  • 1
    I'm not sure if you were talking about Windows, but on linux, python looks for files to open in the folder where you run the program. the path should be relative to the folder in which you start the program unless it's an absolute path. – Shiping Feb 20 '17 at 03:07
  • Also check capitalization. It sounds stupid, but I personally have spent a lot of time wondering why it doesn't import `dude.png` when the file is actually called `Dude.png` – Synedraacus Feb 20 '17 at 03:11
-3

all you need to do is put this picture in the same folder where your code file is. but this is for Linux