CS 1 Lecture 6

Animation and user input

Professor Devin Balkcom
devin@cs.dartmouth.edu
office: Sudikoff 206

http://www.cs.dartmouth.edu/~cs1

Local vs. global variables

A variable that is first assigned a value inside a function is a local variable.

Local variables are good. Most lines of code: compute something, and store it in a nicely named local variable. Then use that variable to compute something else, and store it in a nice local.

Frames and scope

When a function is called, it creates a frame, an area of memory to store values of local variables. When the function returns, it destroys the frame.

Python tutor example of local variables. Or use Pycharm debugger.

Global variables

Global variables are initialized outside of any function, and are placed into the global frame.

Changing a global variable is dangerous

All code that relies on that global will work differently. Must use a special keyword global to change a global within a function.

Corollary. Do not write naked code that depends on a global variable. Wrap in a function and pass parameters. Bad Brutus!

Global pollution

Photo by Nils Ally

Any global variable you declare has to have a unique name. If you create a global variable x to mean one thing, you cannot later create a variable x to mean another.

Corollary. Do not use global variables.

Named constants

There is one good use for global variables. Constants are better than magic numbers.

AVOGADRO = 6.0221415e23

Animation and user input

In stage magic, they say that the hand is quicker than the eye.
Python is faster than either.

How can we make Python wait for the slow humans?

Callback functions

Types: int, float, string, function.
A function definition creates a global variable referring to a function.

A callback function is a function that you write, but never intend to call yourself. Instead, you pass it to another function for use.

Animation

Each page of the flipbook is called an animation frame.

Flip book animation

Viewing the animation:

  1. Go to the first page of the book.
  2. There's a page with a frame of the animation. Display it.
  3. Wait for the human to see it.
  4. Move to the next page.
  5. Go back to step 2.

It is a loop, but step 3 requires that we use the callback approach. (Let the browser do the looping; we write the function.)

Animation on a computer

Model-view-controller approach:

  1. Create variables represent state of frame. (model)
  2. Use state variables to draw the frame. (view)
  3. Wait for human.
  4. Change values of state variables. (controller)
  5. Go back to step 2.

Animation example: growing circle

Animation example: pulsing circles

Animation example: psychboxes

Go do exercise: moving ball

Mouse input

Mouse input

Notice:

  1. Named parameter used to specify mouse callback
  2. Mouse callback function does not draw.
  3. State updated by mouse callback.
  4. Drawing function draws based on state variables.

Mouse movement

Keyboard input

Do at home: smiley game exercise