简介屏幕未加载

时间:2019-10-22 00:33:50

标签: python pygame

我无法正常加载由def game_intro()定义的介绍屏幕。每当我运行它时,它只会卡在空白的黑屏上。在我添加游戏之前,游戏运行良好。

我已经尝试过调试器,但是无法成功找出问题所在。我正在使用Python IDE编写代码。问题代码如下:

import pygame
import time

pygame.init()

scrWdt = 500 
scrHgt = 500

win = pygame.display.set_mode((scrWdt,scrHgt))
pygame.display.set_caption("Snake")

clock = pygame.time.Clock()
black = (0, 0, 0)

def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

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

    win.fill(white)
    largeText = pygame.font.Font('freesansbold.ttf',115)
    TextSurf, TextRect = text_objects("Snake", largeText)
    TextRect.center = ((scrWdt/2),(scrHgt/2))
    win.blit(TextSurf, TextRect)
    pygame.display.update()
    clock.tick(100)

game_intro() 

我想看到一个白色的屏幕,上面写着“蛇”一词。

2 个答案:

答案 0 :(得分:1)

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

我很确定该循环将永远运行 ,或者至少直到您退出。 pygame.event.get()调用检索事件列表,但是该循环 的唯一方法是获得QUIT一个事件。

因此,它将永远不会到达实际进行介绍的代码。

您可能想要类似的东西(Pythonic,但实际上是 伪代码):

def intro():
    displayIntroScreen()

    loopCount = 10 # For a 1-second intro
    while loopCount > 0:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
        sleep(100ms)
        loopCount -= 1

答案 1 :(得分:1)

您的缩进完全没有了

def game_intro():
    intro = True
    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                intro = False
    # ---> 
        win.fill(pygame.Color('white'))
        largeText = pygame.font.Font('freesansbold.ttf',115)
        TextSurf, TextRect = text_objects("Snake", largeText)
        TextRect.center = ((scrWdt/2),(scrHgt/2))
        win.blit(TextSurf, TextRect)
        pygame.display.update()
        clock.tick(100)

game_intro() 
pygame.quit()

只要在while循环下适当缩进游戏循环的其余部分即可,并且可以正常工作。 另外,您没有在任何地方定义white,但是对于简单的颜色,您可以使用pygame.Color

此外,我更改了循环的中断条件,以手动使用intro变量而不是pygame.quit(),因为后者会导致视频系统出现一些错误({{1} }在事件循环中取消初始化pygame之后仍然被调用一次,从而导致错误)。