I'm playing around for the first time with pygame, I'd like to know if there is a way I can call a function on a whole class like so:
class Char():
def __init__(self,typ,sid):
self.type=typ
self.x=0
self.y=0
self.vx=0
self.vy=0
self.frame=0
self.sid=sid
self.bord = sheets[sid].get_rect()
self.rct = pygame.Rect((0,0),(self.bord.bottom,self.bord.bottom))
player = Char("player",0)
test = Char("test",1)
myfunct(#All Chars())
This is how I plan on implementing a few simple physics that I'd like to all apply to each Char(), full code here .
You could add them all to a list which is a class variable, like so:
class Char():
all = []
def __init__(self,typ,sid):
self.type=typ
self.x=0
self.y=0
self.vx=0
self.vy=0
self.frame=0
self.sid=sid
self.bord = sheets[sid].get_rect()
self.rct = pygame.Rect((0,0),(self.bord.bottom,self.bord.bottom))
self.all.append(self)
player = Char("player",0)
test = Char("test",1)
myfunct(Char.all)
And if you want to apply your function to each object individually, you can just use a map(myfunct, Char.all)
, but you probably knew that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.