Here's something that I've tried for a snake game, but I get an error.
I did come up with another way,
(that is, redefining the thread every time the snake touches a coin)
but am still confused as to what the problem was, and how to solve it.
import playsound
import threading
import time
def coin():
playsound.playsound('coin.mp3')
ding = threading.Tread(target=coin)
ding.start()
time.sleep(5)
ding.start()
RuntimeError: threads can only be started once
Try this, it works for me. Source: https://stackoverflow.com/a/20021547/13300960
from pygame import mixer # pip install pygame
import threading
import time
def coin():
mixer.init()
mixer.music.load(r"coin.mp3")
mixer.music.play()
threading.Thread(target=coin).start()
time.sleep(5)
threading.Thread(target=coin).start()
time.sleep(5)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.