When developing games using python, we will inevitably use the pyGame module, which has a sound function. Using this function, we can add sound effects to our games.
Problem Description:
To use the sound module, we must initialize our game at the beginning of the main function, so we add the following statement at the beginning of the main function to initialize the game.
# Game initialization
pygame.init()
However, when I run the program, I find that the game window flashes back and an error message appears, as follows:
D:\Game\TankWar\venv\Scripts\python.exe D:/Game/TankWar/main.py
pygame 2.0.2 (SDL 2.0.16, Python 3.8.5)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "D:/Game/TankWar/main.py", line 41, in <module>
is_quit_game = run_Game(config)
File "D:/Game/TankWar/main.py", line 22, in run_Game
sounds[key] = pygame.mixer.Sound(value)
pygame.error: mixer not initialized
Process finished with exit code 1
it says I didn’t initialize the mixer!!! We can’t help it. Let’s go according to his error report and initialize the mixer separately.
pygame.init()
pygame.mixer.init()
The following error message still appears, and the game window still flashes back.
D:\Game\TankWar\venv\Scripts\python.exe D:/Game/TankWar/main.py
pygame 2.0.2 (SDL 2.0.16, Python 3.8.5)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "D:/Game/TankWar/main.py", line 42, in <module>
is_quit_game = run_Game(config)
File "D:/Game/TankWar/main.py", line 17, in run_Game
pygame.mixer.init()
pygame.error: WASAPI can't find requested audio endpoint: Could not find the element.
Process finished with exit code 1
Solution:
After repeated tests, I found that it can run normally sometimes, and the above error reports will appear sometimes. Finally, I found a big man’s article and solved this problem.
earphone problem
Because I use a desktop computer and have no audio connected, there has been no audio output device, which causes pyGame to not know where to output the sound (in this case, the audio device cannot be found), resulting in an error. After inserting the audio device (i.e. my headset), it’s solved…
Read More:
- [Solved] Pygame Install Error: Command errored out with exit status 255: hg clone –noupdate -q https://bitbucket.o
- [Solved] transformers Install Error: error can‘t find rust compiler
- [Solved] Grid Search Error (GridSearchCV): ‘ascii‘ codec can‘t encode characters in position 18-20: ordinal not in r
- [Solved] ValueError: Connection error, and we cannot find the requested files in the cached path…
- [Solved] AttributeError: ‘WebDriver‘ object has no attribute ‘find_element_by_id‘
- [Solved] Python Selenium Error: AttributeError: ‘WebDriver‘ object has no attribute ‘find_element_by_xpath‘
- CMake Error at Could NOT find Python3 [Solved]
- NPM install Error: gyp ERR! stack Error: Could not find any Python installation to use
- [Solved] torchvision Error: UserWarning: Failed to load image Python extension: Could not find module
- FileNotFoundError: Could not find module ‘D:\Anaconda3\envs\labe\Library\bin\geos_c.dll‘ [Solved]
- [Solved] TensorFlow Error: GetNext() failed because the iterator has not been initialized
- Not find or load Qt Platform plugin “windows“ [How to Solve]
- Jupyter notebook Failed to Switch to the Virual Environment: DLL load failed python.exe could not find the entry
- AttributeError: can‘t set attribute [How to Solve]
- GPY installation error: ERROR: Could not find a version that satisfies the requirement scipy…
- [Solved] git review Error: UnicodeDecodeError: ‘gbk‘ codec can‘t decode
- Ctypes DLL Error: FileNotFoundError: Could not find module ‘***_dll.dll‘ (or one of its dependencies).
- [Solved] YOLO v5 Error: AttributeError: Can‘t get attribute SPPF on module models
- [Solved] ValueError: only one element tensors can be converted to Python scalars
- Python Selenium: element is not attached to the page document error