Skip to main content
Engineering LibreTexts

8.23: showTextScreen(), A Generic Text Screen Function

  • Page ID
    14599
  • def showTextScreen(text):
        # This function displays large text in the
        # center of the screen until a key is pressed.
        # Draw the text drop shadow
        titleSurf, titleRect = makeTextObjs(text, BIGFONT, TEXTSHADOWCOLOR)
        titleRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 2))
        DISPLAYSURF.blit(titleSurf, titleRect)
    
        # Draw the text
        titleSurf, titleRect = makeTextObjs(text, BIGFONT, TEXTCOLOR)
        titleRect.center = (int(WINDOWWIDTH / 2) - 3, int(WINDOWHEIGHT / 2) - 3)
        DISPLAYSURF.blit(titleSurf, titleRect)
    
        # Draw the additional "Press a key to play." text.
        pressKeySurf, pressKeyRect = makeTextObjs('Press a key to play.', BASICFONT, TEXTCOLOR)
        pressKeyRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 2) + 100)
        DISPLAYSURF.blit(pressKeySurf, pressKeyRect)
    
        while checkForKeyPress() == None:
            pygame.display.update()
            FPSCLOCK.tick()
    

    Instead of separate functions for the start screen and game over screens, we will create one generic function named showTextScreen(). The showTextScreen() function will draw whatever text we pass for the text parameter. Also, the text "Press a key to play." will be displayed in addition.

    Notice that lines 5 [328] to 7 [330] draw the text in a darker shadow color first, and then lines 10 [333] to 12 [335] draw the same text again, except offset by 3 pixels to the left and 3 pixels upward. This creates a "drop shadow" effect that makes the text look a bit prettier. You can compare the difference by commenting out lines 5 [328] to 7 [330] to see the text without a drop shadow.

    The showTextScreen() will be used for the start screen, the game over screen, and also for a pause screen. (The pause screen is explained later in this chapter.)

    We want the text to stay on the screen until the user presses a key. This small loop will constantly call pygame.display.update() and FPSCLOCK.tick() until checkForKeyPress() returns a value other than None. This happens when the user presses a key.