194 Chapter 8
Changing direction
“Bouncing” off the edge of the screen means going in the oppo-
site direction after hitting that edge. The direction our image is
moving is controlled by the updates to picx and picy. In our old
SmileyMove.py, we just added 1 pixel to picx and picy every time
through the while loop with these lines:
picx += 1
picy += 1
However, these lines kept our image moving right and down
1 pixel every time; there was no “bounce,” or changing direction,
because we never changed the number added to picx and picy.
Those two lines mean we’re guaranteed to move right and down
at a speed of 1 pixel per frame, every frame, even after the smiley
has left the screen.
Instead, we can change the constant value 1 to a variable that
will represent the speed, or number of pixels the image should
move each frame. Speed is the amount of movement in a period of
time. For example, a car that moves a lot in a short time is moving
at a high speed. A snail that barely moves in the same period of
time is moving at a low speed. We can define a variable called speed
in the setup portion of our program for the amount of movement in
pixels that we want for each frame:
speed = 5