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 to get full credit.
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:
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.
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.
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 walkBug(bug, dx, dy, seconds) that moves a bug a specified amount in a specified time. The walkBug function has the following parameters:
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.
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 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.
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.
Once you are satisfied with your programs, hand them in by typing
handin21 in a terminal window.