# Need help with setting up Random Walker (Coding Train)

**URL:** https://discourse.processing.org/t/need-help-with-setting-up-random-walker-coding-train/25715
**Category:** Processing.py
**Created:** [November 25, 2020, 10:49pm UTC](https://discourse.processing.org/t/need-help-with-setting-up-random-walker-coding-train/25715 "2020-11-25T22:49:20Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Epsilon](https://avatars.discourse-cdn.com/v4/letter/e/f14d63/32.png) [@Epsilon](https://discourse.processing.org/u/Epsilon)
#### Post date: [November 25, 2020, 10:49pm UTC](https://discourse.processing.org/t/need-help-with-setting-up-random-walker-coding-train/25715/1 "2020-11-25T22:49:20Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

### Author: ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)
#### Post date: [November 25, 2020, 11:01pm UTC](https://discourse.processing.org/t/need-help-with-setting-up-random-walker-coding-train/25715/2 "2020-11-25T23:01:28Z")

</div>

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:

```python
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:

```python
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](https://github.com/nature-of-code/noc-examples-python)

_… but don’t reach straight for the solutions without challenging yourself first 😉_

---

<div class="post-metadata">

### Author: ![Epsilon](https://avatars.discourse-cdn.com/v4/letter/e/f14d63/32.png) [@Epsilon](https://discourse.processing.org/u/Epsilon)
#### Post date: [November 27, 2020, 2:07pm UTC](https://discourse.processing.org/t/need-help-with-setting-up-random-walker-coding-train/25715/3 "2020-11-27T14:07:10Z")

</div>

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