CPSC150A
Scientific Computing

Activity 12

Functions

Target

Create a Python program that defines the function draw_target(center_x: float, center_y: float, radius: float) -> None. The function should use the turtle module to draw a series of 3 concentric circles centered with alternating color at the specified x and y location. The radius of each inner circle decreases by 1/3 of the original radius each time.

Example

Challenge

Use nested for loops and the draw_target function to create a pattern like the following:

X

Create a Python program that defines the draw_x(center_x: float, center_y: float, size: float, thick: float) -> None. The function should use the turtle module to draw an X centered at the specified x and y location. The width and height of the X is determined by the size parameter and the thickness of the lines is specified by the thick parameter.

Example

Challenge

Use a loop and the draw_x function to create a tessalated pattern like the following:

Pixel Art

Early computers had such a low resolution that you could see the the pixels, or squares, that made up the images. Modern computers are capabile of displaying smooth, detailed graphics. But blocky pixel art style remains popular. Creating retro pixelated visuals requires drawing filled-in squares. This is a great use for functions.

Details

Create a Python program that defines the function draw_square(center_x: float, center_y: float, size: float, color: float) -> float. The function should use turtle graphics to draw a square centered on the point (center_x, center_y). The square should be size wide and size tall. And finally it shold be filled with color.

A great benefit of functions is that present an opportunity to test an isolated portion of your program before it is completed. Take adantage of this to test the function before moving on.

Once you have tested the function and are satisfied that it works, use it to create a drawing of a checker board.

Example

  • The center_x and center_y parameters to the function specify the center of the square. You need to move the turtle half of the size in both the x and y directions to center the square before drawing.
  • Drawing the square is pretty easy, you just need to go forward and turn right 4 total times.
  • Use the fillcolor function to set the fill color, and the begin_fill and functions to actually fill the square.

Challenge

The checkerboard is easy to draw but not very fun. Use the function to create some better pixel art. Use a Google Image Search for some suggestions.