I’m trying to make a little pong game (for learning the basics of coding) and I play a sound every time the ball hits anything. when implementing this I found that I need to (or might need to) use multithreading to play a sound and run the game at the same time. but when trying to play sounds at the same time they become delayed.
I used winsound to play audio and the threading library to multithreaded. every time the ball hit a pong or wall i used this line of code:
sound_thread = threading.Thread(target=lambda:winsound.PlaySound(r”sound.wav”,winsound.SND_FILENAME))
sound_thread.start()
which works fine unless as explaned before too much audio at the same time comes. i thought i could just make another thread for every wall or pong. but when trying to make muiltiple threads like this:
sound_thread_1 = threading.Thread(target=lambda:winsound.PlaySound(r”sound1″, winsound.SND_FILENAME))
sound_thread_1.start()
sound_thread_2 = threading.Thread(target=lambda:winsound.PlaySound(r”sound2″, winsound.SND_FILENAME))
sound_thread_2.start()
it stll has the same effect and i have a feeling it has something to do with the lambda being the target but i might just be doing something wrong. (this is my first stack overflow question so it be a little hard to follow)
josh dehut is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.