Code Toolkit: Python, Fall 2025

Week 4 — Wednesday, September 17 — Class notes

Table of contents for today

  1. Review of last week
    1. Homework review
  2. Conditionals and making things move (on their own)
    1. Background
  3. mousePressed and keyPressed
  4. Conditionals
  5. Keyboard interaction
  6. Events
  7. Making things move (on their own)

I. Review of last week

(jump back up to table of contents)

a. Homework review

In class we reviewed Simarna's homework togther.

We practiced reading her code and deduced that it was choosing a random color once at the start of the sketch and drawing three shapes: one moved horizontally, one moved vertically, and one was a triangle with two hard-coded vertices, and a third vertex that moved left and right with the mouse.

Together we worked out how to use sin() to add some curved motion to this composition.

First, I explained how sine and cosine work in general terms, recalling principles of trigonometry. The sine function returns a range of values from 0 to 1 to -1 and then back to 0, as its variable (usually shown along the x axis) moves from zero to 2 pi.

In Python / Processing, we can work with that like this:

t = map( mouseX, 0,width, 0,2*PI )
s = sin(t)
fill(155,155,255)
ellipse(t*75,s*200+300, 50,50)

Note that mouseX is being map()ed from its minimum and maximum values into the minimum and maximum values of one cycle of sine, which I call t, and which we calculate with the built-in function sin(). Processing also offers us a built-in variable for pi: PI

Then, we take t as the input (the argument) for the sin() function. This way, as the mouse moves from the left to the right of the window, t will span the range of one cycle of sine, and the variable s will also go through one cycle of a sine wave, from 0 to -1 to 1 and back to 0.

Just drawing that would not yield very much movement because those values (0 to about 6.28, and -1 to 1) are not very large values within our draw window. So, we multiplied t by 75 and s by 200. I also used addition to shift the wave down in the window by 300.

Next we talked about how we could add in cos() to create a circle:

line( 300,300, 300+sin(t)*100,300+cos(t)*200 )

This code uses the same t parameter, so that as the mouse moves from the left to the right of the window, we will get values within one cycle of sine and cosine.

If you recall from trig, the x and y coordinates of a circle are determined by sine and cosine. As the y value of the circle cycles counter-clockwise through sine values (from 0, to 1, back to 0, then -1, and then back to 0), the x value of the circle cycles through the cosine values (from 1 to 0, to -1, to 0, and back to 1). Thus, we use sin(t) for the x value of ellipse(), and we use cos(t) for the y value.

As beore, just using these values would make very small movement. So we manipulated them with multiplication to shift and scale them as we wanted.

You can see the full code here:

week04_week03hw_simarna.pyde.txt

Thanks, Simarna, for sharing!

II. Conditionals and making things move (on their own)

This week we will build on last week in two ways.

First, we will learn how to use conditionals so that instead of the continuous change of mouse movement, you can create movement that is discrete and discontinuous.

Second, we will build on our use of variables and Processing's interactive mode to create things that move on their own, not only as directly controlled by the mouse.

a. Background

Last week we saw how to make interactive compositions, but they were always moving in continuous, connected ways. Colors that changed as smooth gradients, and shapes that moved, resized, or left trails, but only ever directly correlated with mouse movement.

Today we will see how to work with one of the fundamental principles of digital media which is how to work with discontinuity, or on/off relationships — shapes that appear and disappear, or change their size and locations in discontinuous ways. This kind of behavior is often described as discrete. (Not to be confused with "discreet"!)

In 1997, the net artist John Simon created a project called Every Icon. This conceptul work enacts a play of combinatorics by starting with the first (top, left) pixel of a 32 by 32 grid, and advances in sequence, creating every possible combination of pixels, or in other works, every possible icon.

This work emphasizes the way that all digital images are created not as smooth strokes, continuous marks, or smooth lines, but rather always as grids of pixels, always turned either on or off.

To similar ends but in a more poetic and ironic way, the artist Hito Steyerl, in her documentary How Not to be Seen: A Fucking Didactic Educational .MOV File, explores (and blurs) the boundary between the analog and digital, between the physical world and the world of digital representations, or in other words, between the smooth and the discrete.

Let's keep all of this in the back of our minds as we explore the logic of discrete on/off structures today and dive in to binary logic.

(jump back up to table of contents)

III. mousePressed and keyPressed

(jump back up to table of contents)

IV. Conditionals

(jump back up to table of contents)

V. Keyboard interaction

(jump back up to table of contents)

VI. Events

Sidenote: There is a similar pattern here for the mouse. The mousePressed() block is also valid syntax and would be used in a similar way:
def setup():
    size(600,600)
    rectMode(CENTER)

def draw():
    background(255)

def mousePressed():
    ellipse( 300,300, 50,50 )
(jump back up to table of contents)

VII. Making things move (on their own)