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/05There 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.
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 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!):
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:
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). 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.
dx = x - xc dy = y - yc
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 (e.g., t = radians(degrees)).
Once you have completed this function, test it in your main
function. For example, after you draw a bug, 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 function should satisfy the following requirements:
Don't forget the written part of the lab! Please fill out the
stack frame handout and turn
it in on Tuesdayduring class.
Once you are satisfied with your programs, hand them in by typing
handin21 in a terminal window.