7.5: The main() Function

Making Games with Python and Pygame · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-NC-SA 3.0, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    7.5: The main() Function
    def main():
        global FPSCLOCK, DISPLAYSURF, BASICFONT
    
        pygame.init()
        FPSCLOCK = pygame.time.Clock()
        DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
        BASICFONT = pygame.font.Font('freesansbold.ttf', 18)
        pygame.display.set_caption('Wormy')
    
        showStartScreen()
        while True:
            runGame()
            showGameOverScreen()
    

    In the Wormy game program, we’ve put the main part of the code in a function called runGame(). This is because we only want to show the "start screen" (the animation with the rotating "Wormy" text) once when the program starts (by calling the showStartScreen() function). Then we want to call runGame(), which will start a game of Wormy. This function will return when the player’s worm collides into a wall or into itself and causes a game over.

    At that point we will show the game over screen by calling showGameOverScreen(). When that function call returns, the loop goes back to the start and calls runGame() again. The while loop on line 11 [44] will loop forever until the program terminates.


    This page titled 7.5: The main() Function was last modified on Mon, 18 May 2020 08:26:07 GMT and is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Al Sweigart via source content that was edited to the style and standards of the LibreTexts platform.