[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] USEREVENT



> I've started playing a little with USEREVENT:s, but I'm not exactly sure
> as to what I can do with them. Could you please elaborate a little bit
> what I can do? Basically I'd like to inherit an Event class and mark it as
> a USEREVENT. The I'd internally keep my own event id:s, so that I could
> have a lot more (infinite in practice) than the 8 extra allowed by SDL
> (USEREVENT=24, max=32). I'd like to do something like this:

ok, the Event types are the only thing you can post on the stack.
they are actually python extension types, not classes that can
be derived from. still, they are plenty flexible to give you what
you want.

first thing, for the type id, for the main type id you pretty much
need to stick with the limited range of SDL USEREVENT's. but feel
free to add a second type id of your own, which you really can use
for an infinite amount of event types.

also, it may be more appropriate to just embed a function or class
into your Event, instead of a "type id" value that you send through
a big switch type block.

as for the dict/keyword-args initialization. in previous pygame 
releases, you could only create a new Event by passing a dictionary
containing all the properties and values for the event.

>>> props = {'source': self, 'time': time.get_ticks()}
>>> event = event.Event(USEREVENT, props)

now the dictionary method is a lot more flexible, but for most of
the time, a couple string property names are all you need. with the
new keyword arguments you can shorten this process to

>>> event = event.Event(USEREVENT, source=self, time=time.get_ticks())


remember that you can put anything you want into these dictionarys.
it can be simple variables, up to classes, class instances, even modules
can be passed through the Events. here's a mini example showing most of
these ideas...


## set up some contrived classes

GAMEEVENT = USEREVENT+1

class CustomEvent():
    def __init__ (self, type):
        self.type = type

    def getType (self):
        return self.type

    def callevent(self, screen):
        pass


class SomeEvent(CustomEvent):
    def __init__(self, image):
        CustomEvent.__init__(10)
        self.image = load_an_image(image)

    def callevent(self, screen):
        screen.blit(self.image, (0,0))


## meanwhile, in the game code...

if some_object.has_changed():
    event = pygame.event.Event(GAMEEVENT, object=SomeEvent('changed.png'))
    pygame.event.post(event)


## later on, in the event queue reading code...

event = pygame.event.wait()
    if event.type == GAMEEVENT:
        event.object.callevent(gamescreen)




just small sampling there, just remember that you really can pass
any type of python object into the event queue, and also remember
that using 'event id types' is not really the best way to handle
things in python. it is easy to add your own "secondary" type id
if you want to, but first i'd sit and think about how i could solve
the problem without them, then decide which method would work better.



____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org