Code: Select all
So, in the loop you get the name of the key pressed from pygame. Then look it up in key2action dictionary. Then with the item returned from the key2action dictionary you use it as a key to get an item from the action2function dictionary. That item is the name of a function that then gets called.
While you could get the same functionality using only one dictionary instead of two, I doubt that that would solve the slowness of the program. I would expect python dictionaries to be fast enough for this not introducing such a slowdown into the program. Have you tested how many fps you can get without the double dictionary approach? Could the slowdown be somewhere else in the loop?
Sorry for not being more helpful.
The dictionary lookups will be done only if some key is pressed. If that would be behind the slowdown, then pressing keys would slow down fps from what it is without pressing any keys. If the program do not behave like that, the reason for the poor fps should be somewhere else.
Also, does not your code result in error whenever a key is pressed down if that key is undefined in your key2action dictionary?