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:
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:
Related Stack Overflow questions:
Related Stack Overflow questions:
Related Stack Overflow questions:
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:
Related Stack Overflow questions:
Related Stack Overflow questions:
Related Stack Overflow questions:
_init_
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:
Related Stack Overflow questions:
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 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 to code a rectangles that start from right in circular motion in pygame
Pygame problem: how to execute conditional on collision?
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