CS21 Lab5: Functions

Due 11:59pm Tuesday October 6

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 to get full credit.

Build a bug

Edit a 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. An example bug is shown below.

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 draws 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. This function should NOT draw a bug. See the next function.

Here is another example of bugs made of all circles:

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.

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. Try drawing a few bugs of different sizes to different locations in the graphics window by making calls to buildBug with different argument values. You do not need to get input values from the user for the bugs' positions and sizes.

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.

Walk bug

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

  1. bug: A list of Circle objects describing a bug
  2. dx: The total distance to move in the x-direction
  3. dy: The total distance to move in the y-direction
  4. seconds: The total time the animation should last in seconds
To get a smooth walking animation, you should use 30 steps per second. Thus, if dx=100, dy=0 and seconds=5, then you should have an animation loop takes 5*30=150 steps and moves one 150th of the full dx value (100/150=0.667 units) on the x-axis at each step.

The walkBug function does not return anything.

Your solution for walkBug should make calls to 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". You can simulate this effect by moving one bug a little bit, then another, to make it look like the bugs are moving at the same time. You should not modify walkBug to move bugs at the same time. Rather, modify your main function to make multiple calls to walkBug in such a way as to create this effect. Remember you can use the sleep function after including from time import sleep to aid in animation.

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



Putting it all together

Once all of the required functions are working correctly, create an interesting animation with your bugs in the main function by calling your walkBug and rotateBug functions. 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.