我正在使用pygame制作图形界面。该程序通常运行速度很快,但每当我进行按键操作时,鼠标都会拒绝移动半秒钟。我迷失了想法,想知道是否有人能给我一些指导。
这是我的代码如何工作的一般意义,我认为这是顶级主循环,绑定或基础结构的问题,因为问题与每个键绑定函数一致:
#######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)
绑定字典在稍后定义,并且在将来的某个时间点,将在程序运行时由用户修改。 如果你有一个不相关的建议让我知道,我刚刚开始,任何pygame建议将不胜感激:)