Target
Create a Python program that defines the
function draw_target(center_x, center_y, radius)
.
The function should use
the graphics
module's draw_oval
function to draw a series of 3
concentric circles centered with alternating color at the
specified x and y location. The radius of the external circle
is specified by the radius parameter. The radius of
each inner circle decreases by 1/3 of the original
radius each time.
Example
graphics.window_size(320, 240) draw_target(160, 120, 100) graphics.wait_for_key_press()

Example
window_width = graphics.window_width() window_height = graphics.window_height() window_center_x = window_width / 2 window_center_y = window_height / 2 target_size = window_height // 10 max_x = window_width + target_size max_y = window_height + target_size for y in range(0, max_y, target_size): for x in range(0, max_x, target_size): draw_target(x, y, target_size / 2) graphics.wait_for_key_press()

X
Create a Python program that defines the draw_x(center_x,
center_y, size, thick)
. The function should use
the graphics
module's draw_line
function to draw an X centered
at the specified x and y location. The width and height of
the X determined by the size parameter and the
thickness of the lines specified by the thick
parameter.
Example
graphics.window_size(320, 240) draw_x(160, 120, 100, 5) graphics.wait_for_key_press()

Example
window_width = graphics.window_width() window_height = graphics.window_height() window_center_x = window_width / 2 window_center_y = window_height / 2 x_size = 100 x_thickness = x_size * (2 ** (1 / 2)) / 3 x_offset = x_size * 2 x_delta = (x_size * 3) + (x_thickness / 2 * (2 ** (1 / 2))) y_delta = x_size * 2 / 3 x_max = int(window_width / x_delta + x_delta) y_max = int(window_height / y_delta + y_delta) x_min = int(-x_delta) y_min = int(-y_delta) for i in range(0, y_max): for j in range(x_min, x_max): y = i * y_delta x = (j * x_delta) + ((i % 5) * x_offset) draw_x(x, y, x_size, x_thickness) graphics.wait_for_key_press()

Animate
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 that defines the function
move_image(image_name, start_x, end_x, y_location)
.
The function should 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 any gif image you want. Do
an image search for
something you like. Include filetype:gif
as one of
the search terms to only find gif images.
Hint
Hint
-
The
draw_image
function in the graphics module draws an image centered at a specified location. Your image needs to be stored in the same directory as your program. To achieve the motion required here, we just need to change the x coordinate the image every time it is drawn. - You will either need to use an accumulator, or use the loop variable as the updated value of the x coordinate for the drawn image.
Challenge
Modify your program so that you move not along a horizontal line, but you can move between any two arbitrary (x, y) coordinates.
Orrery
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 with the function
def animate_planet(orbit_radius, planet_radius, sun_radius, revolutions)
, which animates 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
graphics.window_size(320, 240) animate_planet(100, 10, 25, 3)
Hint
Hint
Create a for loop that iterates over the angular
position of the planet. For each iteration, use the provided
equations to convert the angle to x and y coordinates. Don't
forget to clear
the screen and wait
to slow the animation.
The only mathematical issue you may run into is the fact that
the trigonometric functions expect their input in radians
instead of degrees. You can use the math.radians
function to convert from degrees to radians.
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.