An introduction to Python and Processing — creative programming

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 1 of 20 · Python and Processing

What is Python Processing?


Processing is a tool for visual and creative programming. Instead of printing text in the console, the program draws graphics directly in the application window.

Programs in Processing are often called sketches. A sketch is a small program that draws graphics, makes animations or reacts to the user's actions, such as moving the mouse or pressing a key.

Using Python Mode in the Processing IDE we can write Python code that draws shapes, makes animations and reacts to the user's actions.

Processing is often used in:

  • teaching programming
  • generative art
  • data visualisation
  • interactive installations
  • game prototypes

Note:

Processing is one of the most popular tools for learning graphics programming. Students, artists, scientists and programmers all over the world use it to make visual experiments and prototypes quickly.

Why use Processing?


  • You see the result of the code on the screen straight away
  • An easy way to learn graphics and animation
  • An excellent tool for students and competitors
  • It connects mathematics, physics and programming

Where do we write Python Processing code?


  • Thonny + py5 — recommended: real Python 3, the same environment as for the basics and MicroPython
  • Processing IDE + Python Mode — the old examples from this course; Python Mode is no longer maintained
  • Processing with Java — if you also want to learn Java

The examples in the lessons are written for Processing Python Mode, and each lesson explains what changes in py5. The reason for choosing the environment is explained in the lesson Processing 4 – problems and choosing a path.


How does a Processing program work?


A Processing program usually has two main functions:

  • setup() — runs once at the start of the program
  • draw() — runs all the time (many times a second)

The function setup() is used for the starting settings, such as the size of the window or the starting values of variables.

The function draw() runs again and again, and that is why we can make animations and interactive programs.

Processing usually runs the function draw() about 60 times a second.


The coordinate system


Processing uses a two-dimensional coordinate system.

  • (0,0) is the top left corner of the window
  • the x axis goes to the right
  • the y axis goes down

For example:


ellipse(100, 100, 50, 50)

This code draws a circle whose centre is at the point (100,100).


Example: The first window



def setup():
    size(600, 400)

def draw():
    background(200)

This program opens a window of 600×400 pixels.


Experiment:

Change the size of the window in the function size(). For example:

size(800, 500)
Run the program again and see how the size of the window has changed.

Example: Draw a circle



def setup():
    size(600, 400)

def draw():
    background(240)
    fill(0, 100, 200)
    ellipse(300, 200, 100, 100)

The program draws a blue circle in the centre of the window.


Try it yourself:

Change the parameters of the function ellipse() and watch what happens. For example:
  • make the circle bigger
  • move the circle to the left or to the right
  • change the colour with the function fill()
Experiments are the best way to learn Processing.

Mini projects — experiments


A moving circle

Short description: A circle moves across the screen.


x = 300

def setup():
    size(600, 400)

def draw():
    global x
    background(240)
    fill(200, 50, 50)
    ellipse(x, 200, 50, 50)

    x += 1

    if x > width:
        x = 0

The circle moves to the right and comes back to the left edge when it leaves the window.


Changing the colour with the mouse

Short description: The colour of the circle depends on the position of the mouse.


def setup():
    size(600, 400)

def draw():
    background(220)
    fill(mouseX % 255, 100, 200)
    ellipse(width/2, height/2, 100, 100)

Moving the mouse horizontally changes the colour of the circle.


The difference between Processing and classic Python


  • There is no need to import graphics libraries
  • Functions such as ellipse(), rect() and line() already exist
  • The program keeps running through the draw() loop

Related lessons



What comes next?

In the next lessons you will learn:

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: how a sketch works

1. How many times does the function setup() run?




2. Where is the point (0, 0) in the window?




3. Why does the circle move if we increase x in draw()?




Useful external resources