我正在用 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 建议将不胜感激:)