CS21 Lab5: Functions

Due 11:59pm Tuesday October 4

Run update21 to create the cs21/labs/05. 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
There is a file in the cs21/labs/05 directory, busyBugs.py, that you can use as a starting point for your solution.

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, you must follow the guidelines provided for the input parameters and the return value for full credit.

Create a bug

Open a file named busyBugs.py and write a function createABug(center, radius, window).

createABug(center, radius, window) creates and then draws an bug in the given window at a specific location (center) with a specific scale (radius). Your bug must be comprised of only circles, but how many circles, their color, and their relative position is up to you.

The createABug 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.
  3. window: A GraphWin object.
The createABug 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 you can use the clone method to make a copy of the original shape, and then move it to the desired location.

Once you have your createABug function implemented, test it by making calls to it from the main function; create several different sample bug by making calls to your createABug function, passing in different values for the center and radius to test drawing to different locations and bugs of different scales. Here are a couple examples:

Here is another example of bugs made of all circles:

Make a bug Crawl

Write a function crawlBug(bug, dx, dy) that moves a bug a specified amount. The crawlBug 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 crawlBug function should not return anything.

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

Make a bug Spin

Write a function spinBug(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 spinBug are:

  1. bug: A list of Circle objects describing a bug
  2. degrees: The number of degrees to rotate the bug
The spinBug 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). Using the move formula given below, you will rotate the entire bug (all circle objects in your list) around the center point of the main circle.

First, get the main circle's center point (xc, yc), using the getX and getY methods. Next, get the body-part-to-be-rotated's center point (x, y).

Now calculate dx and dy using:

dx = x - xc 
dy = y - yc
Finally, 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 should 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)

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, after your draw bugs, call this function to change a bug's orientation in the graphics window.



Animate Bugs

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

Your main should satisfy the following requirements:



Submit

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