“排序之声”

时间:2016-05-28 20:32:13

标签: python multithreading pygame

我正在使用pygame在python 2.7.10中编写一个小程序。我正在使用多线程播放声音(带有winsound)并且仍能快速绘制内容。我试图复制一个类似的想法,我在youtube上看到的一些视频排序算法。不幸的是它运行了大约10秒然后随机崩溃,声音继续,但pygame窗口没有响应,绘图不会改变。即使我在某个地方的代码中打印“hi”,它仍将在控制台中打印,但pygame仍然没有响应。不能想到什么是错的,但它可能是多线程的东西(我很新!)

from random import randint
import pygame,winsound,threading,time
from pygame.locals import *
screen=pygame.display.set_mode([1000,500])
def bubbleSort(listName):
    sorting=True
    while sorting:

        listChanged=False
        for i in range(len(listName)-1):
            draw(listName,1000/len(listName),500/11,i)

            if listName[i] < listName[i+1]:
                listName[i],listName[i+1]=listName[i+1],listName[i]
                threading.Thread(target=playsound,args=([listName[i]])).start()
                listChanged=True
        if not listChanged:
            sorting=False
            time.sleep(10)
            pygame.quit()

def playsound(frequency):
    winsound.Beep(frequency*100,100)




def draw(listName,width,height,comparing):
    print "hi"
    screen.fill([0,0,0])
    for i in range(len(listName)):
        if i == comparing or i == comparing+1:
            pygame.draw.rect(screen,[0,255,0],[width*(len(listName)-i),500-(height*listName[i]),width,500])
        else:
            pygame.draw.rect(screen,[255,255,255],[width*(len(listName)-i),500-(height*listName[i]),width,500])
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
    pygame.display.update()  




list1=[]
for i in range(100):
    list1.append(randint(1,10))

threading.Thread(target=bubbleSort,args=([list1])).start()

0 个答案:

没有答案