标题屏幕未运行Pygam

2024-03-28 11:33:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图为我正在制作的游戏创建一个标题屏幕,但当我运行程序时,它只是一个黑色的画布。没有标题,一切正常,游戏(这是一个迷宫)运行完美,但如果我试图添加我的标题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)

我感谢任何帮助或建议!你知道吗


Tags: 程序eventgametrue游戏标题displayscreen
1条回答
网友
1楼 · 发布于 2024-03-28 11:33:52

您需要在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)

相关问题 更多 >