CS21 Lab 5: Functions

Due 11:59pm Tuesday night, Oct. 7

Run update21, if you haven't already, to create the cs21/labs/05 directory. Then cd into your cs21/labs/05 directory and create the python programs for lab 5 in this directory (handin21 looks for your lab 5 assignments in your cs21/labs/05 directory):


$ update21
$ cd cs21/labs/05
$ vi busyBugs.py

Your programs are graded on both correctness and style. Please review the comments regarding programming style on the main page.

The goal of this assignment is to practice creating and using functions. You will use the graphics library to construct a program that builds, draws, moves, animates, and rotates bugs. For each function, follow the guidelines provided for the input parameters and the return value.

1. Build a bug

Open the file busyBugs.py and write a function buildBug(center, radius) that creates a bug at a specific location with a specific scale. Your bug must be comprised of only circles, but how many circles, their color, and their relative position is up to you. The buildBug function has the following parameters:

  1. center: A Point object indicating the location in the window for the center of the bug
  2. radius: The radius of the largest circle in the bug. All other circles in the bug should be scaled in proportion to this radius. Thus a bigger radius will draw a bigger bug.
The buildBug function should return a list of Circle objects that describe the bug. The first Circle object in the list (at index 0) should be centered at center.

For symmetrical features (such as eyes or ears), remember to use the clone method to make a copy of the original shape, then move it to the desired location.

2. Draw a bug
Write a function drawBug(bug, window) that draws a bug in a specific window. The drawBug function has the following parameters:
  1. bug: A list of Circle objects describing a bug
  2. window: A GraphWin object in which to draw the bug
The drawBug function should not return anything.

Once you have finished writing drawBug, edit the main function to test your buildBug and drawBug functions by building and drawing a few bugs in a graphics window. You may want to use the setCoords method of the GraphWin object to adjust the coordinates of the window. Note at this point, you should be able to draw bugs at different scales, as shown in the example below.


3. Move a bug

Write a function moveBug(bug, dx, dy) that moves a bug a specified amount. The moveBug function has the following parameters:

  1. bug: A list of Circle objects describing a bug
  2. dx: The distance to move in the x-direction
  3. dy: The distance to move in the y-direction
The moveBug function should not return anything.

Once you have written this function, add some code to main to test your new function.

4. Animate a bug

Write a function animateBug(bug, dx, dy, steps, wait) that moves a bug a specified amount for a specified number of steps. The animateBug function has the following parameters:

  1. bug: A list of Circle objects describing a bug
  2. dx: The distance to move in the x-direction in one step
  3. dy: The distance to move in the y-direction in one step
  4. steps: The number of total steps to take
  5. wait: The time in seconds to wait between steps
The animateBug function does not return anything.

Your solution for animateBug should use the moveBug function that you already wrote.

Once you have written this function, add some code to main to test your new function. Also test that you can move at least two bugs at the same time.

(For an extension to the animateBug function, click here.)

5. Rotate a bug

Write a function rotateBug(bug, degrees) that rotates a bug a specified number of degrees about its center. NOTE: this rotation is not animated. This function should simply rotate all the features of the bug a fixed angle. The parameters for rotateBug are:

  1. bug: A list of Circle objects describing a bug
  2. degrees: The number of degrees to rotate the bug
The rotateBug function should not return anything.

The bug should be rotated around the center of the first Circle object in the list bug (at index 0). To rotate one of the bug's parts with a center (x,y) by an angle t around the main center point (xc, yc), you must move the part's center point (x,y) by an amount mx, my defined as folows:

mx = dx * (cos(t)-1)  -  dy * sin(t) 
my = dx * sin(t)      +  dy * (cos(t)-1)
where
dx = x - xc 
dy = y - yc

The python math functions sin and cos expect the angle to be in radians. You can use the python math function radians to convert the angle from degrees to radians.

Once you have completed this function, test it in your main function. For example, you should now be able to draw bugs at different orientations, as shown below.

(For an extension to the rotateBug function, click here.)


Putting it all together

Once all of the required functions are working correctly, create an interesting animation with your bugs in the main function. Feel free to write additional functions as needed.



Submit

Once you are satisfied with your programs, hand them in by typing handin21 in a terminal window.

Extensions
Here are two extensions that you might consider adding to your program:
  1. speedBugs(bug, dx, dy, speed): Write a new function to animate bugs that allows bugs to move at a specific speed (measured in coordinates per second). Assuming that you animate 30 steps per second, calculate the correct dx and dy to move each step, then animate the bug. Be sure to test that your bug correctly moves to the specified position after you have animated it.
  2. rotatePart(part, center, degrees): Modify your rotateBug function so that it first computes the center of the bug, then for each part of the bug, calls rotatePart which does the actual rotation of each bug part.