Code Toolkit: Python, Spring 2026

Week 6 — Wednesday, February 25 — Class notes

  1. Review: loops
    1. Homework review: modulo (%) and lerpColor()
    2. Homework review: Space invaders

I. Review: loops

Last week we covered loops, and in particular, while loops.

Loops allow us to create repetition, which in coding is often called iteration.

Before last week we'd already seen one kind of repetition: the def draw() block, which runs all the code in that block and then repeats many times per second. (The number of repetitions per second is defined by the frame rate, and indicated by the special variable frameRate.) But this repetition with def draw() unfolds in time, and so is not typically referred to as iteration.

The repetition that we call iteration is how you would go from drawing one thing on the screen to drawing several, or hundreds, or millions, or a dynamic number of things that you don't know in advance.

The syntax for while loops looks like this:

i = 0
while i < 10:
    println("i = " + str(i))
    rect(i,i,5,5)
    i = i + 1

Which is comprised of: the initial variable assignment of what I call the looping variable, a boolean expression, and a variable increment.

If the boolean expression in the while statement returns True, all the code in the while block will run, and then Python will check the boolean expression again. If the expression is still True, the code will run again. And the code keeps running as long as the expression returns True, or we might say while it is True.

Remember: Don't forget your variable incremeent or you might end up with an infinite loop, which repeats forever without ever stopping.

I also talked briefly about how while loops are functionally equivalent to for loops, which offer you an option of a different syntax that effectively does the same thing. for loops look like this:

for i in range(10):
    println("i = " + str(i))
    rect(i,i,5,5)
There are times when you may need to use one type of loop over the other, but in general these are equivalent and you can use the one that is more clear to you.

(jump back up to table of contents)

a. Homework review: modulo (%) and lerpColor()

The class notes for last week discuss how you can use modulo to create alternating colors for elements drawn within loops, although we did not cover this topic last week. Here is an example of how to do that with a single, linear loop, modifying part 1 above:

i = 50
while i <= 550:
    if i % 100 == 0:
        fill(100,210,0)
    else:
        fill(210,100,0)
    rect(i,300, 20,20)
    i = i + 50

And here is how to do that with a nested loop, modifying part 2 above:

i = 50
while i <= 500:
    
    j = 50
    while j <= 500:
        if (i+j) % 100 == 0:
            fill(100,210,0)
        else:
            fill(210,100,0)

        rect(j,i, 40,40)
        j = j + 50

    i = i + 50

lerpColor(). We didn't talk about this in class but loops allow you to use several different techniques for creating interesting color gradients. Many of you in your homework used using looping variables with the fill() command to acheive this. Another technique is to use looping variables with lerpColor(), which allows you to create a gradient that fades smoothly between two previously defined colors. Again, we didn't discuss this in class, but here's an example of how you might use lerpColor() in a loop to create a gradient:

start_color = color(252,98,255,150)
end_color = color(250,174,103,150)
i = 0
while i < 25:
    c = lerpColor(start_color,end_color,i/25.0)
    fill(c)
    ellipse(350,100+i*20,200,30)
    i = i + 1
And here is how you might try a similar technique in a nested loop:
start_color = color(252,98,255,150)
end_color = color(250,174,103,150)
i = 0
while i < 50:
    j = 0
    while j < 50:
        c = lerpColor(start_color,end_color,(i+j)/100.0)
        fill(c)
        ellipse(100+j*10,100+i*10,12,12)
        j = j + 1
    i = i + 1
If you'd like to experiment with this feel free to reach out with any questions.

(jump back up to table of contents)

Homework review: Space invaders

The bonus / challenge parge of the homework for last week asked you to work with some code that implements a space invaders -style game dynamic, and to think about how you could add a shooting action to the game. Let's talk about that ...

Make sure you can read and understand that code.

After you're able to make sense of that, the question I pose to you is to think about how you might keep track of whether the "bullet" has hit one of the aliens.

Determining contact should be straightforward. During week 5, when reviewing week 4, I talked about two approaches to collision detection. You could apply those here to determine if the bullet hits an alien. But, how do you keep track of whether an alien has been hit or not?

You could use a boolean variable. We could call it something like isHit, set it to False initially, and then if we detect a hit, set that variable to True. But how could we keep track of this for each alien? Would we need one boolean variable for each one? This is precisely what lists are for, and this will be the topic of next week ...