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.
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:
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.
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.
Write a function moveBug(bug, dx, dy) that moves a bug a specified amount. The moveBug function has the following parameters:
Once you have written this function, add some code to main to test your new function.
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:
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.)
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:
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.)
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.
Once you are satisfied with your programs, hand them in by typing handin21 in a terminal window.