1 of 27

Code Camp

Day 5/6

2 of 27

Drawing Program?

Have an idea for a project you want to work on by Monday.

3 of 27

4 of 27

5 of 27

Remember this?

mouseIsPressed is a property. It is a boolean system variable.

6 of 27

So what is this?

function mousePressed() {

d = d + 10;

}

It is a function that gets called whenever the mouse is clicked.

It is a type of EVENT.

7 of 27

Mouse Events

Keyboard Events

8 of 27

Drawing a Pattern of Circles

This code is terribly inefficient.

What if the number of rows or columns changes?

What if the spacing changes?

Red Flags:

  1. Look for repetition in your code
  2. Look for numbers where parameter (like diameter) should be.

function mousePressed(){

fill("blue");

//draw one row

circle(mouseX +10, mouseY, 10);

circle(mouseX +20, mouseY, 10);

circle(mouseX +30, mouseY, 10);

circle(mouseX +40, mouseY, 10);

circle(mouseX +50, mouseY, 10);

//draw another row

circle(mouseX +10, mouseY+10, 10);

circle(mouseX +20, mouseY+10, 10);

circle(mouseX +30, mouseY+10, 10);

circle(mouseX +40, mouseY+10, 10);

circle(mouseX +50, mouseY+10, 10);

}

9 of 27

Using A While Loop (Review)

let x = 1;�while (x < 5){ //do this; X++}

// don’t forget to increment or your browser will stop working!

10 of 27

Using the While Loop Reduce Repetition

This loop replaces all the repetitive actions.

10?

10?

11 of 27

What is wrong with this picture?

5

4

This is a one-off error

12 of 27

Some red flags are removed.

Now see how easy it is to change the diameter and number of columns.

Could it be better?

13 of 27

for(let i = 1; i <= 5; i++ ){ }

Packs the whole loop process into one neat package

14 of 27

A nice, neat for loop

15 of 27

Nested column function inside of row function.

Does this code draw columns first, or rows first?

16 of 27

Review Video

0:00 Events�0:13 mousePressed() event�5:44 While Loop Review�7:50 One Off Error�8:45 For Loop�10:07 Increment Shortcut i++�10:27 Nesting For Loops

17 of 27

18 of 27

Objects

So darn powerful.

myBall = new ball();

myBall.show();

myBall.move();

myBall.pop();

But… how do you make a ball object?

19 of 27

Classes - Constructor method

Classes explain how to make objects and what they can do.

class Ball {

constructor( _x, _y ) {

}

move( ){

}

show( ){

}

The constructor function builds the object

These are passed to the constructor

“Instantiating” an object

20 of 27

Classes

Classes explain how to make objects and what they can do.

class Ball {

constructor( _x, _y ) {

}

move( ){

this.x = this.x + 10;

}

The methods give the object functionality

21 of 27

Example

Find a class called “Ball”

Arguments from new go into constructor

22 of 27

Showing the ball

Just because a ball was created does not mean we can see it. We need to “show” the ball. We need to know what “showing” a ball means.

This is “how” to show a ball

This says to show a particular ball

23 of 27

Moving the ball (on mousePressed, for example)

This is “how” to move a ball

24 of 27

Look how simple the draw function is.

25 of 27

Add a Method: pop

This method will be added inside the Ball class...

There is a special function that works like draw, but only on mouse event:

26 of 27

Using Images

27 of 27

Adding other files

This is how a library works.