A simple decision can be implemented with an if statement.
Read moreTable of Contents
Textbook Solutions for Python Programming: An Introduction to Computer Science
Question
Write a program to animate a circle bouncing around a window. The basic idea is to start the circle somewhere in the interior of the window. Use variables dx and dy (both initialized to 1) to control the movement of the circle. Use a large counted loop (say 10000 iterations), and each time through the loop move the circle using dx and dy . When the x-value of the center of the circle gets too high (it hits the edge), change dx to -1. When it gets too low, change dx back to 1. Use a similar approach for dy . Note: Your animation will probably run too fast. You can slow it down by using update from the graphics library with a rate parameters. For example, this loop will be limited to going around at a rate of 30 times per second: for i in range ( 10000) : 241 update (30) # pause so rate is not more than 30 times a second
Solution
The first step in solving 7 problem number 40 trying to solve the problem we have to refer to the textbook question: Write a program to animate a circle bouncing around a window. The basic idea is to start the circle somewhere in the interior of the window. Use variables dx and dy (both initialized to 1) to control the movement of the circle. Use a large counted loop (say 10000 iterations), and each time through the loop move the circle using dx and dy . When the x-value of the center of the circle gets too high (it hits the edge), change dx to -1. When it gets too low, change dx back to 1. Use a similar approach for dy . Note: Your animation will probably run too fast. You can slow it down by using update from the graphics library with a rate parameters. For example, this loop will be limited to going around at a rate of 30 times per second: for i in range ( 10000) : 241 update (30) # pause so rate is not more than 30 times a second
From the textbook chapter Decision Structures you will find a few key concepts needed to solve this.
Visible to paid subscribers only
Step 3 of 7)Visible to paid subscribers only
full solution