p5
p5 copied to clipboard
Interacting with p5py in a more OO fashion
Hello! Really excited about what you all are doing here.
I'm trying to do some catch up to understand the internals (haven't looked at the project since it was on pyglet), and figure out how to interact with the library in a more OO fashion. It's tricky because so much of the code lives in a 'global' fashion. This is very difficult because all of the shape primitives (circle, line, etc...) are all functions, and not classes. So trying to do things like set the fill of a shape isn't as easy as calling a setter/getter on a class. How do I write my own classes to allow it to hold it's own state (maybe return closures...)?
Here is some sudo code I wrote to try and achieve what I'm after. This actually works MINUS the fill/stroke setter.
import p5
from random import randrange
frame = 0
width = 200
height = 200
class Circle():
def __init__(self, location=(0,0), r=10):
self.location = location
self.r = r
self.c = p5.circle
self._make()
def _make(self):
self.set_fill() # comment these out and it works
self.set_stroke() # comment these out and it works
return self.c(self.location, self.r)
def set_fill(self, fill:tuple=(255,10,10)):
self.c.fill = fill
def set_stroke(self, stroke:tuple=(10,10,255)):
self.c.stroke = stroke
def setup():
p5.size(width,height)
def draw():
for i in range(10):
Circle(location=(randrange(0,width), randrange(0,height)), r=10)
p5.background(255)
p5.run(setup, draw)
I am familiar with the p5/processing paradigm of holding shape attributes in a class, then just applying those attributes in the draw loop.... But I'd like the classes to hold the ACTUAL shape. Not just it's attributes (position, size, color, etc...)
Maybe I'm approaching this the wrong way... or it's just not possible. Any thoughts would be great!!
UPDATE: Just stumbled across https://github.com/p5py/p5-ideas ... Maybe this should go in there?
It would be possible, this is really pushing p5.py into game engine territory, rather than drawing and graphics. If you want to have persistent objects, feel free to use p5.py as a base, though I don't think this will or should be natively integrated.
I am familiar with the p5/processing paradigm of holding shape attributes in a class, then just applying those attributes in the draw loop.... But I'd like the classes to hold the ACTUAL shape. Not just it's attributes (position, size, color, etc...)
That is indeed an interesting thought and will probably require rewriting large parts of the internal code. But I can see this potentially being useful, so if someone is up for it, we can have a branch with a more-OO version of the code.
It would be possible, this is really pushing p5.py into game engine territory, rather than drawing and graphics. If you want to have persistent objects, feel free to use p5.py as a base, though I don't think this will or should be natively integrated.
I don't think it's an issue as long as it doesn't change the higher-level behavior for most-users. I can definitely imagine use-cases where one would like to have all shape attributes stored with the object.
Just stumbled across https://github.com/p5py/p5-ideas ... Maybe this should go in there?
Yep. If this is something that builds on the existing code-base, that's probably the place to put it.
Keeping this open as I would be interesting if people could come up with use-cases where is important and/or have arguments for/against.