Need help with setting up Random Walker (Coding Train)

Hello everyone,

I’m a newcomer to Processing.py and I’ve been trying to follow the Nature of Code videos.
They revolve around a subject I am very interested in, coming from 3D modelling and motion graphics

I’ve been trying to “port” the code used in Processing.js to Python mode with each step but I’m hitting some roadblocks. I keep finding clunky workarounds to make the code work but don’t really know what is wrong with the code.

Here is my code:

x = 200
y = 200
class Walker():
    def setup():
        x = width/2
        y = height/2
    def render(self):
        stroke(0)
        point(x, y)
    def step(self):
        global x
        global y
        choice = int(random(4))
        if choice == 0:
            x += 1
        elif choice == 1:
            x -= 1
        elif choice == 3:
            y += 1
        else:
            y -= 1        
       
w = Walker()

def setup():
    frameRate(100)
    global w
    size(400, 400)
    background(255)

def draw():
    global x, y 
    x = constrain(x, 0, width-1)
    y = constrain(y, 0, height-1)
    w.step()
    w.render()

The output is correct, but I can tell I created some redundancies in the code because I kept having issues with x and y being undefined in the Walker class. The code kept asking for them as global variables in order to work so I had to set them up “manually” at the center of the canvas instead of letting the halving of width and height doing it for me. And if I set outside of setup(), height and width are considered undefined variables aswell.

I’m sure it’s something obvious to many but since I’m new to working with classes in Processing, I would like some help.

Thank you in advance

1 Like

Welcome @Epsilon

I think this is an excellent way to pick up some Processing.py skills (converting NOC examples to Python.)

To set your x, y variables dynamically based on the width and height of the display window, use something like this:

def setup():
    global x, y, w
    frameRate(100)
    size(400, 400)
    x = width / 2
    y = height / 2
    background(255)

This way, there’s no need to define variables x, y at the top of your code. Where necessary, the global keyword will reference variables outside of that function – and if they don’t exist, Python will define them in the global scope for you.

But – it’s probably better to avoid the global variables altogether! Instead, store the x, y values in each instance of your Walker class. Something like:

class Walker(object):

    def __init__(self, x, y):
        self.x = x
        self.y = y

    def render(self):
        point(self.x, self.y)

    ...

def setup():
    global w
    ...
    w = Walker(width/2, height/2)

def draw():
    global w
    w.step()
    w.render()

This way, each instance of Walker has it’s own set of x, y values – which is handy if you wish to add multiple walkers.

For ‘model’ solutions, you can refer to this GitHub repo: https://github.com/nature-of-code/noc-examples-python

… but don’t reach straight for the solutions without challenging yourself first :wink:

2 Likes

Thank you so much @tabreturn !
For the valuable help and the repo. I didn’t know there were a python mode version :slightly_smiling_face:
I think I’m starting to understand the “order of execution” in Processing.
Thank you again for the quick and thorough reply

1 Like