CS21 Lab5: Aliens on the loose

Due 11:59pm Tuesday, February 22, 2011

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.

1. Create an alien

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:

  1. window: A GraphWin object.
  2. center: A Point object indicating the location in the window for the center of the alien. All other circles in the alien should be located relative to this center point.
  3. radius: The radius of the largest circle in the alien. All other circles in the alien should be scaled in proportion to this radius. Thus a bigger radius will draw a bigger alien.
After drawing the alien in the graphics window, the createAlien function should return a list of Circle objects that describe the alien. The first Circle object in the list (at index 0) should be centered at center.

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.

2. Jump an 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:

  1. alien: A list of Circle objects describing an alien.
  2. dx: The distance to jump in the x-direction.
  3. dy: The distance to jump in the y-direction.
The jumpAlien function should not return anything.

Once you have written this function, update main to test your new function by jumping your sample aliens to new locations.



3. Spin an alien

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:

  1. alien: A list of Circle objects describing an alien.
  2. degrees: The number of degrees to rotate the alien.
The spinAlien function should not return anything.

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.





4. Animate aliens

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:


Submit

Once you are satisfied with your program, hand it in by typing handin21 in a terminal window.