# Nature of Code (Random Walker) using Python

**URL:** <https://discourse.processing.org/t/nature-of-code-random-walker-using-python/43236>\
**Category:** Processing.py\
**Created:** [November 17, 2023, 5:53pm UTC](https://discourse.processing.org/t/nature-of-code-random-walker-using-python/43236 "2023-11-17T17:53:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hsu](https://avatars.discourse-cdn.com/v4/letter/h/9de0a6/32.png) [@Hsu](https://discourse.processing.org/u/Hsu)\
**Post date:** [November 17, 2023, 5:53pm UTC](https://discourse.processing.org/t/nature-of-code-random-walker-using-python/43236/1 "2023-11-17T17:53:32Z")

</div>

Hello, everyone.  
I am currently learning Processing by watching Nature of Code wrote by Dan Shiffman.  
And I encountered a problem that bothers me for 1 week, that’s why I am asking for help from the brilliants here.  
I tried to replicate the Random Walker Java code into Python, and I stuck in import / reference “class” from another tab, which is file\_name.py  
I did figured out how to make it worked in one tab, code shown below:

```auto
posx, posy, direction = 400, 400, 0 # Initial position and direction

def setup():
    size(800, 800)
    background(255)

def draw():
    global posx, posy, direction

    direction = int(random(4))

    if direction == 0:
        posx += 1
    elif direction == 1:
        posx -= 1
    elif direction == 2:
        posy += 1
    else:
        posy -= 1

    posx = constrain(posx, 0, width - 1)
    posy = constrain(posy, 0, height - 1)

    stroke(0)
    point(posx, posy)

```

BUT.  
I can’t import class into main tab, every time I hit Render, I got blank window or just white background without the “Walker” object.  
Please help me out, cheers.

Main\_Tab.pyde

```auto
from Setups import *

w = 0

def setup():
    size(800,800)
    background(255)
    global w
    w = Walker()

    
def draw():
    w.step()
    w.display()

```

Setups.py

```auto
class Walker:
    def __init__ (self, posx, posy):
        self.posx = width / 2
        self.posy = height / 2
    
    def display(self):
        stroke(0)
        point(self.posx, self.posy)
        
    
    def step(self):
        direction = int(random(4))

        if direction == 0:
            self.posx += 1
            
        elif direction == 1:
            self.posx -= 1
        elif direction == 2:
            self.posy += 1
        else:
            self.posy -= 1
        
        self.posx = constrain(self.posx, 0, width - 1)
        self.posy = constrain(self.posy, 0, height - 1

```

)

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 17, 2023, 8:16pm UTC](https://discourse.processing.org/t/nature-of-code-random-walker-using-python/43236/2 "2023-11-17T20:16:11Z")

</div>

Hello @Hsu,

This worked with Processing 3.5.4 in Python mode:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/5/a/5a5706545071394295bb5ec956bcf9248b938d6c.png)

Reference:

> [@Imported class from another tab doesn't work](https://discourse.processing.org/t/imported-class-from-another-tab-doesnt-work/22135/2):
>
> I believe you’ve got instead NameError: global name ‘voronoi’ is not defined, right? Should be instead: from voronoi\_class import voronoi Keyword global should go inside a function instead: [Py.Processing.org/reference/globals.html](http://Py.Processing.org/reference/globals.html) def setup(): # ... global v v = voronoi(100, w, h) println(v.r) It’s customary for class names to be “CamelCase” instead: class Voronoi:

`:)`

---

<div class="post-metadata">

**Author:** ![Hsu](https://avatars.discourse-cdn.com/v4/letter/h/9de0a6/32.png) [@Hsu](https://discourse.processing.org/u/Hsu)\
**Post date:** [November 19, 2023, 2:19pm UTC](https://discourse.processing.org/t/nature-of-code-random-walker-using-python/43236/3 "2023-11-19T14:19:18Z")

</div>

Hello, Mr. @glv .  
You are very kind to solve my issue that puzzles me for awhile.

Thank you for your reference, you have a wonderful day, sir.

I made 3 adjustment, and my code works.

I leave my example here, in case someone need same help.

Main\_tab.pyde

```auto
from Setups import Walker

def setup():
    size(800,800)
    background(255)
    global w
    w = Walker(40, 40)
    # Calls out Walker function that previously defined
    # def __init__ (self, posx, posy):
    
def draw():
    w.step()
    w.display()

```

Setups.py

```auto
class Walker:
    def __init__ (self, posx, posy):
        self.posx = width / 2
        self.posy = height / 2
    
    def display(self):
        stroke(0)
        point(self.posx, self.posy)
        
    
    def step(self):
        direction = int(random(4))
        print(direction)

        if direction == 0:
            self.posx += 1
            
        elif direction == 1:
            self.posx -= 1
        elif direction == 2:
            self.posy += 1
        else:
            self.posy -= 1
        
        self.posx = constrain(self.posx, 0, width - 1)
        self.posy = constrain(self.posy, 0, height - 1)

```
