This lab consists of two parts: a written portion due in class Monday, February 21st, and the programming portion described below due Tuesday night as usual.
The goal of this lab is to practice creating and using functions. You will use the graphics library to construct a program that creates, jumps, and spins aliens. For each function, follow the guidelines provided for the input parameters and the return value. Once you have these functions working you will create an animation using several aliens of different sizes.
A skeleton version of the program aliens.py will appear
in your cs21/lab/05/ directory when you run update21
in a terminal window. Notice that this starting point file contains
comments explaining the inputs, outputs, and
purpose of each function you will need to
write.
Open the file aliens.py and write a function createAlien(window, center, radius) that creates and then draws an alien in the given window at a specific location with a specific scale. Your alien must be comprised of only circles, but how many circles, their color, and their relative position is up to you. The createAlien function has the following parameters:
In the main function, create several different sample aliens
using your createAlien function. They should be created at
different locations with different scales, such as in the example
below. Remember your aliens need not look like this. Be creative,
and design your own alien.
Write a function jumpAlien(alien, dx, dy) that jumps an alien the specified amounts in the x and y directions. Note: this jump is not animated, and the jump function should not use sleep().The jumpAlien function has the following parameters:
Once you have written this function, update main to test your new function by jumping your sample aliens to new locations.
Write a function spinAlien(alien, degrees) that spins an alien a specified number of degrees about its center. NOTE: this spin is not animated, and the spin function should not sleep(). This function should simply rotate all of the features of the alien a fixed angle. The parameters for spinAlien are:
The alien should be rotated around the center of the alien's main circle, the first Circle object in the list alien (at index 0). To do this you will need to move each alien part a different distance, based on how far that alien part is from the alien's main circle. For example, given an alien part part and the alien's main circle mainCircle, we can first compute some initial distances between them as:
distX = part.getCenter().getX() - mainCircle.getCenter().getX() distY = part.getCenter().getY() - mainCircle.getCenter().getY()Given these initial distances and an angle rad (in radians), we can rotate the alien part by rad by moving it by the distances dx and dy given by:
dx = distX * (cos(rad)-1) - distY * sin(rad) dy = distX * sin(rad) + distY * (cos(rad)-1)
You will need to import the python math functions sin and cos. Because sin and cos expect the angle to be in radians, you might also want to import the python math function radians to convert the angle from degrees to radians.
Once you have completed this function, test it in main. For example, you should now be able to have aliens at different orientations, as shown below.
Once all of the functions described above are working correctly, use them to create an interesting animation in main. Feel free to write additional functions as needed. Your main should satisfy the following requirements:
Once you are satisfied with your program, hand it in by typing handin21 in a terminal window.