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

Re: [pygame] solving a few event problems



Brendan Becker wrote:
> 1) Is there an easy way to initialise, say, all joysticks at once?

your code with the loop is probably the easiest. 3 lines shouldn't be a
problem to initialize all system joysticks. note that in python you can
access the last element in a list with the index "-1", which makes your
loop a little simpler

def get_all_joysticks():
    sticks = []
    numsticks = pygame.joystick.get_count()
    for i in range(pygame.joystick.get_count()):
        j = pygame.joystick.Joystick(i)
        j.init()
        sticks.append(j)
    return sticks


just stick this code in a module like "pygame_convenience.py" and import
and use as needed.



> 2) I'm looking for an easy way to save events, like with a keymapper. For
> stance, think a controls configuration menu in a game, going through and
> customising your controls, where you click on a certain description of a
> control "Move Up" and then press the up key. Sure, I could save event.key's
> value to a file or something. But what if "move up" could be a keypress OR
> a buttonpress? I don't want to say:

hmm, i've had a pretty good idea on how to setup something like this. you
could create a function that compares event attributes from one event to
another.

def does_event_match(event, match):
    if event.type != match.type: return 0
    for name,val in match.__dict__.items():
        try:
            if event[name] != val: return 0
        except AttributeError:
            return 0
    return 1
def is_event_one_of(event, matches):
    for m in matches:
        if not does_event_match(event, m): return 0
    return 1


with this you can check if an event matches one type of event, or many
types of event...


savedevent = pygame.event.Event(KEYDOWN, key=K_DOWN)
multipleevents = [pygame.event.Event(KEYDOWN, key=K_UP),
                  pygame.event.Event(JOYBUTTONDOWN, button=1, joy=2)]

then easily check if an event matches one of these descriptions..

if does_event_match(event, savedevent): print 'DOWN'
if is_event_one_of(event, multipleevents): print 'EVENT'


for an easy way to gather user inputs into these simple events, you could
create a function that takes a list of event masks and turns regular user
events into these simple events to check with the previous functions. these
interesting events just use "dummy" values for interesting attributes.

interesting_events = (pygame.event.Event(KEYDOWN, key=0),
                      pygame.event.Event(JOYBUTTONDOWN, button=0, joy=0)
interesting_map = {}
for e in interesting_events:
    interesting_map[e.type] = e


def convert_event_to_simple_mask(event):
    interesting = interesting_map[event.type]
    if not mask: return None
    mask = Event(event.type)
    for attr in interesting.keys():
        mask.__dict__[attr] = event[attr]
    return mask


with this code you can easily convert any user input event into a normal
"savedevent" as we use above. the convert function will return None if the
users event should not be saved.

savedevent = convert_event_to_simple_mask(event)

this will also make sure we match things like the proper joystick number to
the proper saved event.




where you will run into trouble is with the joystick positions. it's a bit
tricky to get a "joystick_moved_up" type event from the jostick, even with
gamepad type joysticks. the joystick returns a stick "position" which is a
lot like a mouse position. you'll need to track the previous position and
state of the joystick to determine if it moved up or not.

i was able to do a great version of this joystick input in SolarWolf. if
you peek into it's input.py you'll see where it converts any pygame input
event into a simple UP, DOWN, LEFT, RIGHT, BUTTON, or RELEASE type events
that the game uses. of course solarwolf is a little simplish, since it
doesn't care when the user releases these directions or deal with diagonals.


ok then, hopefully there's enough advice in here to show you where to go
next :]


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