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