This forum is full of examples how to accept key presses (or mouse clicks) without counting time between presses. Like this:
from direct.showbase.DirectObject import DirectObject
class Controls(DirectObject):
def __init__(self):
self.keyMap = {"run":0}
self.accept("space", self.setKey, ["run", 1])
self.accept("space-up", self.setKey, ["run", 0])
def setKey(self, key, value):
self.keyMap[key] = value
............................................................
if controls.keyMap["run"]:
RunFast() # Check whether running is allowed by other conditions and do run
This code is suitable when you, for example, want the controlled actor to run somewhere.
If you what your actor to jump, this code is not what you need. If you use it, your actor would jump every frame while the jump key is pressed. The following modification calls the function only once:
from direct.showbase.DirectObject import DirectObject
class Controls(DirectObject):
def __init__(self):
self.keyTMap = {"jump":0}
self.accept("time-space", self.setTKey, ["jump"])
def setTKey(self, key, value):
# Time between function calls (in seconds):
time = 0.3
if value - self.keyTMap[key] > time:
self.keyTMap[key] = value
JumpHigh() # # Check whether jumping is allowed by other conditions and do jump
This code does not accept the same key for the next time seconds after the key was pressed.
Pay attention, we accept not “space” but “time-space”. Also, “value” is supplied automatically, it’s the time when the key was pressed (in seconds).
EDIT: To accept another “time-space” event the player will need to release the space button first, and then press it again. Holding the space key does not send another “time-space” event.
EDIT2: Comments in the snippets are slightly edited.