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 :)