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