CS21B Lab5: Functions

Due 11:59pm Tuesday night, February 23

The goal of this assignment 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. The program handin21 will only submit files in this directory.

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.
  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.
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.

2. Jump an alien

Write a function jumpAlien(alien, dx, dy) that jumps an alien the specified amounts in the x and y directions. 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. 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 first Circle object in the list alien (at index 0). To rotate one of the alien'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 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.