# move all the squirrels for sObj in squirrelObjs: # move the squirrel, and adjust for their bounce sObj['x'] += sObj['movex'] sObj['y'] += sObj['movey'] sObj['bounce'] += 1 if sObj['bounce'] > sObj['bouncerate']: sObj['bounce'] = 0 # reset bounce amount # random chance they change direction if random.randint(0, 99) < DIRCHANGEFREQ: sObj['movex'] = getRandomVelocity() sObj['movey'] = getRandomVelocity() if sObj['movex'] > 0: # faces right sObj['surface'] = pygame.transform.scale(R_SQUIR_IMG, (sObj['width'], sObj['height'])) else: # faces left sObj['surface'] = pygame.transform.scale(L_SQUIR_IMG, (sObj['width'], sObj['height']))
The enemy squirrels all move according to the values in their
'movey' keys. If these values are positive, the squirrels move right or down. If these values are negative, they move left or up. The larger the value, the farther they move on each iteration through the game loop (which means they move faster).
for loop on line 2  will apply this moving code to each of the enemy squirrel objects in the
squirrelObjs list. First, line 4  and 5  will adjust their
'y' keys’ values.
The value in
sObj['bounce'] is incremented on each iteration of the game loop for each squirrel. When this value is
0, the squirrel is at the very beginning of its bounce. When this value is equal to the value in
sObj['bouncerate'] the value is at its end. (This is why a smaller
sObj['bouncerate'] value makes for a faster bounce. If
3, then it only takes three iterations through the game loop for the squirrel to do a full bounce. If
10, then it would take ten iterations.)
sObj['bounce'] gets larger than
sObj['bouncerate'], then it needs to be reset to
0. This is what lines 7  and 8  do.
There is a 2% chance on each iteration through the game loop that the squirrel will randomly change speed and direction. On line 11  the
random.randint(0, 99) call randomly selects an integer out of 100 possible integers. If this number is less than
DIRCHANGEFREQ (which we set to
2 on line 33) then a new value will be set for
Because this means the squirrel might have changed direction, the Surface object in
sObj['surface'] should be replaced by a new one that is properly facing left or right and scaled to the squirrel’s size. This is what lines 14  to 17  determine. Note that line 15  gets a Surface object scaled from
R_SQUIR_IMG and line 17  gets one scaled from