FIFE forums

Please login or register.

Login with username, password and session length
Advanced search  

News:

FIFE 0.4.0 has been released on 15th of January, 2017!

Author Topic: Little tip on key state handling  (Read 2067 times)

pensaku

  • Newbie
  • Posts: 2
    • View Profile
Little tip on key state handling
« on: January 28, 2011, 07:04:59 am »

self.keyBinds = {fife.Key.UP: "UP"}
self.keyState = {}
self.actionBind = {"UP": OnMoveUp}

def keyPressed(self, evt):
        keyval = evt.getKey().getValue()
        keystr = evt.getKey().getAsString().lower()
        self.keyState[keyval] = True

def Tick():
      for key in self.keyState:
            if self.keyState[key]:
                   self.actionBind[self.keyBinds[key]](args)

Not exactly like that but kind of like that...
Less tiresome than tons of if elses!
Logged

chewie

  • Developer
  • Full Member
  • *
  • Posts: 123
    • View Profile
    • zero-projekt.net
Re: Little tip on key state handling
« Reply #1 on: January 30, 2011, 11:15:57 am »

Yep, that´s a common technique to condense code in such cases - you can e.g. also use that method for FSM implementations or every other code parts which behave like "switch" checks.
« Last Edit: January 31, 2011, 02:09:12 am by chewie »
Logged