Time for action – creating and using a key map
The first step to using a key map is to actually create the dictionary that serves the purpose. Right after the line that calls
self.cycle.setPos()method in the__init__method, insert the following block of code to create the dictionary:self.keyMap = {"w" : False, "s" : False, "a" : False, "d" : False}Now we need to register events for the four keys in our key map. Replace the current
self.accept()call, with the following code:self.accept("w", self.setKey, ["w", True]) self.accept("s", self.setKey, ["s", True]) self.accept("a", self.setKey, ["a", True]) self.accept("d", self.setKey, ["d", True]) self.accept("w-up", self.setKey, ["w", False]) self.accept("s-up", self.setKey, ["s", False]) self.accept("a-up", self.setKey, ["a", False]) self.accept("d-up", self.setKey, ["d", False])Next, we need to change our
setKey()method to look like this:def setKey(self, key, value...