CS21 Lab5: Functions

Due 11:59pm Tuesday October 4

Run update21 to create the cs21/labs/05 directory. Then cd into your cs21/labs/05 directory and create the python program 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. Note: this lab is to be completed in one file, busyBugs.py. Please complete all subtasks listed below as a part of a larger program. It is advised you read through the whole lab document before starting, and don't forget that you can run handin21 multiple times to turn in the portion of the program you have completed at any point in time.

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 bugs 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 two examples (be creative and have fun making your own bugs!):

Make a bug Crawl

Write a function crawlBug(bug, dx, dy) that moves a bug a specified amount (i.e., moves all the circles in the list that make up the bug). 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). This is the main circle. Using the move formula given blow, you will rotate the entire bug (all circles in your list) around the center point of the main circle.