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?
Related
This question already has answers here:
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 7 months ago.
I ran this code on replit.com, but this didn't work and said video system not initialized.
Can anyone tell me what I should do?
import pygame
from pygame.locals import *
pygame.init()
while True:
pygame.display.update()
import pygame
from pygame.locals import *
pygame.init()
while True:
screen = pygame.display.set_mode((640, 480))
pygame.display.update()
You need to define the display dimensions, try the code above.
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
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')
This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 2 years ago.
i have no problem of syntax or any erorr its just a problems when i run the program
import pygame
pygame.init()
# generate window
pygame.display.set_caption("shooter Game")
pygame.display.set_mode((1080, 720))
running = True
while running:
# if the player close the window
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
print("game closed")
sceenshot of the code and the 🚀 of python that not stoping to jump
You don't update the display, which you have to do with
pygame.display.update()
You can add it right after pygame.display.set_mode((1080, 720))
Try like this and tell me if it works :)
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?