Bill Manaris : Fall 2007 / CSCI 220 Homework 3

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)

Learning Objective

Assignment

Write a program to animate a ball (well, a circle) bouncing around in a window. (Similar to exercise 7.17 in book, p. 230)

Step 1

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 time library.

from time import sleep
...
sleep(0.005)  # this pauses the program for 5 thousandths of a sec
 

Step 2

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)
 

Step 3

Make the ball bounce back when its circumference hits one of the edges.

Bonus

When the mouse is clicked change the direction of the ball randomly.

Hint: Assuming your window is stored in variable win, variables win.mouseX and 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.

Submission

Email your 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 manaris@cs.cofc.edu.

Policies

The following policies are in effect for this assignment:

Documentation

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>
# <ProgramName>.py
#
# Problem: <Brief, one or two sentence description of the
#           problem that this program solves, in your own
#           words.>
#
# 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)>
#
 
(Printable View of http://www.cs.cofc.edu/~manaris/?n=Fall2007.CSCI220Homework3)