所以我遇到了一个大问题,自5个小时以来我一直没有进行任何研究,这就是为什么我现在要在这里问问题。在底部,您可以看到代码,这是我遇到的问题。 因此,它的基本作用是:它打开一个带有3个按钮的pygame窗口:一个带有开始按钮,一个带有停止按钮,一个带有退出按钮
当您按下开始时,应该运行一个循环,该循环每x秒播放一次声音,直到您按下停止按钮为止。 问题是,time.sleep()确实冻结了整个窗口。是的,我做了很多研究,但我并没有为自己得出结论……我是python的初学者,我做了大约2周的时间。这也是我在这个社区的第一篇文章,对不起,如果这篇文章与众不同! :p
我想拥有的是,在Tonemaker循环运行时,pygame窗口保持活动状态,并且您可以单击其他按钮。
如果您需要更多信息,请问! 谢谢你的帮助! Iam真的很感谢每个答案,因为Iam确实停留在现在。
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()
答案 0 :(得分:0)
请看看您是否可以从下面的代码片段中汲取灵感。 我不是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()
答案 1 :(得分:0)
这里是一种合并您已经拥有的主循环的方法。它检查循环中每次迭代的时间。如果经过了足够的时间,它将调用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