6.5: The main() Function
- Page ID
- 13594
def main(): global FPSCLOCK, DISPLAYSURF, BASICFONT, BEEP1, BEEP2, BEEP3, BEEP4 pygame.init() FPSCLOCK = pygame.time.Clock() DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT)) pygame.display.set_caption('Simulate') BASICFONT = pygame.font.Font('freesansbold.ttf', 16) infoSurf = BASICFONT.render('Match the pattern by clicking on the button or using the Q, W, A, S keys.', 1, DARKGRAY) infoRect = infoSurf.get_rect() infoRect.topleft = (10, WINDOWHEIGHT - 25) # load the sound files BEEP1 = pygame.mixer.Sound('beep1.ogg') BEEP2 = pygame.mixer.Sound('beep2.ogg') BEEP3 = pygame.mixer.Sound('beep3.ogg') BEEP4 = pygame.mixer.Sound('beep4.ogg')
The main()
function will implement the bulk of the program and call the other functions as they are needed. The usual Pygame setup functions are called to initialize the library, create a Clock object, create a window, set the caption, and create a Font object that will be used to display the score and the instructions on the window. The objects that are created by these function calls will be stored in global variables so that they can be used in other functions. But they are basically constants since the value in them is never changed.
Lines 15 [55] to 18 [58] will load sound files so that Simulate can play sound effects as the player clicks on each button. The pygame.mixer.Sound()
constructor function will return a Sound object, which we store in the variables BEEP1
to BEEP4
which were made into global variables on line 2 [42].