Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 标题屏幕未运行Pygame_Python_Pygame - Fatal编程技术网

Python 标题屏幕未运行Pygame

Python 标题屏幕未运行Pygame,python,pygame,Python,Pygame,我试图为我正在制作的游戏创建一个标题屏幕,但当我运行程序时,它只是一个黑色画布。没有标题,一切都很好,游戏(这是一个迷宫)运行完美,但如果我试图添加标题fcn,一切都会一团糟。 运行我的程序时,我正在呼叫: game_intro() game_loop() #the fcn that stores my game code 这是我的标题fcn代码: def game_intro(): intro = True while intro == True: for

我试图为我正在制作的游戏创建一个标题屏幕,但当我运行程序时,它只是一个黑色画布。没有标题,一切都很好,游戏(这是一个迷宫)运行完美,但如果我试图添加标题fcn,一切都会一团糟。 运行我的程序时,我正在呼叫:

game_intro()
game_loop() #the fcn that stores my game code 
这是我的标题fcn代码:

def game_intro():
    intro = True
    while intro == True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

    screen.fill((WHITE))
    largeText = pygame.font.Font(None, 115)
    TextSurf, TextRect = text_objects("H&D: Hedges and Dragons", largeText)
    TextRect.center = ((DISPLAY_WIDTH/2), (DISPLAY_HEIGHT/2))
    screen.blit(TextSurf, TextRect)
    pygame.display.update()
    clock.tick(FPS)

我感谢任何帮助或建议

您需要在while循环之前初始化pygame:

def game_intro():
    pygame.init()
    screen = pygame.display.set_mode((1000, 900))
    intro = True
    while intro: #do not need intro == True
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
               pygame.quit()
               quit()

    screen.fill((WHITE))
    largeText = pygame.font.Font(None, 115)
    TextSurf, TextRect = text_objects("H&D: Hedges and Dragons", largeText)
    TextRect.center = ((DISPLAY_WIDTH/2), (DISPLAY_HEIGHT/2))
    screen.blit(TextSurf, TextRect)
    pygame.display.update()
    clock.tick(FPS)

您需要在while循环之前初始化pygame:

def game_intro():
    pygame.init()
    screen = pygame.display.set_mode((1000, 900))
    intro = True
    while intro: #do not need intro == True
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
               pygame.quit()
               quit()

    screen.fill((WHITE))
    largeText = pygame.font.Font(None, 115)
    TextSurf, TextRect = text_objects("H&D: Hedges and Dragons", largeText)
    TextRect.center = ((DISPLAY_WIDTH/2), (DISPLAY_HEIGHT/2))
    screen.blit(TextSurf, TextRect)
    pygame.display.update()
    clock.tick(FPS)

您的
game\u intro
功能将在初始
循环检查退出事件时暂停。下面的代码永远不会运行--它应该缩进到与
for
循环相同的级别,以便在处理事件后运行。然后,您需要某种方法退出
while
循环,可能会在按键或其他事件中将
intro
设置为False。您的
game\u intro
功能将在初始
循环检查退出事件时暂停。下面的代码永远不会运行--它应该缩进到与
for
循环相同的级别,以便在处理事件后运行。然后,您需要某种方法退出
while
循环,可能在按键或其他事件中将
intro
设置为False。