Nature of Code (Random Walker) using Python

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:

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

from Setups import *

w = 0

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

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

Setups.py

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

)

Hello @Hsu,

This worked with Processing 3.5.4 in Python mode:

Reference:

:)

1 Like

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

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

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)
1 Like