Review

Custom Functions - Review

def function_name( arg1, arg1 ):
    statement1
    statement2
    return expression    # Optional

Calico Editor Hint:

global Statement

Why?

def Vegas():
    x = 20
    print('In Vegas, x=', x)

x = 10
Vegas()
print('Not in Vegas, x=', x)

>>> In Vegas, x= 20
>>> Not in Vegas, x= 10

global Statement

Why? Because what happens in Vegas, stays in Vegas. ;-)

def Vegas():
    x = 20
    print('In Vegas, x=', x)

x = 10
Vegas()
print('Not in Vegas, x=', x)

>>> In Vegas, x= 20
>>> Not in Vegas, x= 10

global Statement

Why? Variables initialized in a function, stay in that function.

def Vegas():
    x = 20
    print('In Vegas, x=', x)

x = 10
Vegas()
print('Not in Vegas, x=', x)

>>> In Vegas, x= 20
>>> Not in Vegas, x= 10

global Statement

Why? Variables initialized in a function, stay in that function,
and disappear when the function exits.

def Vegas():
    x = 20
    print('In Vegas, x=', x)

x = 10
Vegas()
print('Not in Vegas, x=', x)

>>> In Vegas, x= 20
>>> Not in Vegas, x= 10

global Statement

Why? Variables initialized in a function, stay in that function,
and disappear when the function exits, unless we use the global statement.

def Vegas():
    global x
    x = 20
    print('In Vegas, x=', x)

x = 10
Vegas()
print('Not in Vegas, x=', x)

>>> In Vegas, x= 20
>>> Not in Vegas, x= 20

global Statement

What would have happened if we had no global statement?

# Store the last mouse pressed position.
from Processing import *
window(500, 300)

# Draw a line between last and current position.
lastX = 0
lastY = 0

def mousePressed(o, e):
    global lastX, lastY
    line(lastX, lastY, mouseX(), mouseY())
    lastX = mouseX()
    lastY = mouseY()

onMousePressed += mousePressed

global Statement

What would have happened if we had no global statement?
>>> Local variable 'lastX' referenced before assignment

# Store the last mouse pressed position.
from Processing import *
window(500, 300)

# Draw a line between last and current position.
lastX = 0
lastY = 0

def mousePressed(o, e):
    # global lastX, lastY
    line(lastX, lastY, mouseX(), mouseY())
    lastX = mouseX()
    lastY = mouseY()

onMousePressed += mousePressed

global Statement

Recall: Simple Animations with the loop Timer

# lineRotate.py
from Processing import *
from math import *
window(500, 500)

# Globals and settings
angle, dangle = 0.0, 0.01
strokeWeight(20)
stroke(0, 255, 0)

# Draw the line at the current angle
def rotate(o, e):
    global angle
    background(255)
    x1 = cos(angle)*200+250
    y1 = sin(angle)*200+250
    x2 = cos(angle + PI)*200+250
    y2 = sin(angle + PI)*200+250
    line(x1, y1, x2, y2)
    angle = angle + dangle

# Set up the loop
frameRate(100)
onLoop += rotate
loop()

Conditional Code Execution: the if-statement

if-statements allow you to execute a block of code, conditionally.
Expressions must evaluate to a boolean (True or False).

if relational-or-logical-expression:
    statement1
    statement2

Conditional Code Execution: the if-statement

Draw red ellipses on the right, green on the left

# splitEllipse1.py
from Processing import *
window(500, 500)

def drawEllipse(o, e):
    fill(0, 255, 0)             # Start with green
    x, y = mouseX(), mouseY()
    w, h = width(), height()    # Conditionally,
    if x > 0.5*w:               # change fill
        fill(255, 0, 0)         # to red
    ellipse(x, y, 30, 30)

onMousePressed += drawEllipse

Conditional Code Execution: the else-statement

The else statement sets the default branch of if

if relational-or-logical-expression:
    statement1
    statement2
else:
    statement3
    statement4

Conditional Code Execution: the else-statement

The else statement sets the default branch of if

# splitEllipse2.py
from Processing import *
window(500, 500)

def drawEllipse(o, e):
    x, y = mouseX(), mouseY()
    w, h = width(), height()

    if x > 0.5*w:
        fill(255, 0, 0)     # Red on right
    else:
        fill(0, 255, 0)     # Green in left
    ellipse(x, y, 30, 30)

onMousePressed += drawEllipse

Conditional Code Execution: the elif-statement

The elif statement defines alternate branchs of if

if relational-or-logical-expression:
    statement1
    statement2
elif relational-or-logical-expression:
    statement3
    statement4
elif relational-or-logical-expression:
    statement5
    statement6
else:
    statement7
    statement8

Conditional Code Execution: the elif-statement

The elif statement defines alternate branchs of if

# splitEllipse3.py
from Processing import *
window(500, 500)

def drawEllipse(o, e):
    x, y = mouseX(), mouseY()
    w, h = width(), height()

    if x > 0.5*w and y > 0.5*h:
        fill(255, 0, 0)     # Red: on lower right
    elif x > 0.5*w and y <= 0.5*h:
        fill(0, 0, 255)     # Blue: lower left
    elif x <= 0.5*w and y > 0.5*h:
        fill(255, 255, 0)   # Yellow: upper right
    else:
        fill(0, 255, 0)     # Green upper left
    ellipse(x, y, 30, 30)

onMousePressed += drawEllipse

Conditional Code Execution: the elif-statement

Alternative implementation with nested if-statements

# splitEllipse4.py
...
def drawEllipse(o, e):
    x, y = mouseX(), mouseY()
    w, h = width(), height()

    if x > 0.5*w:
        if y > 0.5*h:
            fill(255, 0, 0)     # Red low right
        else:
            fill(0, 0, 255)     # Blue low left
    elif x <= 0.5*w:
        if y > 0.5*h:
            fill(255, 255, 0)   # Yellow up right
        else:
            fill(0, 255, 0)     # Green up left

the while-statement

while-statements allow you to repeatedly execute a block of code, conditionally.
Expressions must evaluate to a boolean (True or False).

while relational-or-logical-expression:
    statement1
    statement2

the while-statement

Pattern: Repeat a block of code a defined number of times

# while1.py
from Processing import *
window(500, 500)

# Draw 1000 rectangles
i = 0
while i < 1000:
    i = i + 1
    x = random(0, width())
    y = random(0, height())
    rect(x, y, 40, 40)

the while-statement

The break statement exits a while-loop

# while2.py
from Processing import *
window(500, 500)

# Draw 1000 rectangles
i = 0
while True:
    i = i + 1
    if i >= 1000:
        break

    x = random(0, width())
    y = random(0, height())
    rect(x, y, 40, 40)

Key Events

onKeyPressed:Event raised when a key is pressed
onKeyReleased:Event raised when a key is released
key():Function that returns the key value, if pressed, 0 otherwise
keyCode():Function that returns the numeric key code, if pressed, 0 otherwise
isKeyPressed():Function that returns True if a key pressed, False otherwise

Key Events

# keyTest.py
from Processing import *
window(200, 100)

# Print key, keyCode when pressed
def testKey(o, e):
	print( "key() is ", key() )
	print( "keyCode() is ", keyCode() )

onKeyPressed += testKey

Assignment 2

...

/

#