Parsing Booleans

Quick Overview of Day

Work together with a partner to read code and determine what it will do. Then add new functionality to the code.

Boolean Order of Operations Reminder

  • NOT !

  • AND &&

  • OR ||

Reading Code

Figure this out, without using a computer!.

Don’t open these until you have read the handout above, and figured out what will happen using only your brain!

Experimenting with the Code

Why are all of the variables defined outside of the setup() and draw() functions? What happens if you define them inside those functions? Try it.

Talk about local and global variables, and the fact that p5js built in variables are not able to be used until the createCanvas() function is called (demo this by randomizing the x or y value of the rectangle on first run).

Adding New Functionality

Now that you have determined how the code works, improve both of the above projects so that when the button variable is true, the rectangle can be dragged around the screen to a new location. In order to make this work reasonably well, you should drag the rectangle by middle point (half way across it vertically, half way across it horizontally). If you are unsure what I mean, take a look at the completed versions below:

Next Section - Loops and Nested Loops