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.
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.
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
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.
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…
- How to Solve Turtle_tf Error in ROS
- [Solved] Pygame Install Error: Command errored out with exit status 255: hg clone –noupdate -q https://bitbucket.o
- Python3 Fatal error in launcher: Unable to create process using ‘”‘
- OSError libespeak.so.1 error: no such file or directory [How to Solve]
- [Solved] AttributeError: module ‘logging‘ has no attribute ‘Handler‘
- [How to Solve] ImportError: No module named typing
- [Solved] Pdfplumber Read PDF Sheet Error: AttributeError: function/symbol ‘ARC4_stream_init‘ not found in library
- An error is reported in the requirements code of the generated project
- [Solved] Python Relative Reference Error: ImportError: attempted relative import with no known parent package
- The solution of no such file or directory and cannot load native module running error of python3 pyinstaller after packaging
- Error reading file by pandas pandas.errors.EmptyDataError: no columns to parse from file
- How to Solve Python Importerror: DLL load failed: unable to find the specified program using tensorflow
- Tensorflow import Error: ImportError: libcuda.so.1: cannot open shared object file: No such file or dire
- AttributeError: module ‘enum‘ has no attribute ‘IntFlag‘ [How to Solve]
- [Solved] Python Project Import Module Error: ModuleNotFoundError
- Python: How to Create an Automatic Recording Program
- KeyError: b ‘TEST’ problem in python3 conversion from csn-rcnn code
- An introduction to sys modules in Python and how packages are imported and used
- Import CV2 & Numpy report red Warning and Error [How to Solve]
- [Solved] AttributeError: ‘Manager‘ object has no attribute ‘get_by_natural_key‘