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

pygame.init()

#Colours here
RED      =  pygame.Color(150,   0,   0)
GREEN    =  pygame.Color(  0, 150,   0)
BLUE     =  pygame.Color(  0,   0, 150)
BLACK    =  pygame.Color(  0,   0,   0)
WHITE    =  pygame.Color(255, 255, 255)

FPS = pygame.time.Clock()

WIDTH  = 1024
HEIGHT = 768
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Penaldo v0.1')

#Set BACKGROUND to an instance of pygame.Surface, which will get its coordinates from the previous ones we assigned to the game's display
BACKGROUND = pygame.Surface((SCREEN.get_width(), SCREEN.get_height()))

SCREEN.blit(BACKGROUND, (0, 0))

key_press = pygame.key.get_pressed()

class Player():
    def __init__(self):


        #Try to load our sprite.
        #'r' makes it a raw string so it ignores escape sequences

        self.player = pygame.image.load(r"\..\..\resources\mario_sprite_by_killer828-d3iw0tz.png")
        self.p_rect = self.player.get_rect()

        #Spawn the player just in the middle of our screen
        self.p_rect.centerx = WIDTH / 2
        self.p_rect.centery  = HEIGHT / 2

    def move(self):
        if key_press[UP]:
            self.p_rect.y -= 5

        elif key_press[DOWN]:
            self.p_rect.y += 5

        elif key_press[LEFT]:
            self.p_rect.x -= 5

        elif key_press[RIGHT]:
            self.p_rect.x += 5

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
        #Some IDLE friendliness (prevents from hanging)
            pygame.quit()
            sys.exit()


    BACKGROUND.fill(GREEN)

    p1 = Player()

    # FOR THE GAME TO WORK you have to include this one inside main while-loop (no display update = no game)
    pygame.display.update()
FPS.tick(40)

The full error message is:

Traceback (most recent call last):
  File "C:\Users\NZXT\Documents\GameLab\Penaldo\game\version1\Penaldo.py", line 79, in <module>
    p1 = Player()
  File "C:\Users\NZXT\Documents\GameLab\Penaldo\game\version1\Penaldo.py", line 49, in __init__
    self.player = pygame.image.load(r"\..\..\resources\mario_sprite_by_killer828-d3iw0tz.png")
error: Couldn't open \..\..\resources\mario_sprite_by_killer828-d3iw0tz.png

I read a few questions such as Python 2.6: "Couldn't open image" error, error: Couldn't open Image and I've tried to implement the different tips but the window just goes black and hangs.

Community
  • 1
  • 1
WorkShoft
  • 440
  • 6
  • 18

3 Answers3

0

Looking at that /../../ I see you are trying to look for a dynamic file path depending on the users file path to the image? That isn't how it works, and that is why you are getting that error. It can't find the .PNG file because there is no such thing as :

 \..\..\resources\mario_sprite_by_killer828-d3iw0tz.png

You can look at this article that explains it well: Relative paths in Python

Community
  • 1
  • 1
macas
  • 67
  • 1
  • 9
0

I have a few ideas.

  1. Perhaps you entered the image name incorrectly.
  2. Or the folders in the path don't exist.
  3. You may have misplaced the file.
  4. You didn't consider your operating system.

And here are a few suggestions:

  1. Rather than backslashes, use os.path.join('..', '..', 'resources', 'mario_sprite_by_killer828-d3iw0tz.png') inside pygame.image.load. This will correctly load for Linux, Windows, and Mac which ever one you may be using.

  2. Try using a more organized arrangement so that backtracking that far will not be necessary.

  3. Check and/or rename the file to ensure you're using the correct path. Make sure file is actually a png.

  4. Use os.chdir to change the current directory to the folder in which the image is contained then use pygame.image.load('mario_sprite_by_killer828-d3iw0tz.png') as you normally would.

Give these a try. By the way, your key events won't work as expected because the key_press variable isn't continuously updated, you simply initialized it at the beginning.

Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
0

Usually to ensure the image file can be loaded without raising errors about not being able to find your image, put that image in the same folder as your program. As for PyCharm users, just copy and paste the file into your current project that has the correct program. Using os.path.join() might be better if you dislike doing it the other way. Try going to the Pygame Docs for more information if you have time. Your problem is that you are using backward slashes () instead of using forward slashes (/). You should change your path from:

\..\..\resources\mario_sprite_by_killer828-d3iw0tz.png

to:

/../../resources/mario_sprite_by_killer828-d3iw0tz.png

Credit for this answer and the real explanation of your problem can be found in this problem in Stack Overflow: Python 2.6: "Couldn't open image" error I hope this helps you! Oh, if the question link above doesn't help, use the Pygame Docs link instead.

Community
  • 1
  • 1
Anthony Pham
  • 3,096
  • 5
  • 29
  • 38