0

I am making a graphical interface with pygame. The program is generally running fast, but whenever I make a keypress, the mouse refuses to move for half a second. I am lost for ideas, wondering if anyone could give me some direction.

Here's a general sense of how my code works, I do think it's a problem with the top level main loop, the bindings or the infrestructure because the problem arises consistently with every key-bound function:



#######PERPETUAL ACTIONS#
_perpetual_actions = []

def start_doing(function,insertAt=None):
    global _perpetual_actions
    if insertAt==None:
        insertAt=len(_perpetual_actions)
    _perpetual_actions.insert(insertAt,function)

def stop_doing(function):
    global _perpetual_actions
    if _perpetual_actions.count(function):
        _perpetual_actions.remove(function)

def do_perpetual_actions():
    global _perpetual_actions
    for action in _perpetual_actions:
        action()

def print_perpetual_actions():
    global _perpetual_actions
    for action in _perpetual_actions:
        print action.__name__


######BINDING INFRASTRUCTURE######## 

_bindings_dict = { }

def get_action(key, mod):
    try:
        action = _bindings_dict[(key,mod)]
    except KeyError:
        action = None
    return action

def get_binding(action_to_find):
    for event, action in _bindings_dict.iteritems():
        if action == action_to_find:
            return event
    return None

def bind(event, action):
    _bindings_dict[event] = action

def unbind(event):
    _bindings_dict.pop(event)

def swap_bindings(newBindings):
    _bindings_dict = newBindings




####MAIN LOOP####
if '-t' in sys.argv:
    test()
while True:
    for event in pygame.event.get():
        if event.type == MOUSEMOTION: 
            mousex, mousey = event.pos 
            mouse.x = mousex 
            mouse.y = mousey 
        elif event.type == KEYDOWN:
            action = get_action(event.key,event.mod)
            if not action == None:
                action()
        elif event.type == KEYUP:
            action = get_action(-event.key,event.mod)
            if not action == None:
                action()
        elif event.type == QUIT:
            quit()
    do_perpetual_actions()
    clock.tick(40)

The binding dictionary is defined later and at some point in the future, will be modifiable by the user while the program is running. If you have an unrelated recommendation let me know, I'm just starting out and any pygame advice would be appreciated :)

hedgehogrider
  • 1,168
  • 2
  • 14
  • 22
  • 1
    Is the elif on the 3rd line in the main loop in your original code too? – cerealy Oct 30 '12 at 23:16
  • Ah, crap, I removed an if blcok for testing and forgot to add it back in. Thanks cerealy, nice catch. – hedgehogrider Oct 30 '12 at 23:21
  • 1
    one thing that can speed up performance if defining a `main` function and using `if __name__=="__main__": main()` at the end of your code, this will push your main loop out of global scope and make it run faster, might semi-fix your problem. – Serdalis Oct 31 '12 at 00:59
  • Not really that big of a deal but you dont need to write `if not action == None`, only `if action:` is required. – Willy Oct 31 '12 at 14:38
  • Does the lag take place when you press any other (not mapped) key? – pmoleri Oct 31 '12 at 18:44
  • @pmoleri Yes, it does. I'm actually starting to suspect that this is a problem with my windowing system, as (and I can't believe I'm just starting to notice) the lag is present throughout usage of all programs on this computer. I'm traveling south america and I haven't gotten a chance to test on another computer yet! Thanks to all for looking through and being helpful :) – hedgehogrider Nov 02 '12 at 19:51
  • checking for mouse movement in the event for loop, is not a good idea, if you only need to the mouse coordinates on a mouse click, i suggest getting them in the click method. – Bartlomiej Lewandowski Nov 11 '12 at 22:51
  • i had to put a window up but it works for me... – alexpinho98 Jun 29 '13 at 11:03

0 Answers0