Why does my path not work without details? [duplicate] - python

This question already has answers here:
Could not open resource file, pygame error: "FileNotFoundError: No such file or directory."
(1 answer)
Is there any other way to load a resource like an image, sound, or font into Pygame? [closed]
(1 answer)
Closed 5 months ago.
I am currently working on a little simple game for a class and because I am supposed to then give it to my teacher who will be looking at it on his own computer, I can't use paths like pygame.image.load('C:/Users/name/Desktop/project/mapwitheverything.png')
But when I don't, even if my python file and my image are in the same file, VSC always shows me "FileNotFoundError: No file 'mapwitheverything.png' found in working directory 'C:\Users\name'"
Here is my current code:
class ysortcameragroup(pygame.sprite.Group):
def __init__(self):
super().__init__()
self.display_surface = pygame.display.get_surface()
self.half_width = self.display_surface.get_size()[0] // 2
self.half_height = self.display_surface.get_size()[1] // 2
self.offset = pygame.math.Vector2()
self.floor_surf = pygame.image.load('mapwitheverything.png').convert_alpha()
self.floor_rect = self.floor_surf.get_rect(topleft = (0,0))

Related

Why isn't my image loading on pygame on chromebook? [duplicate]

This question already has answers here:
Python - error: couldn't open .png file [duplicate]
(4 answers)
Could not open resource file, pygame error: "FileNotFoundError: No such file or directory."
(1 answer)
Closed 10 months ago.
I'm coding in python with pygame, but my images won't open. When I try to load an image, this error message pops up: Exception has occurred: error Couldn't open player0.png. I have looked at other people's solutions, but none of them worked. I have tried converting the image into .bmp and .jpg, but it made no difference and gave me the same error message. I have also tried to put it into a separate folder and do this: img = pygame.image.load('assets/player0.png'), but it made no difference.
This is my code currently:
import pygame,sys
from pygame.locals import *
pygame.init()
pygame.display.init()
screen = pygame.display.set_mode((800,600))
player = pygame.image.load('player0.png')
while True:
screen.fill((255,255,255))
screen.blit(player, (100,100))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
Make sure the path to the image is correct.
Check out this solution already posted.
Python - error: couldn't open .png file

Having difficulty using pygame.image.load() [duplicate]

This question already has an answer here:
Could not open resource file, pygame error: "FileNotFoundError: No such file or directory."
(1 answer)
Closed 1 year ago.
I am trying to use the pygame.image.load() function to create an image while following a tutorial.
This is the code that I used:
import pygame
pygame.init()
running=True
screen = pygame.display.set_mode((1350, 700))
pygame.display.update()
wheel = pygame.image.load('wheel_image.png')
while running:
screen.fill(0, 0, 0)
for event in pygame.event.get():
if event.type ==pygame.QUIT:
running=False
screen.blit(wheel, (675, 350))
pygame.display.update()
But when I run it, it gives this error message:
line 6, in <module>
wheel = pygame.image.load('wheel_image.png')
pygame.error: Couldn't open wheel_image.png
I have the png file in the same directory as my code, so have I made an error in my code or what could be causing this?
Whether the file can be found depends on the location from where you call your code. That's inconvenient so better calculate the project's base path using the __file__ property:
from pathlib import Path
BASE = Path(__file__).resolve().parent
wheel_path = BASE / 'wheel_image.png'
wheel = pygame.image.load(wheel_path)
If you use an older version of pygame that can't handle Path objects, manually turn it into a string:
wheel_path = str(BASE / 'wheel_image.png')

pygame can't load image [duplicate]

This question already has answers here:
Could not open resource file, pygame error: "FileNotFoundError: No such file or directory."
(1 answer)
Is there any other way to load a resource like an image, sound, or font into Pygame? [closed]
(1 answer)
Closed 2 years ago.
import pygame, sys
from pygame.locals import *
pygame.init()
window = pygame.display.set_mode((400, 300))
my_image = pygame.image.load(r'/Users/Humberto/documents/python/games/alien')
posX, posY = 200, 150
window.blit(my_image, (posX, PosY))
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
I am trying to load an image that is in the same folder and it will return an error saying pygame.error: Couldn't open /Users/Humberto/documents/python/games/alien I have tried writing the .png extension, also writing the whole path using / and // and also tried the raw string. How can I fix the problem?

Python, pygame: Unable to open .wav file [duplicate]

This question already has answers here:
Could not open resource file, pygame error: "FileNotFoundError: No such file or directory."
(1 answer)
Is there any other way to load a resource like an image, sound, or font into Pygame? [closed]
(1 answer)
I've got an error when trying to create sound using pygame
(1 answer)
Closed 2 years ago.
I am trying to play a simple beat with the pygame.mixer.
import os
import pygame
import pygame.mixer
# Initialize the game
pygame.init()
pygame.mixer.init() # frequency, size, channels, buffersize
print(os.listdir())
# ... some other stuff....
try:
sound = pygame.mixer.Sound("low_click.wav")
print('low_click.wav')
sound.play()
except pygame.error:
pass
The program just prints
['metronom_quintenzirkel.py', 'low_click.wav', 'data']
, so an exception occured.
What has gone wrong?

error: couldn't open .png [duplicate]

This question already has answers here:
pygame.error "couldn't open image.png" only in command prompt
(3 answers)
Closed 2 years ago.
Can someone help me identify where I went wrong? Thanks. I get: pygame.error: Couldn't open space.png. My code is as follows:
import random, math, pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("orbit demo")
space = pygame.image.load("space.png").convert()
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
sys.exit()
#draw backgroud
screen.blit(space, (0,0))
pygame.display.flip()
You may have to do the following:
import pygame
import os
try:
# load from subfolder 'data'
background = pygame.image.load(os.path.join("data","background640x480_a.jpg"))
ball = pygame.image.load(os.path.join("data","snake.gif"))
except:
raise UserWarning, "Unable to find the images in the folder 'data' :-( "
This was obtained from the Python Game Book website. Basically you tell it where to look (in the code above, it is the data folder. If it cannot find it, it reports the User Warning.
So in the example that I'm writing for my game, I have an images folder, so I do this
self.image = background = pygame.image.load(os.path.join("images","starship.png")).convert()
I am a newbie to this, but reading suggests this is how you do it.
EDIT - The other reason could be. Is it the correct format (e.g. png) or have you named the file something else?
Try specifying the full path of the image(s), example: C:/Python34/your_folder/file_name.png
Note: in your python code to refer to a path you can't use "\", must use "/"
the "\" has a specific meaning in python.
For me the answer was I was not running the command from within the folder containing the game file. Both my .py file and .png file are in the top level, so I cd into that and ran and it worked.

Categories