Skip to main content
Engineering LibreTexts

7.5: The main() Function

  • Page ID
    13601
  • 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.