This is the animated branch.
The hero has four additional attributes.
facing_rightis a boolean which isTruewhen the hero is facing right andFalseotherwise. When the hero is first initialized,facing_rightis set toTrue. Using the arrows to move right or left set this variable while playing.stepsis a variable that tracks how many iterations of the game loop have elapsed while the hero is walking.step_rateis the number of frames that elapse before incrementing thewalk_indexto advance to the next image.walk_indexis the index of the image in the walk list to show.
Two functions have been added to the hero.
step is called whenever the player is pressing either the right or left arrow key. This function increments the steps variable by one. Each time the steps variable reaches the step_ rate, it resets to zero and the walk_index is incremented. The walk_index resets to zero when equal to the length of the walk image list.
In the heros update function calls a new function set_image. set_image looks at the current direction and state of the hero (hurt, jumping/falling, idle, or walking) to set the appropriate image.
Enemies do not have a direction. Instead, they just cycle through all images in their images list. The step_rate is the number of frames that pass before the walk_index is incremented.