Making with Code

Functions Lab | Part II #

In this lab, we will deepen our understanding with a series of design challenges.


[0] Set up #

πŸ’» Start by going into the unit folder.
cd ~/desktop/making_with_code/unit00_drawing/
πŸ’» Then clone the lab
git clone https://github.com/the-isf-academy/lab_functions2.git
πŸ’» Now that you have the lab, go into its folder.
cd lab_functions2

πŸ’» Enter the Poetry Shell to start the lab. As a reminder, we will run this command at the start of each lab, but only when we are inside a lab folder.

poetry shell
πŸ‘Ύ πŸ’¬ Exiting the poetry shell

When you want to exit the shell, you can type exit or ^D


πŸ’» Open the file:
code circle_patterns.py
Take a look at the code inside
from turtle import *

def filled_circle(circle_size,circle_color):
    fillcolor(circle_color)
    begin_fill()
    circle(circle_size)
    end_fill()

[Helpful Turtle Functions] #

Here are some helpful Turtle functions that may make this problem easier. You are not required to use all of these functions.

FunctionInputExample UseExplanation
forwardamountforward(100)Moves the turtle forward by the specified amount
backwardamountbackward(100)Moves the turtle backward by the specified amount
rightangle in degreesright(45)Turns the turtle clockwise by the specified angle
leftangle in degressleft(45)Turns the turtle counter clockwise by the specified angle
colorcolornamecolor('red')Sets the color for drawing. Use “red”, “black”, etc. Here’s a list of all the colors.
speednumber from 0-10speed(0)Determines the speed at which the turtle moves around the window. 1 for slowest, 3 for normal speed, 10 for fast, 0 for fastest.
pendownNonependown()Puts down the turtle/pen so that it draws when it moves
penupNonepenup()Picks up the turtle/pen so that it doesn’t draw when it moves
pensizewidthpensize(4)Sets the width of the pen for drawing
setheadinganglesetheading(90)Sets the pen to the 0th degree
circlesizecircle(10)Sets the radius of the circle
gotox, ygoto(90,0)Moves turtle to a given coordinate
begin_fillNonebegin_fill()Marks the start of the color fill
end_fillNoneend_fill()Marks the end of the color fill
fillcolorcolornamefillcolor('purple')Sets the color of the fill
pencolorcolornamepencolor('purple')Sets the color of the pen
bgcolorcolornamebgcolor()Changes the background of the turtle screen
colormodecolornamecolormode(255)Changes the turtle color to accept rgb colors. e.g. color(255,15,23)

[1] Circle Patterns #

This mini lab is designed to be a pick and choose adventure. The patterns below are in order of difficulty. It is up to you to choose patterns that are at an appropriate level of difficulty for you.

Each of the patterns rely on the filled_circle() function provided in the starter code.

πŸ‘Ύ πŸ’¬ Commenting Code

It can be incredibly useful to use comment code! Comments are useful for

  • leaving yourself notes
  • leaving others notes
  • isolating testing
# this is a comment
# print('hello')

In visual studio code, you can select one or multiple lines of code and press ⌘+? to comment or uncomment code.


Alternating Pattern #

πŸ’» Code a function alternating_pattern(num_circles)

Example function call: alternating_pattern(5)

  • It takes num_circles as a parameter - this will change the amount of circles in the pattern
  • Hard code the 2 colors to alternate between
  • Hint: look at the conditionals lab

For an additional challenge:

  • add num_circle_size as a parameter - this will change the size of the circles in the pattern
    • Example function call: alternating_pattern(5,200)

Bullseye #

πŸ’» Code a function bullseye_pattern(num_circles, starting_circle_size)

Example function call: bullseye_pattern(5, 200)

  • It takes num_circles and starting_circle_size as parameters - the number of circles and the size of the bullseye should adjust accordingly
  • Hard code the 2 colors to alternate between

For an additional challenge:

  • add color1 and color2 as parameters - this will change the colors of the pattern.
    • Example function call: bullseye_pattern(5, 200, 'coral', 'light blue')

Size Pattern #

πŸ’» Code a function size_pattern(num_columns, num_rows, color)

Example function call: size_pattern(6,5,'coral')

  • It takes num_columns, num_rows, and color as parameters

Half-tone Pattern #

πŸ’» Code a function halftone_pattern(num_columns, num_rows, color1, color2)

Example function call: halftone_pattern(6,5,'dark blue','light blue')

  • It takes num_columns, num_rows, color1, and color2 as parameters

Random Pattern #

πŸ’» First, code a function thick_circle(circle_size, circle_color, thickness)

  • It should draw a circle of any color, size, and any pen thickness

πŸ’» Second, code a function random_pattern(num_circles, background_color)

Example function call: random_pattern(20,'black')

  • It should create a random pattern of thick circles and filled circles
  • The circles should all be of random colors and sizes.

This will require you to:

  • use the randint() function from the random library
    • add from random import randint to the top of your file
  • change the color mode to accept rgb colors
    • add colormode(255) to the start of the function

[2] Deliverables #

⚑✨

β˜‘οΈ At then end of class, be sure to fill out this Google form.

✏️ Add a screenshot of a code snippet to your CS9 Lab Code Log in your Google Drive. Add a comment with a key takeaway, a question about your code, or describing a piece of code you are proud of.