49552/python-error-pygame-error-couldn-t-open-pygame-png
I'm trying to build the snake game using pygame. I'm following this tutorial.
I end up with the following error:
pygame.error: Couldn't open pygame.png
icon = pygame.image.load('spaceship.png') pygame.error: Couldn't open spaceship.png I am getting this error,\anyone?
Hope this Helps!!
To learn more, join the online course to do Masters in Python.
Thanks!
Hey, @ Ali,
It will be very helpful if you can post your code. Though I can suggest you something like better to use Relative paths instead.
current_path = os.path.dirname(__file__) # your .py file is located
current_path = os.path.dirname(__file__) # the resources 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.
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, 'spaceship.png'))
I hope this will be helpful.
Hi, @There,
You have to add in the file that has the script.
Every application usually ships with various resources, such as image and data files, configuration files and so on. Accessing those files in the folder hierarchy or in a bundled format for various platforms can become a complete task, for which the resources module can provide ideal supportive application components.
The Resource class allows you to manage different application data in a certain directory, providing a dictionary-style access functionality for your in-application resources.
Hey, @Sumit,
Do you import os?
Just add:
import os
in the beginning, before:
from settings import PROJECT_ROOT
This will import the python's module os, which apparently is used later in the code of your module without being imported.
Hey, @There,
Are you facing the exact same error which is mentioned above while executing your code?
my code is like this
import pygame import sys screen = pygame.display.set_mode([680, 480]) pygame.init() screen.fill([130, 0, 200]) class Block(pygame.sprite.Sprite): def __init__(self,file_name,location): img = pygame.image.load(file_name) pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface([width,height]) self.image.fill(color) self.rect = self.image.get_rect() pygame.image.load('ball(2)') while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() pygame.display.flip()
So using this code you want to conclude that there is no error after executing it?
add import os at top of your ...READ MORE
Try installing libpng You can do it with ...READ MORE
You need to download and install the ...READ MORE
pygame.Surface takes in integer values for building ...READ MORE
You must be trying this command in ...READ MORE
file = open('text.txt', 'w+) READ MORE
You can also use the random library's ...READ MORE
Syntax : list. count(value) Code: colors = ['red', 'green', ...READ MORE
Enumerate() method adds a counter to an ...READ MORE
You can simply the built-in function in ...READ MORE
OR
At least 1 upper-case and 1 lower-case letter
Minimum 8 characters and Maximum 50 characters
Already have an account? Sign in.