Pygame视频闪烁

时间:2017-03-21 07:39:52

标签: python video pygame

我一直在玩pygame上的视频,我发现我的视频在以下代码中闪烁了很多:

import pygame

pygame.init()

FPS = 60

resolution = (240, 160)

clock = pygame.time.Clock()

pygame.mixer.quit()

screen = pygame.display.set_mode(resolution)

surface = pygame.Surface(resolution).convert()

movie1Playing = True
movie2Playing = False
playedEarly = False
frames = 0
earlyCounter = 0

movie = pygame.movie.Movie('./intromovie1.mpg')
movie.set_display(screen)

movie.play()
movie.set_volume(0.5)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            movie.stop()
            raise SystemExit
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN:
                if frames > 300 and movie1Playing:
                    movie.stop()
                    playedEarly = True
                    movie1Playing = False
                    movie2Playing = True
                    movie = pygame.movie.Movie('./intromovie2.mpg')
                    movie = pygame.movie.Movie('./intromovie2.mpg')
                    surface = pygame.Surface(movie.get_size()).convert()
                    movie.set_display(surface)
                    movie.play()
                    movie.set_volume(0)
                    frames = 0
                elif movie2Playing:
                    raise SystemExit

    if movie.get_busy() == 0:
        if movie1Playing == True:
            movie.stop()
            movie1Playing = False
            movie2Playing = True
            movie = pygame.movie.Movie('./intromovie2.mpg')
            movie = pygame.movie.Movie('./intromovie2.mpg')
            surface = pygame.Surface(movie.get_size()).convert()
            movie.set_display(surface)
            movie.play()
            movie.set_volume(0.5)
            frames = 0
        else:
            movie.stop()
            movie1Playing = True
            movie2Playing = False
            movie = pygame.movie.Movie('./intromovie1.mpg')
            movie = pygame.movie.Movie('./intromovie1.mpg')
            surface = pygame.Surface(movie.get_size())
            movie.set_display(surface)
            movie.play()
            movie.set_volume(0.5)
            frames = 0

    if playedEarly:
        earlyCounter += 1
        if earlyCounter > 67:
            movie.set_volume(0.5)
            playedEarly = False
            earlyCounter = 0
    frames += 1

    screen.blit(surface,(0,0))
    pygame.display.flip()
    clock.tick(FPS)

我已经尝试删除blit和pygame.display.flip()方法,这似乎可以减少闪烁,但是第二个视频应该在第一个视频之后立即播放,如果删除了第二个视频则没有一点都玩。任何帮助表示赞赏!

!! EDIT !!

在完成第一个视频播放后,视频实际上可以正常播放。例如,如果我让第一个视频播放或跳过它,那么第二个视频播放完美无缺,当它再循环播放第一个视频时,该视频也可以完美播放

1 个答案:

答案 0 :(得分:0)

我实际上只是为那些在同样的事情上挣扎的人解决了我自己的问题。似乎pygame在整个运行过程中都需要有戒烟的威胁。我的意思是,不应该调用“raise SystemExit”,而应该改为调用pygame.quit()。这是清理过的代码:

import pygame

pygame.init()

FPS = 60

#Scales
GAMEBOY         = 1        #240x160  (Standard gameboy resolution)
FOUREIGHTYP     = 2        #480x320  (480p resolution)
SEVENTWENTYP    = 4        #960x640  (720p resolution)
TENEIGHTYP      = 6        #1440x960 (1080p resolution)

scale = GAMEBOY

resolution = (240*scale, 160*scale)

clock = pygame.time.Clock()

pygame.mixer.quit()

screen = pygame.display.set_mode(resolution)

surface = pygame.Surface(movie.get_size()).convert()

#Gamestates
introsequence = 1
mainmenu      = 2

def introsequence(screen, surface):
    movie1Playing = True
    movie2Playing = False
    playedEarly = False
    frames = 0
    earlyCounter = 0

    movie = pygame.movie.Movie('./intromovie1.mpg')
    movie.set_display(surface)
    movie.play()
    movie.set_volume(0.5)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                movie.stop()
                pygame.quit()   #<---- Here
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    if frames > 300 and movie1Playing:
                        movie.stop()
                        playedEarly = True
                        movie1Playing = False
                        movie2Playing = True
                        movie = pygame.movie.Movie('./intromovie2.mpg')
                        movie = pygame.movie.Movie('./intromovie2.mpg')
                        surface = pygame.Surface(movie.get_size()).convert()
                        movie.set_display(surface)
                        movie.play()
                        movie.set_volume(0)
                        frames = 0
                    elif movie2Playing:
                        pygame.quit()    #<----- Here

        if movie.get_busy() == 0:
            if movie1Playing == True:
                movie.stop()
                movie1Playing = False
                movie2Playing = True
                movie = pygame.movie.Movie('./intromovie2.mpg')
                movie = pygame.movie.Movie('./intromovie2.mpg')
                surface = pygame.Surface(movie.get_size()).convert()
                movie.set_display(surface)
                movie.play()
                movie.set_volume(0.5)
                frames = 0
            else:
                movie.stop()
                movie1Playing = True
                movie2Playing = False
                movie = pygame.movie.Movie('./intromovie1.mpg')
                movie = pygame.movie.Movie('./intromovie1.mpg')
                surface = pygame.Surface(movie.get_size()).convert()
                movie.set_display(surface)
                movie.play()
                movie.set_volume(0.5)
                frames = 0

        if playedEarly:
            earlyCounter += 1
            if earlyCounter > 67:
                movie.set_volume(0.5)
                playedEarly = False
                earlyCounter = 0
        frames += 1

        screen.blit(surface,(0,0))
        pygame.display.update()
        clock.tick(FPS)



def mainMenu(screen, surface):
    pass

def main(screen, surface):
    currentGamestate = introsequence
    pygame.mixer.quit()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()         #<---- and here

        if currentGamestate == introsequence:
            introsequence(screen, surface)
        else:
            pygame.mixer.init()
            if currentGamestate == mainmenu:
                mainMenu(screen, surface)

if __name__ == "__main__":
    main(screen, surface)

希望这可以帮助一些人!快乐的编码!