Assigned Date: Monday, Oct. 8, 2007
Due Dates: Wednesday, Oct. 17, 2007
Due Time: 11:55pm
Last modified on November 09, 2007, at 09:58 AM (see updates)
- Build an interactive animation
Write a program to animate a ball (well, a circle) bouncing around in a window. (Similar to exercise 7.17 in book, p. 230)
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 large (it hits the edge), change dx to
-1. When it gets too small, 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 the
sleep() function from the
from time import sleep
sleep(0.005) # this pauses the program for 5 thousandths of a sec
Initialize dx and dy to random values (between -1 and 1). When the x-value of the center of the circle gets too large (it hits the edge), negate dx. When it gets too small, negate it again. Use a similar approach for dy.
from random import random
x = random() # assigns a random number between 0.0 and 1.0 (including 0.0, but excluding 1.0)
x = random() * 2 # assigns a random number between 0.0 and 2.0 (including 0.0, but excluding 2.0)
x = (random() * 2) - 1 # assigns a random number between -1.0 and 1.0 (including -1.0, but excluding 1.0)
Make the ball bounce back when its circumference hits one of the edges.
When the mouse is clicked change the direction of the ball randomly.
Hint: Assuming your window is stored in variable
win.mouseY contain the x-y coordinates of the mouse every time it gets clicked. Note that accessing these variables does not pause the program.
ball.py as an attachment. It is fine to submit a partial solution for partial credit. However, you need to state in your email how far you went (e.g., I completed up to step 3).
Email subject should be "Assignment #3".
My email address is firstname.lastname@example.org.
The following policies are in effect for this assignment:
- Programming assignment grades will be based on design and style as well as correctness of result.
- You may discuss the problem and how to solve it with others, but you may not look at, copy, or use any code (or pseudocode) that was written by anyone other than yourself. If I have evidence that you have shared program code or used code found anywhere, your grade will be zero.
- If you do discuss the problem and how to solve it with others, you must document that in the program code.
- Not following these rules is in violation of the Student Honor Code and instances of such violations will be reported to the Honor Board.
All identifiers should be meaningful.
Include your design (pseudocode) as comments in your program.
The following comments should appear in your program as the first lines in the file. Items in angle brackets are either to be removed or replaced with what is specified within the brackets:
# Name: <your name goes here – first and last minimum>
# Problem: <Brief, one or two sentence description of the
# problem that this program solves, in your own
# Certification of Authenticity:
# <include one of the following>
# I certify that this lab is entirely my own work.
# I certify that this lab is my own work, but I
# discussed it with: <Name(s)>