Fall 2022 ︎︎︎ SUNY Purchase ︎︎︎ (DES3090) Interactive & Experience Design
CPX Warm up #5
(you may need to refresh to see the code)
We're gonna be talking about functions in this warm-up! You've already seen functions before time.sleep() cp.pixels.fill(), the word with parentheses is the function: sleep() and fill().
The above are functions, they are defined by someone else who created that code, and when we type time.sleep(0.2) we are doing what is called calling a function.
What that function do?
Functions allow you to encapsulate functionality. That is to say, prevent you from having to re-type boring or complex code. This serves other functions like preventing you from having to deal with errors and creating code that is more "human-readable." That is to say, a function named sleep() gives you some idea about what it does.
Hi is this function?
So below is some rather simple code we have seen a bunch, a for loop to blink our lights.
It blinks the lights several different colors.︎︎︎
Let's imagine that ultimately we want to make it so we can have a function that says how many times we want our lights to blink, and the color we want our lights to blink. To do that we need, as you can imagine, function.
To create a function you need to use the word "def" this means you are defining the function. You have to define the function before our main loop (while True:) so that is defined by the time we need to call it.
You put the function name (in the case below it is "light_blink"), parentheses, colon and then say what you want to do. After that to actually use it you have to call the function. To do this you just type the name of the function somewhere in your code. Let's take one of the blink loops and put it in our function definition.︎︎︎
Goin' ham...on params (idk, whatever)
So the thing you can do to functions to increase their...functionality, is to add what are called
Parameters are things like the color for cp.pixels.fill((255,255,255)), or the amount of time in time.sleep(0.2). To do that we have to simply add them to our definition.︎︎︎
Our function above, assumes that we want to blink a light a certain color and then turn the light off. We'll also give the function something called blinks which says how many times we want to blink. This allows us to execute an example like that below︎︎︎
The word “color” and “blinks” can be anything, but they should be something that makes it clear what they are being used for. Additionally you can always add comments that explain it if it is a complex function.
When we define the function, we substitute the word color where we would put our tuple of colors (255,0,255) or (255,0,0). This allows us to call the function with any color. This is the same with the word blinks, which controls the amount of blinks.
There are more things you can do with functions but we won't talk about them here for the sake of clarity, and basically they are just making functions more dynamic.
One more random thing
Last thing I want to introduce is randomness just so we can create a specific example. To use randomness we need to import random and then call a function called "random.randint()" and give a range of two numbers.︎︎︎
Note in the above that we create a variable called "which" so that the random number doesn't change each time. We pick one random pixel, fade it out, and then move onto the next one. That's it!
(make sure that you upload each code example to the respective area in the spreadsheet. Create a separate .py for each exercise. Please make the files are named clearly, they need at least, your name, the week/warmup #, and the exercise number so something like “SantiagoBenjamin__WU5__E01.py” would be something I might name my files, but you’re not obligated to use this exact syntax.)
- Recreate the light_blink() function, and add a parameter that allows you to control the wait time in time.sleep()
- Create a function that will play a tone three times, and the frequency/pitch of the tone is defined by
a parameter. Call the function with three different pitches.
- Create two different functions. One makes each pixel blink in sequence (any colors or direction), and one make all the pixels start at (255,255,255) and then fade out to (0,0,0). Call those two functions to show they work.
- Put the randomness example above into a function. Add the functions you created for the previous exercise (#3) and call each of them.
- Take the randomness example and convert it into a function such that the pixels are either "pink" (255,0,255), "yellow" (255,255,0) or "cyan" (0, 255, 255)
︎Back to Interactive & Experience Design