Using Python mode in Processing

Hi @all! :wave: 

Here is [Luiz](http://luizs.com/). I'm a back-end engineer at [SQUEEZE](https://squeeze-inc.co.jp/).
This is the 9th post for the Python Advent Calendar. I hope you guys don't mind a bit of English. :sweat_smile: 

I have been working with Python only for 5 months, so there isn't much I can bring yet. 

Last year I wrote a post about [Creative Code with Processing](http://qiita.com/luiz/items/191a1e8bfb1037ba7af6 ). If you don't know [Processing](https://processing.org/) or what is [creative coding](https://en.wikipedia.org/wiki/Creative_coding), I recommend you to check my previous post before proceeding on this one. 

Processing is a language based on Java, but with a simple add-on, it's possible to write sketches in Python. Today, let's see how it works.

## How to start

1. Download [Processing](http://processing.org/download) if you still don't have it.
2. Install the Python mode, as described [here](https://github.com/jdf/processing.py).


## Example: Cellular Automation

[Cellular automation](https://ja.wikipedia.org/wiki/%E3%82%BB%E3%83%AB%E3%83%BB%E3%82%AA%E3%83%BC%E3%83%88%E3%83%9E%E3%83%88%E3%83%B3) is a discrete mathematical model, often used in [Generative Art](https://en.wikipedia.org/wiki/Generative_art).

Just as a demonstration, I adapted the example from this [book](https://www.amazon.co.jp/Algorithms-Visual-Design-Processing-Language/dp/0470375485), from the original Processing syntax to Python.

This is the result:

```py
def setup():
    size(300, 300)
    colorMode(RGB)

    global generation
    generation = 0
    
    global memory
    memory = [[0 for x in range(width)] for y in range(height)] 
        
    for x in range(0, width):
        for y in range(0, height):
            if random(1) > 0.5:
                set(x, y, color(0, 0, 0))
            else:
                set(x, y, color(255, 255, 255))

def draw():
    global generation

    for x in range(1, width):
        for y in range(1, height):
            
            k = 0
            for i in range(-1, 2):
                for j in range(-1, 2):
                    if i == 0 and j == 0:
                        continue

                    c = get(x + i, y + j)
            
                    if red(c) == 0:
                        k += 1
            
            if k == 3:
                memory[x][y] = 0

            elif k >= 5:
                memory[x][y] = 255

    for x in range(0, width):
        for y in range(0, height):
            set(x, y, color(memory[x][y]))
    
    generation += 1
    print generation         
```

You are probably thinking that it doesn't really look like "real Python code" (what is that `global`?).
Indeed, some workarounds are necessary to make it work. However, in general, it's the same Python you use every day. For more complex projects, it's possible to create classes, Python libraries and everything else.

This is how a basic cellular automata looks like:

![celluar_automata](http://i.giphy.com/l0HlwPSpuBbs8HZcY.gif)

Change the values in the code and observe how they affect the result! 
Also, instead of only black and white, try to add some colors! :)

Check the [Processing Language Reference](https://processing.org/reference/) for more information about functions you can use.


## Conclusion

If you like Python and would like to play with creative coding or generative art, the [Python Mode for Processing](http://py.processing.org/) is definitely your best choice.


By the way, [SQUEEZE IS HIRING!](https://www.wantedly.com/projects/20396). Join us and make part of a international team in a fast growing Japanese startup! :muscle_tone3: