Time.Sleep() 正在冻结我的 pygame window 并冻结我的功能

Time.Sleep() is freezing my pygame window and freezing my functions

所以我遇到了一个大问题,我研究了 5 个小时,但一无所获,这就是为什么我现在在这里问。在底部你可以看到我遇到问题的代码。 所以它基本上做的是:它打开一个带有 3 个按钮的 pygame window:一个是开始,一个是停止,一个是退出

当您按下开始循环时,应该 运行 每隔 x 秒播放一次声音,直到您按下停止按钮。 问题是,time.sleep() 确实冻结了整个 window。是的,我研究了很多,但我自己没有得出结论……我是 python 的初学者,我已经做了大约 2 周。这也是我第一次 post 在这个社区,很抱歉,如果这个 post 不一样! :p

我想要的是,当 Tonemaker Loop 运行ning 时,pygame window 保持活动状态,您可以单击其他按钮。

如果您需要更多信息,请询问! 感谢您的帮助!我真的很感谢每一个答案,因为我现在真的卡住了。

import pygame, threading
from pygame import *
import time


run = True
distance = int(input("What Distance you wanna Run? [Meters]:"))
timer = int(input("How fast do you wanna Run it? [Seconds]:"))  
frequence = int(input("The Tone should come every? [Meters]:")) 
# Ask all the needed informations and save them as Variables 

tonerepeats = distance / frequence
tonetimer = timer / tonerepeats  # Calculate the time the progam should wait inbetween the sounds
tonetimer = float(tonetimer)  # make the tonetimer an float (number)  important, otherwise sleep will give an error


pygame.init()
displaywidth = 800
displayheight = 800
black = (0, 0, 0)
grey = (30, 30 ,30)
white = (255, 255, 255)              #stuff for the pygame window
red = (200, 0, 0)
green = (0, 200, 0)
brightgreen = (0, 255, 0)
brightred = (255, 0, 0)

gameDisplay = pygame.display.set_mode((displaywidth, displayheight))
pygame.display.set_caption("Tulsa Driller 1.0")
clock = pygame.time.Clock()
tulsaimg = pygame.image.load("D:/python/tulsa.png")


def Tonemaker():                                                #the loop for the playsound function
    while run == True:  # as long as run is true do this
        pygame.mixer.init()  # needed line for playing an sound
        pygame.mixer.music.load("D:/python/sound.wav")  # load up the sound from libarys
        pygame.mixer.music.play(0)  # play the sound (0 = play 1 time) (1= play infinite)
        time.sleep(tonetimer)
          # After playing the sound wait x time till play it again
        clock = pygame.time.Clock()  # needed line for pygame to work
        clock.tick()  # needed line for pygame to work
        while pygame.mixer.music.get_busy():  # avoid errors with the playsound. "while the sound is playing do nothing"
            pygame.event.poll()
            clock.tick()





def tu(x,y):
    gameDisplay.blit(tulsaimg, (x,y))

x = (displaywidth*0.25 )
y = (displayheight*0.25 )

def text_objects(text, font):
    textSurface = font.render(text, True, white)
    return textSurface, textSurface.get_rect()
def button(msg,x,y,w,h,ic,ac,action=None):                 #function for the buttons
    mouse= pygame.mouse.get_pos()
    click= pygame.mouse.get_pressed()


    if x+w > mouse[0] > x and y+h > mouse[1] > y:
        pygame.draw.rect(gameDisplay, ac, (x, y, w, h))
        if click[0] == 1 and action !=None:
            if action == "start": Tonemaker()           # if you press start it starts the loop
            elif action== "stop": run= False            #if you press stop the loop will stop
            elif action== "quit": quit()                #quit the program, when quit button is pressed


    smalltext = pygame.font.Font("freesansbold.ttf", 20)
    textSurf, textRect = text_objects(msg, smalltext)
    textRect.center = ((x + (w / 2)), (y + (h / 2)))
    gameDisplay.blit(textSurf, textRect)



crashed = False

while not crashed:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True



    gameDisplay.fill(grey)
    tu(x, y)

    button("Start",150,650,100,50,green,brightgreen,"start")
    button("Stop", 350, 650, 100, 50, red, brightred,"stop")       #the 3 buttons
    button("Quit", 550, 650, 100, 50, red, brightred,"quit")

    pygame.display.update()
    clock.tick(75)

pygame.quit()
quit()

看看你是否能从下面的代码片段中得到一些启发。 我不是 pygame 开发人员。但我相信概念,它应该按照下面提到的行进行编码。

import threading
import time

class SoundThread(threading.Thread):
    def __init__(self):
        self.quit = False
        self.event = threading.Event()  # Whenever this event is set, play sound
        self.event.clear()  # By default no sound should be played
        threading.Thread.__init__(self)

    def run(self):
        while not self.quit:
            eventSet = self.event.wait(timeout=1)
            if not eventSet:
                print("Skipping playing the sound")
                continue

            if self.quit:
                break
            # Play sound over here
            print("Play the sound")
            time.sleep(1)

    def begin(self):
        self.event.set()

    def halt(self):
        self.event.clear()

    def quit(self):
        self.quit = True


obj = SoundThread()
obj.start() # Launches a separate thread which can be controlled, based upon calls to begin, halt, and quit


# And add the below to wherever you have the action handlers.
if action == "start": obj.begin()
elif action== "stop": obj.halt()
elif action== "quit": obj.quit()

这是一种结合您已有的主循环的方法。它通过循环检查每次迭代的时间。如果经过了足够的时间,它会调用 Tonemaker() 函数。 (省略无关代码)。

import time

last_time = None
# Start the timer
def StartTone():
    last_time = time.time()
    ToneMaker()
# Stop the timer
def StopTone():
    last_time = None
# Play the sound
def Tonemaker():
    pygame.mixer.init()  # needed line for playing an sound
    pygame.mixer.music.load("D:/python/sound.wav")  # load up the sound from libarys
    pygame.mixer.music.play(0)  # play the sound (0 = play 1 time) (1= play infinite)


while not crashed:
    # Other stuff.....

    # Check elapsed time if timer is running
    if last_time != None:
        now = time.time()
        # Has time elapsed?
        if now - last_time >= tonetimer:
            Tonemaker()
            # Reset
            last_time = now