Reputation: 41
I'm having trouble getting my intro screen, defined by def game_intro(), to load properly. Whenever I run it, it just gets stuck on a blank, black screen. Before I added it, the game worked fine.
I've tried the debugger, but have not been able to successfully figure out what's wrong. I'm using the Python IDE to write the code. The problem code is below:
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()
I want to see a white screen with the word "Snake" written on it.
Upvotes: 3
Views: 74
Reputation: 354
Your indentation is simply off
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()
Just indent the rest of your game loop properly under the while loop and it works.
Also, you didn't define white
anywhere, but for simple colors you can just use the pygame.Color
class
In addition, I changed the breaking condition of the loop to acutally use your intro
variable instead of pygame.quit()
, because the latter will result in some errors with the video system (the pygame.display.update()
is still called once after de-initialising pygame in the event loop, resulting in an error).
Upvotes: 1
Reputation: 881623
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
I'm pretty certain that loop will run forever, or at least until you exit. The pygame.event.get()
call retrieves a list of events but the only way out of that loop is if you get a QUIT
one.
Hence it will never get to your code that actually does the intro.
You probably want something like (Pythonic, but actually pseudo-code):
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
Upvotes: 1