如何停止或结束线程,然后重新启动它?

2024-06-16 12:23:47 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我在蛇游戏中尝试过的东西,但我遇到了一个错误

我确实想出了另一种方法,

(即每次蛇碰到硬币时重新定义线)

但我仍然不清楚问题是什么,以及如何解决它

import playsound
import threading
import time

def coin():
    playsound.playsound('coin.mp3')

ding = threading.Tread(target=coin)

ding.start()

time.sleep(5)

ding.start()

运行时错误:线程只能启动一次


Tags: 方法import游戏定义timedef错误硬币
1条回答
网友
1楼 · 发布于 2024-06-16 12:23:47

试试这个,对我有用。资料来源: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)

相关问题 更多 >