-4

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

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Red
  • 26,798
  • 7
  • 36
  • 58
  • See middle part of this answer https://stackoverflow.com/a/29692864/13300960 You cannot restart a thread. Creating a new one is your best option. – Luka Mesaric May 18 '20 at 17:56
  • What exactly is the problem? You want to play a sound on a specific event? Why is creating a thread and starting it now working for you? – Luka Mesaric May 18 '20 at 17:58
  • I tried to run it and I got playsound.PlaysoundException: A problem occurred in initializing MCI. Unfortunately I do not know any library that could help you, but I would assume there must be some available. – Luka Mesaric May 18 '20 at 18:05
  • Actually I tried this and it worked: https://stackoverflow.com/a/20021547/13300960 You should also use it in another thread. – Luka Mesaric May 18 '20 at 18:10
  • It has only 11MB when extracted, it is really not that large, especially considering other things don't really work. – Luka Mesaric May 18 '20 at 18:16

1 Answers1

1

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)
Luka Mesaric
  • 657
  • 6
  • 9