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.
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.
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.
Do you 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.
Are you facing the exact same error which is mentioned above while executing your code?
my code is like this
screen = pygame.display.set_mode([680, 480])
screen.fill([130, 0, 200])
img = pygame.image.load(file_name)
self.image = pygame.Surface([width,height])
self.rect = self.image.get_rect()
for event in pygame.event.get():
if event.type == pygame.QUIT:
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+)
You can also use the random library's ...READ MORE
colors = ['red', 'green', ...READ MORE
Enumerate() method adds a counter to an ...READ MORE
You can simply the built-in function in ...READ MORE
At least 1 upper-case and 1 lower-case letter
Minimum 8 characters and Maximum 50 characters
Already have an account? Sign in.