Teach Your Kids To Code: A Parent-friendly Guide to Python Programming

(vip2019) #1
Timers and Animation: What Would Disney Do? 191

Figure 8-6: Our goal is to keep the smiley “bouncing”
between the corners of the screen.


The reason our image ran off the screen before is that we
didn’t set boundaries, or limits, for our animation. Everything we
draw on the screen is virtual—meaning it doesn’t exist in the real
world—so things don’t really bump into one another. If we want
the virtual objects on our screen to interact, we have to create
those interactions with programming logic.


h tting the Walli


When I say that we want the smiley face to “bounce” off the edge
of the screen, what I mean is that when the smiley comes to the
edge of the screen, we want to change the direction it’s moving so
that it looks like it bounces off the solid edge of the screen. To do
this, we need to test whether the (picx,picy) location of the smiley
has reached the imaginary boundary at the edge of the screen. We
call this logic collision detection because we’re trying to detect, or
notice, when a collision occurs, like the smiley face image “hitting”
the edge of the drawing window.
We know that we can test for conditions using an if statement,
so we could see if our image is touching, or colliding with, the
right side of the screen by checking whether picx is greater than
some value.

Free download pdf