如何在pygame中使用计时器以一定间隔触发事件?

时间:2016-04-10 04:31:08

标签: python python-2.7 pygame

我正在用pygame写一个闪电和雨的蟒蛇动画。闪电的动画每两秒发生一次,同时雨的动画不断发生。

我目前正在使用time.sleep()模块来延迟闪电 每一个,但它延迟整个计划。

最好的办法是什么?。

#Before while loop, list and variable for animating rain

list_rain=[]
for i in range(40):
    x=random.randint(400,700)
    y=random.randint(96,690)
    list_rain.append([x,y])

    q=0

#In main program loop(while loop)



#code for animating lightning
q+=5
if q <100:
    pygame.draw.polygon(screen,YELLOW,[[430,85],[410,95+q],[450,95+q]])
    if q >80:
        pygame.draw.rect(screen,YELLOW,[412,180,light_y,14])
        pygame.draw.polygon(screen,YELLOW,[[505,180],[525,180],[480,250+q]])
if light_y >100:
    light_y=0
    #why does time.sleep() affect raining?
    time.sleep(2)
    #end code for animating lightning


#start code for animating rain
for i2 in range(len(list_rain)):
    pygame.draw.circle(screen,BLUE,list_rain[i2],2)
    list_rain[i2][1]+=1
    if list_rain[i2][1]> 690:
        list_rain[i2][1]=110
    #end code for animating rain

0 个答案:

没有答案