Skip to main content
Engineering LibreTexts

9.34: Flipping the Squirrel Image

  • Page ID
    14655
  •     if sq['movex'] < 0: # squirrel is facing left
            sq['surface'] = pygame.transform.scale(L_SQUIR_IMG, (sq['width'], sq['height']))
        else: # squirrel is facing right
            sq['surface'] = pygame.transform.scale(R_SQUIR_IMG, (sq['width'], sq['height']))
        sq['bounce'] = 0
        sq['bouncerate'] = random.randint(10, 18)
        sq['bounceheight'] = random.randint(10, 50)
        return sq
    

    The L_SQUIR_IMG and R_SQUIR_IMG constants contain Surface objects with left-facing and right-facing squirrel images on them. New Surface objects will be made using the pygame.transform.scale() function to match the squirrel’s width and height (stored in sq['width'] and sq['height'] respectively).

    After that, the three bounce-related values are randomly generated (except for sq['bounce'] which is 0 because the squirrel always starts at the beginning of the bounce) and the dictionary is returned on line 8 [372].