我在获得由def game_intro()定义的介绍屏幕以正确加载时遇到了问题。每当我运行它,它就卡在一个空白的,黑色的屏幕上。在我加入之前,游戏进行得很好。
我已经尝试过调试器,但没有能够成功地找出问题所在。我正在使用Python编写代码。问题代码如下:
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() 我想看一个白屏,上面写着“蛇”这个词。
发布于 2019-10-22 10:42:35
你的压痕很简单
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(),因为后者将导致视频系统中的一些错误(在事件循环中取消初始化游戏之后仍会调用pygame.display.update()一次,从而导致错误)。
发布于 2019-10-22 00:42:01
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 -= 1https://stackoverflow.com/questions/58495685
复制相似问题