Hey, I am trying to get this 8-directional movement to work, but I have it wrong and would like to know a way of doing it without having to check each and every key combination (up + left, up + right, up + down, etc.).
Here’s what I have so far:
own = cont.owner key = logic.keyboard.events wkey = key[events.WKEY] skey = key[events.SKEY] akey = key[events.AKEY] dkey = key[events.DKEY] own["speed"] = 4.0 own["x"] = (-akey + dkey) / 2 own["y"] = (-skey + wkey) / 2 if ((akey + wkey) or (dkey + wkey) or (akey + skey) or (dkey + skey))/2 == 2: own["x"] /= 2**0.5 own["y"] /= 2**0.5 print(own["x"], own["y"]) own.setLinearVelocity([own["x"] * own["speed"], own["y"] * own["speed"], 0], False)
^this doesn’t work as intended.
My idea is essentially to use the int values returned by logic.keyboard.events to determine the direction of the player; then I want to check for diagonal movement and change the values of both the x and y to 2**0.5 (pythag theorem) – but in doing so without the annoying line “(akey + wkey) or (dkey + wkey) or (akey + skey) or (dkey + skey)” (it’s not fundamentally essential for it to work, I’d just like to know a simpler way).