如何在一定时间后blit一个新的背景图像?

时间:2015-05-10 17:02:51

标签: python image pygame

我目前在游戏中有背景图片,我想在30秒后更改图像。什么是最好的方法,一个功能?一个循环?或pygame函数,如pygame等待函数?

def main():
    #Loading the music for the title screen of the game and playing it infintely.
    sound = pygame.mixer.Sound('C:\\Users\\Joseph Molina\\Desktop\\CST\\KeyGen.ogg')
    sound.play(loops = -1)

    while True:

        global total_frames
        process(bug,FPS, total_frames, SCREENHEIGHT)

        #LOGIC
        bug.motion(SCREENWIDTH, SCREENHEIGHT)
        Enemy.movement(SCREENWIDTH)
        BugProjectile.movement()
        total_frames += 1


    #Bliting the image of the background into the screen at the given coordinates
        screen.blit(backgroundImg, (0,0))

        #Draws all the sprites to the screen
        BaseClass.allsprites.draw(screen)
        i = 0
        if i < 255:
            i += 5
        if i > 255:    
            secondImg = pygame.image.load('C:\\Users\\Joseph Molina\\Desktop\\CST\\anything.jpg')
            screen.blit(secondImg, (0,0))


              #pygame.tick(1000)



        #Makes sure that everything is being drawn on the screen
        pygame.display.flip()
        #How many frames are going to be in a second
        clock.tick(FPS)

1 个答案:

答案 0 :(得分:0)

使用pygame.time.set_timer()创建活动:

  

pygame.time.set_timer()

     

在事件队列上重复创建一个事件

     

set_timer(eventid, milliseconds) -> None

     

将事件类型设置为每隔给定的毫秒数显示在事件队列中。第一个事件将在经过一段时间后才会出现。

     

每个事件类型都可以附加一个单独的计时器。最好使用pygame.USEREVENT和pygame.NUMEVENTS之间的值。

     

要禁用事件的计时器,请将milliseconds参数设置为0。

当事件发生时,只需将backgroundImg更改为另一个Surface

def main():
    CHANGEEVENT = pygame.USEREVENT+1
    pygame.time.set_timer(MOVEEVENT, 30000)

    #Loading the music for the title screen of the game and playing it infintely.
    sound = pygame.mixer.Sound('C:\\Users\\Joseph Molina\\Desktop\\CST\\KeyGen.ogg')
    sound.play(loops = -1)

    while True:
        if pygame.event.get(CHANGEEVENT ):
            backgroundImg = get_another_image()
        ...