Skip to main content
Engineering LibreTexts

9.23: Moving the Player, and Accounting for Bounce

  • Page ID
    14642
  •         if not gameOverMode:
                # actually move the player
                if moveLeft:
                    playerObj['x'] -= MOVERATE
                if moveRight:
                    playerObj['x'] += MOVERATE
                if moveUp:
                    playerObj['y'] -= MOVERATE
                if moveDown:
                    playerObj['y'] += MOVERATE
    
                if (moveLeft or moveRight or moveUp or moveDown) or playerObj['bounce'] != 0:
                    playerObj['bounce'] += 1
    
                if playerObj['bounce'] > BOUNCERATE:
                    playerObj['bounce'] = 0 # reset bounce amount
    

    The code inside the if statement on line 1 [255] will move the player’s squirrel around only if the game is not over. (This is why pressing on the arrow keys after the player’s squirrel dies will have no effect.) Depending on which of the move variables is set to True, the playerObj dictionary should have its playerObj['x'] and playerObj['y'] values changed by MOVERATE. (This is why a larger value in MOVERATE makes the squirrel move faster.)

    The value in playerObj['bounce'] keeps track of at what point in bouncing the player is at. This variable stores an integer value from 0 to BOUNCERATE. Just like the bounce value for the enemy squirrels, a playerObj['bounce'] value of 0 means the player squirrel is at the start of a bounce and a value of BOUNCERATE means the player squirrel is at the end of the bounce.

    The player squirrel will bounce whenever the player is moving, or if the player has stopped moving but the squirrel hasn’t finished its current bounce. This condition is captured in the if statement on line 12 [266]. If any of the move variables is set to True or the current playerObj['bounce'] is not 0 (which means the player is currently in a bounce), then the variable should be incremented on line 13 [267].

    Because the playerObj['bounce'] variable should only be in the range of 0 to BOUNCERATE, if incrementing it makes it larger than BOUNCERATE, it should be reset back to 0.