Unit 2: Drawing Algorithms
In this unit students let the computer do the repetitive work. They use variables, loops, conditionals, lists, and the random module to draw patterns that would take hundreds of lines to write by hand.
Learning Objectives
Students will be able to:
Use variables and arithmetic to position shapes relative to each other.
Use
forloops withrange()to repeat a drawing step.Use nested loops to draw rows and grids.
Use
if,elif, andelseto make decisions inside a loop.Use lists to store colors and points.
Use the
randommodule to draw something different each time the program runs.
Lesson 2.1: Variables Make Drawings Move Together
If every shape in a face uses its own numbers, moving the face means changing every number. If every shape is placed relative to x and y, we only change two numbers.
Practice: Change x, y, and size and run the program. Does the face stay together? Then add a nose that also uses x, y, and size.
Lesson 2.2: Loops
A for loop repeats code. The loop variable changes each time, so we can use it to move each shape.
Walk through the first few times the loop runs as a table: when i is 0 the x is 30, when i is 1 the x is 90, and so on.
The loop variable can change size and color too. Drawing big shapes first keeps the small ones on top:
Practice:
Draw a staircase of 8 squares.
Draw 12 lines that all start at (300, 300) and end at different points along the bottom of the canvas.
Draw a "fan" by rotating a long thin rectangle a little more each time through the loop.
Lesson 2.3: Nested Loops and Grids
A loop inside another loop draws rows and columns.
Discussion: What does (row + col) % 2 == 0 check? Fill in a small table of row, col, and (row + col) % 2 to find out.
Practice:
Change the checkerboard to 10 x 10. What else has to change?
Draw a grid of circles where every circle in the top half is blue and every circle in the bottom half is green.
Lesson 2.4: Lists
A list can hold the colors for a pattern. % (remainder) makes the colors repeat.
A loop can also build a points_list. This draws a star by alternating between a far point and a close point around a circle.
Practice: Change the star to have 8 points. Which numbers have to change?
Lesson 2.5: Random
The random module makes every run different.
Practice:
Make 5% of the stars yellow. (Hint:
if random.randint(1, 100) <= 5:)Pick each star's color with
random.choice()from a list of colors.
Lesson 2.6: Simple Animation
A loop with time.sleep() changes a shape a little at a time. The browser shows each change.
Discussion: What does speed = -speed do? What would happen without the if statement?
Unit Project: Generative Art
Students write a program that creates an original piece of art with loops and randomness. Every time the program runs, the art should look a little different, but always in the same style.
Ideas: a city skyline with random building heights, a field of flowers, a quilt pattern, a galaxy, abstract art inspired by a famous artist.
Requirements:
At least one
forloop and one nested loop.At least one
ifstatement inside a loop.At least one list (of colors, points, or sizes).
Uses
randomso that each run is different.At least 100 shapes drawn, using no more than 60 lines of code.
Save three different runs with the Save Canvas button and turn them in with the code.
Loops
Nested loops create a complex, purposeful pattern
A loop and a nested loop are used
Loops used, but no nested loop
No loops
Decisions
Conditionals create several clear effects
At least one conditional in a loop
Conditional is not inside a loop
No conditionals
Lists & random
Lists and random work together in creative ways
Both a list and random are used
Only one is used
Neither is used
Code quality
Variables for all "magic numbers," clear comments
Clear names and comments
Few comments
Hard to read
Art
Original, consistent style across runs
Runs look different but related
Runs look the same
Incomplete
Last updated
Was this helpful?