Animation and variables — moving an object

Environment:
the examples in this lesson are written for Processing Python Mode. If you work in the recommended environment Thonny + py5, most of the code stays the same, and names with a capital letter in the middle are written in lower case with an underscore: mouseX → mouse_x, noStroke() → no_stroke(). See the table of differences.

Lesson 6 of 20 · Python and Processing

What actually is animation?


Animation in Processing is changing the picture quickly, many times a second.

The function draw() repeats all the time and draws a new picture every time.

If we change the position of an object a little in every repetition — we get movement.

Important:

Animation = draw → move → draw → move → (many times a second)

Why are variables important?


Variables keep the state of an object — where the object is at every moment.

  • x — the horizontal position
  • y — the vertical position
  • dx — the speed along the x axis
  • dy — the speed along the y axis
Think of variables as memory between frames.

A simple ball animation



x = 50
y = 200
dx = 3

def setup():
    size(600, 400)
    frameRate(60)

def draw():
    global x, dx

    background(220)

    fill(255, 0, 0)
    ellipse(x, y, 50, 50)

    x += dx

    if x > width - 25 or x < 25:
        dx = -dx
Result
A red ball moves left and right and bounces off the edges.

How does this work?


  1. the screen is cleared (background())
  2. the object is drawn
  3. the position changes
Processing does not move the object — it draws it again every time.

Animating a picture (sprite)


Now we use a picture instead of a shape.


img = None

x = 0
y = 150
dx = 3

def setup():
    global img
    size(600, 400)
    img = loadImage("character.png")

def draw():
    global x, dx

    background(220)

    image(img, x, y)

    x += dx

    if x > width - img.width or x < 0:
        dx = -dx
Result
The picture moves left and right and bounces off the edges.
Why global img?

img must be available in the draw() function. If you define it only in setup(), it will not work.
A common mistake:

This does NOT work:

def setup():
    img = loadImage("character.png")
Because img is not a global variable.

The size of the picture:

Use

img.width
img.height
to get the exact dimensions.

Experiment:

  • Change the speed (dx)
  • Change the starting position
  • Use your own picture

Mini challenge

Add vertical movement:
  • make dy
  • add y += dy
  • add bouncing along the y axis

Why do we use global?


Variables defined outside a function must be marked as global to be changed in draw().


global x, dx
Without global, changing the value will not work as you expect.

The speed of animation — frameRate()


frameRate(60) means 60 frames per second.

Experiment:

  • frameRate(10) — slow
  • frameRate(120) — fast

Mini project: an animated scene


  • a picture of a character
  • a background
  • additional objects
Combine pictures and shapes for better projects.

Next lesson


Interaction with the mouse → (SR)


Related topics


The same in Processing with Java

The logic is the same; only the syntax of the language is different.

Check your knowledge

Open the quiz

Mini quiz: animation

1. Why do we write global x in draw()?




2. What happens if you leave out background() in draw()?