Skip to main content
Engineering LibreTexts

9.22: The Event Handling Loop

  • Page ID
    14641
  •         for event in pygame.event.get(): # event handling loop
                if event.type == QUIT:
                    terminate()
    
                elif event.type == KEYDOWN:
                    if event.key in (K_UP, K_w):
                        moveDown = False
                        moveUp = True
                    elif event.key in (K_DOWN, K_s):
                        moveUp = False
                        moveDown = True
                    elif event.key in (K_LEFT, K_a):
                        moveRight = False
                        moveLeft = True
                        if playerObj['facing'] != LEFT: # change player image
                            playerObj['surface'] = pygame.transform.scale(L_SQUIR_IMG, (playerObj['size'], playerObj['size']))
                        playerObj['facing'] = LEFT
                    elif event.key in (K_RIGHT, K_d):
                        moveLeft = False
                        moveRight = True
                        if playerObj['facing'] != RIGHT: # change player image
                            playerObj['surface'] = pygame.transform.scale(R_SQUIR_IMG, (playerObj['size'], playerObj['size']))
                        playerObj['facing'] = RIGHT
                    elif winMode and event.key == K_r:
                        return
    
                elif event.type == KEYUP:
                    # stop moving the player's squirrel
                    if event.key in (K_LEFT, K_a):
                        moveLeft = False
                    elif event.key in (K_RIGHT, K_d):
                        moveRight = False
                    elif event.key in (K_UP, K_w):
                        moveUp = False
                    elif event.key in (K_DOWN, K_s):
                        moveDown = False
    
                    elif event.key == K_ESCAPE:
                        terminate()
    

    The first thing that is checked in the event handling loop is if the QUIT event has been generated. If so, then the program should be terminated.

    If the up or down arrow keys have been pressed (or their WASD equivalents), then the move variable (moveRight, moveDown, etc.) for that direction should be set to True and the move variable for the opposite direction should be set to False.

    The moveLeft and moveRight variables should also be set when the left or right arrow keys are pressed. Also, the value in playerObj['facing'] should be updated to either LEFT or RIGHT. If the player squirrel is now facing a new direction, the playerObj['surface'] value should be replaced with a correctly scaled image of the squirrel facing the new direction.

    Line 15 [229] is run if the left arrow key was pressed and checks if the player squirrel was facing right. If that was so, then a new scaled Surface object of the player squirrel image is stored in playerObj['surface']. The code in line 18's [232] elif statement handles the opposite case.

    If the player has won the game by growing large enough (in which case, winMode will be set to True) and the R key has been pressed, then runGame() should return. This will end the current game, and a new game will start the next time that runGame() gets called.

    If the player lets up on any of the arrow or WASD keys, then the code should set the move variable for that direction to False. This will stop the squirrel from moving in that direction any more.

    If the key that was pressed was the Esc key, then terminate the program.