• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

BMS Processing Class

A Course23.com Production

"Stop, Pause, and Think..." -- Ms. Elwell
"Take your time, it isn't a race" -- Mr. Helmke

  • Home
  • Conditionals and Logic
    • Conditionals 1: Boolean Expressions and the If Statement
      • Activity 16 (IF1)
    • Conditionals 2: If/Else Statements
      • Activity 17 (IFELSE1)
    • Conditionals 3: Else If Statements
      • Activity 18 (elseif1)
      • Activity 19
      • Activity 20 (elseif3)
      • Activity 21
    • Logical Operators
      • Activity 22 (And1)
      • Activity 23
      • Activity 24 (SingleRollover)
      • Activity 25 (FourSquareRollover)
      • Activity 26 (MoveWhenPressed)
      • Activity 27 (bouncy ball 1)
      • Activity 28 (bouncy ball 2)
  • Functions
    • Moving Shapes 1
      • Activity 29 (StarBright)
      • Activity 30 (CreatureAtOrigin)
    • Moving Shapes 2
      • Activity 31 (drawCreatureFunction)
      • Activity 32 (BouncingCreatures)
    • Rotating Shapes
      • Activity 33 (RotatingCreatures)
      • Activity 34 (RotatingShapes)
      • Activity 35 (RotatingShapes2)
  • Looping and Arrays
    • Loops 1
      • Activity 36 (ShapeLoop)
      • Activity 37 (RandomLoop1)
    • Loops 2
      • Activity 38 (Deg2Rad)
      • Activity 39 (NumberLoop1)
      • Activity 40 (NumberLoop2)
    • Arrays 1
      • Activity 41
      • Activity 42
      • Activity 43
    • Arrays 2
      • Activity 44
      • Activity 45
      • Activity 46
  • Final Topics
    • Introduction
    • Particle Systems
    • Maze Game
  • Resources

Loops 1

This section is titled “Looping and Arrays”. Briefly, looping lets you run blocks of code more than once, over and over. Arrays are a kind of variable that can store multiple values at the same time. Loops and arrays work really well together but it would be hard to learn both at once. We will start with loops and some examples and activities that will seem contrived. Trust us, your patience will be rewarded when you get to arrays.

As we were saying, looping is a way to get a block of code to run one or more times. So far, if you wanted to have something run more than once, you write more code. Three rectangles and an ellipse for example:

1
2
3
4
rect(100,50,30,30);
rect(200,50,30,30);
rect(300,50,30,30);
ellipse(100,100,30,30);

could also be written (rather strangely) as:

1
2
3
4
5
6
7
var x=100;
rect(x,50,30,30);
x = x + 100;
rect(x,50,30,30);
x = x + 100;
rect(x,50,30,30);
ellipse(100,100,30,30);

but could also be written as:

1
2
3
4
5
6
7
8
9
var numRects = 3;
var i = 1;
var x = 100;
while (i <= numRects) {
    rect(x * i, 50, 30, 30);
    i = i + 1;
}
 
ellipse(100,100,30,30);

This is an example of a “while loop”. What it does is that “while” whatever is in the expression in parenthesis is true, do the stuff in the curly braces. When the expression is false, the while loop stops running and the ellipse is drawn. Note, if the variable “i” didn’t have 1 added to it each time though the loop, the loop would never stop. That is called an infinite loop and isn’t normally what you want.

Here is another example:

1
2
3
4
5
6
7
8
var i=0;
var total = 0;
var limit = 5;
 
while(i <= limit) {
    total = total + i;
    i = i + 1;
}

And the end of that, total will contain the sum of all of the values for i.

Complete Activity 36

So loops are useful for repetitive work. For the next assignment, I want to display a series of random numbers on the screen but to do that nicely we need to go back to a discussion of functions.

I want to display 10 random numbers on the screen so the display looks like this:

Random number 1 is 23
Random number 2 is 42
Random number 3 is 5
…

However, Processing doesn’t have a way to automatically print a series of lines of text to screen. You can print() to the console but I want the output on the main display because it looks better. You can draw to the screen using the text() function. I’m not going to discuss how to use text() in detail, look it up here: https://p5js.org/reference/#/p5/text

I also recommend you also look at the textAlign() and textSize() functions. There is even a textWidth() function to return how many pixels wide some text is.

textWidth() is called like this:

1
2
var x;
x = textWidth("the quick brown fox jumped over the lazy snail");

Note that the function textWidth returns a value we can assign to a variable. After that code, the variable x holds the number of pixels wide that text will be on screen.

Annoyingly, there is no equivalent “textHeight()” function. However, looking at the p5js reference, I see there are two functions textAscent() and textDescent() which return the maximum height of the current font above and below the font baseline.

That is, you are displaying text to the screen using some font. There is a symbol in that font which is the tallest above some point and that height is returned by textAscent(). Similarly, there is a symbol which goes lowest below some point (like ‘g’) whose height below is returned by textDescent.

We can create this function:

1
2
3
function textHeight() {
    return textAscent() + textDescent();
}

Which happens to be the maximum vertical height of a character in the current font. We can use this to control vertical positioning of text on the screen. It even works if you change the text size (using textSize() ).

The following will print “Howdy!” on 3 consecutive lines and the current value of i. Try putting this in a sketch to see what it does:

1
2
3
4
var i=0;
while(i<3) {
  text("Howdy! " + i, 50, 100 + (i*textHeight()));
}

Note the weird use of “+” between “Howdy!” and i. The text is added to the variable. This is how you stick pieces of text together. It is called “concatenation”. Here are more examples:

1
2
3
4
5
6
7
8
9
10
11
var a = "Hello";
var b = "There";
text(a + " " + b, 100,100);
 
var c = 123;
var d = " some number";
text(c + d, 100,130);
 
var e = 123;
var f = 456;
text(e + f, 130, 150); // this of course prints the sum of 123+456.

Processing is smart enough to know if you are “adding” a string and a number together that you mean to convert the number to a string and then make a combined string of them. You don’t need to understand that yet but other programming languages make a bigger deal of that. In case you start looking at other languages.

Complete Activity 37

Primary Sidebar

Required Reading:

Course Coding Standards

Copyright © 2016–2021 Chris Elwell/Michael Helmke · This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License · Log in