Page 1 of 1

Pygame - user config

Posted: Tue Aug 12, 2014 5:27 pm
by dan3008
Hi guys
I'm writing a set of games for my pi, and I want to have player configurable controls.
I've not really got a clue how to handle it efficiently, I tried storing the controls in an array, and scanning it every time a key was pressed, but that didnt work very well. Even on my desktop, I was getting like 5 FPS...
my second option was to check each key every frame, even worse (obviously)

anyone done this and had any better luck than me?

thanks

dan

Re: Pygame - user config

Posted: Tue Aug 12, 2014 6:02 pm
by Sleep Mode zZ
It would be better if you could post the code. Otherwise people can only speculate on what could be wrong.

Configuring the controls should not have any effect unless you are reconfiguring them (and doing some calculations continually) during the gaming. The effective way would be to replace some default values with the users preferred values at the start of the program. That way there should be no difference in performance.

Re: Pygame - user config

Posted: Tue Aug 12, 2014 7:15 pm
by dan3008
I'll post the code when I get back to my pi. Its all on there at the moment...

Although, heres the example I'm following

Code: Select all

import pygame
from ConfigParser import SafeConfigParser

action2function = {
    "up": move_up,
    "down": move_down,
    "left": move_left,
    "right": move_right
}

config = SafeConfigParser()
config.read(CONFIG_DIRS)
key2action = config.items()

...
for event in pygame.event.get():
    if event.type == KEYDOWN:
        action2function[key2action[pygame.key.name(event.key)]]()
...

Re: Pygame - user config

Posted: Tue Aug 12, 2014 8:19 pm
by Sleep Mode zZ

Code: Select all

action2function[key2action[pygame.key.name(event.key)]]()
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. :(

Edit:

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.

Edit 2:

Also, does not your code result in error whenever a key is pressed down if that key is undefined in your key2action dictionary?

Re: Pygame - user config

Posted: Wed Aug 13, 2014 9:28 am
by dan3008
Sleep Mode zZ wrote: While you could get the same functionality using only one dictionary instead of two
Indeed I could, in fact, I have just this morning modified my original code to use only one dictionary. Thanks for pointing that one out to me :)
Sleep Mode zZ wrote: 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.
It should, but wasnt, that combined with the fact dropping to 1 dictionary increased my speed lead me to the solution. I was redefining my dictionary every loop. I'd made a silly error, where I'd copy and pased something at 2 AM, and never went back and checked it lol...
Sleep Mode zZ wrote: Also, does not your code result in error whenever a key is pressed down if that key is undefined in your key2action dictionary?
It does, but i've managed to catch the error and ignore it. not complicated