Related Stack Overflow questions:
Related Stack Overflow questions:
The current PyGame release, 1.9.6 doesn't support Python 3.9. I fyou don't want to wait for PyGame 2.0, you have to use Python 3.8. Alternatively, you can install a developer version by explicitly specifying the version (2.0.0.dev20 is the latest release at the time of writing):
pip install pygame==2.0.0.dev20
or try to install a pre-release version by enabling the --pre
option:
pip install pygame --pre
Related Stack Overflow questions:
- Unable to install pygame on Python via pip (Windows 10)
- How to troubleshoot python import error - DLL access denied
- When I try to install pygame using python 3.9 I get an error windows
- Installing Pygame on 64-bit Windows 7 and 64-bit Python 2.7
Related Stack Overflow questions:
- Problems getting pygame to show anything but a blank screen on Macos
- pygame installation issue in mac os
- Unable to import Pygame.mixer on an M1 Mac
- Pygame 1.9.6 not loading in Python 3.8.1
- Pygame not showing anything in the window
- Error when installing PyGame on Mac (Catalina) [duplicate]
- Pygame already installed; however, python terminal says “No module named 'pygame' ” (Ubuntu 20.04.1)
- Add pygame module in PyCharm IDE
- Cannot install pygame in Pycharm
- pycharm doesn't recognize pygame package
- Make sure that you use the correct version of 'pip' installed for your Python interpreter located at 'dir:\projectPath\venv\Scripts\python.exe'
Related Stack Overflow questions:
Related Stack Overflow questions:
Related Stack Overflow questions:
Related Stack Overflow questions:
- Bundling data files with PyInstaller (--onefile)
- PyInstaller, spec file, ImportError: No module named 'blah'
- Pyinstaller Unable to access Data Folder
- Python - pygame error when executing exe file
Related Stack Overflow questions:
Related Stack Overflow questions:
Related Stack Overflow questions:
Related Stack Overflow questions:
Related Stack Overflow questions:
- Could not open resource file, pygame error: "FileNotFoundError: No such file or directory."
- Is there any other way to load a resource like an image, sound, or font into Pygame?
- Error - pygame.error: Couldn't open backround.png. Fix?
- I've got an error when trying to create sound using pygame
It is not enough to put the files in the same directory or sub directory. You also need to set the working directory.
The resource (image, font, sound, etc.) file path has to be relative to the current working directory. The working directory is possibly different to the directory of the python script.
The name and relative path of the python file can be retrieved with __file__
. So the absolute path to the python script is os.path.abspath(__file__))
. The current working directory can be changed with os.chdir(path)
.
The current working directory can be get by os.getcwd()
and can be print with print(os.getcwd())
.
Copy the following at the beginning of your code to set the working directory to the same as the script's directory:
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
An alternative solution is to find the absolute path.
If the file is in an subfolder of the python file (or even in the same folder), then you can get the directory of the file and join (os.path.join()
) the relative filepath. e.g.:
import pygame
import os
# get the directory of this file
sourceFileDir = os.path.dirname(os.path.abspath(__file__))
# [...]
# join the filepath and the filename
filePath = os.path.join(sourceFileDir, 'test_bg.jpg')
# filePath = os.path.join(sourceFileDir, '_pycache_/test_bg.jpg')
surface = pygame.image.load(filePath)
The same can be achieved with the pathlib
module.
Change the working directory
import os, pathlib
os.chdir(pathlib.Path(__file__).resolve().parent)
or create an absolute filepath:
import pathlib
# [...]
filePath = pathlib.Path(__file__).resolve().parent / 'test_bg.jpg'
surface = pygame.image.load(filePath)
Related Stack Overflow questions:
- is it possible to run pygame or pyglet in a browser?
- How do I fix pygame.error: video system not initialized in replit
Related Stack Overflow questions:
Related Stack Overflow questions:
Related Stack Overflow questions:
Related Stack Overflow questions:
Related Stack Overflow questions:
Related Stack Overflow questions:
Related Stack Overflow questions:
- Why does it say that module pygame has no init member?
- Pygame error: 'pygame' has no attribute 'init'
Related Stack Overflow questions:
Related Stack Overflow questions:
Related Stack Overflow questions:
Related Stack Overflow questions:
- Local variable referenced before assignment?
- Python 3: UnboundLocalError: local variable referenced before assignment
- Using global variables in a function
In Python, a variable declared outside of the function or in global scope is known as a global variable. This means that a global variable can be accessed inside or outside of the function.
A variable declared inside the function's body or in the local scope is known as a local variable.
Reading a local variable x
before it is declared causes the error:
UnboundLocalError: local variable
x
referenced before assignment
You have to use the global
statement if you want to be interpret the variable as global variable.
Related Stack Overflow questions:
Related Stack Overflow questions:
Related Stack Overflow questions:
- How to draw a rectangle around two points
- I am wondering why it says the distance has to be less than 27? Why less than 27? Where is 27 coming from?
- Why is the math of my stacking game not working?
How to output pygame.image.save to a variable instead of a file?
Collision detection in pygame not working
How to draw with mouse and save as 1s and 0s
Pygame low frame rate on simple game
How can I create a window where my program draws a dot ( which is point.png here ) wherever I click on the screen?
How to code a rectangles that start from right in circular motion in pygame
Pygame problem: how to execute conditional on collision?
How do i correctly update the position and velocity of 100 class objects individually drawing with pygame?
How can I blit an image on the screen where i click after pressing a button
pygame.mouse.get_pressed() not responding
How to shade all the cells that will be useless when you click on them?
How can I make my recursive function go back to continue recursing other cells
How do I add a rubber band for mouse dragging in PyGame?
Pygame Lever Images Display On Key Click Problem
Adding rects in list for displaying in 7-segment digits in pygame
📁 Minimal example - segment display
repl.it/@Rabbid76/PyGame-7SegementDisplay
Colours not printing - connect 4