Processing math: 100%
< Back

Lab 11: Functions

As usual, create a directory to hold today's files. All programs that you write today should be stored in this directory.

    $ cd ~/cs120/labs
    $ mkdir lab11 
    $ cd lab11 
  


Practice Problem

In a file called practice.py, create a function called draw_target(center_x, center_y, size). This function should use the graphics module's draw_oval function to draw a series of 3 concentric circles centered at the specified x and y location. The radius of the external circle is specified by the size parameter. The radius of each circle decreases by 1/3 the original radius each time.

Example


Linear Motion

Creating an animation on a computer screen is fairly comparable to the way that flip-book animations work. In a flip book, you draw your image in one location, and on the following page you draw your image in a new, updated location. The analogue to flipping the page using the graphics module is clearing the screen. We then just need some mechanism to continually update the position of the image that we are drawing between screen clears.

Details

Create a python program in a file called floating.py. You should create a function called move_image(image_name, start_x, end_x, y_location), which will animate an image moving horizontally across the screen from the specified start_x location on the screen to the end_x location along the specified y_location.

Example

graphics.window_size(320, 240)
move_image("blinky.gif", 0, 340, 180)

You can use the image below that is used in the above example. Or you can use any gif image you want.

Hint

Challenge

Modify your program so that you move not along a horizontal line, but you can move between any two arbitrary (x, y) coordinates.

Example


Circular Motion

An Orrery is a mechanical model of the solar system that was first made in the 1700s. A sun was placed in the center of the model, and the orbits of the planets were fixed to a circle surrounding the star. Mimicking the motion that was created with this mechanical device requires using some trigonometry to compute the new x and y positions.

Details

Create a python program in a file called orrery.py. You should create a function called animate_planet(radius), which will animate a planet orbiting around a star in the center of the screen. To compute the position of the planet, use the following equations to convert an angle, in radians, to the x and y coordinates of a point on a circle centered on the origin:

x=radius×math.cos(radians)y=radius×math.sin(radians)

Example

Hint

Challenge

A real orrery doesn't just have one planet. It usually has many planets, all orbiting at different distances and speeds. Modify your program so that it can animate at least 2 separate planets, at different radii and at different speeds.

Example