0
$\begingroup$

i'm beginner in python. I have follow Goran Milovanovic's tutorial about "key combo", in his tutorial he is using a text object which is just changing the text. I want to make a combo like double tap to make the object move. it's a been a while I'm playing on it, but still the code ain't working. here's the code:

from bge import logic, events
import mathutils
import time

class Combo:


def __init__(self, delta, lst_keys):
    self.delta = delta
    self.keys = lst_keys

    self.cidx = 0
    self.time_last = 0

    self.complate = False

def main(self, hit_key):

    self.complate = False

    time_now = time.time()
    delta = time_now - self.time_last
    self.time_last = time_now
    print(time_now)

    if hit_key == self.keys[self.cidx] and delta < self.delta:
        self.cidx += 1
        if self.cidx == len(self.keys):
            self.complate = True
            self.cidx = 0
    elif hit_key == self.keys[0]:
        self.cidx == 1
    else:
        self.cidx == 0


def main():

cont = logic.getCurrentController()
own = cont.owner

keyboard = logic.keyboard.events
hold = logic.KX_INPUT_ACTIVE    
click = logic.KX_INPUT_JUST_ACTIVATED
w = events.WKEY

moveW = own.worldOrientation.to_quaternion() * mathutils.Vector((0,1,0)) * 0.2          

up = Combo(1, keyboard[w]*2)
moveUp = own.localPosition = own.localPosition + moveW 
combo_label = [(up, moveUp)]

for c, l in combo_label:
    c.main(hold)
    if c.complate:            
            own = moveUp  
$\endgroup$
1
$\begingroup$

Think about the design of such a system.

Requirements

  • key pressed once -> start timer AND wait for second key press

  • key pressed a second time AND wait for second key press -> do operation AND stop timer

  • timer exceeds -> stop timer

(I guess this is what you want to achieve. If you want something else let me know).

Design

This can be easily achieved via logic bricks. There is no point where you need Python.

Lets break down the requirements.

Sensing a key press

is simple: keyboard sensor

Running a timer

there are several options such as

  • delay sensor

  • timer property with property sensor

  • property with property actuator and property sensor

The timer property gives the time in seconds while the other options use number of frames.

Wait for second key press

There are several options too.

  • as long as the timer is running, you are waiting ... otherwise not. This could be sensed with a property change sensor

  • remember the wait state (either use build in state system or use a property to remember the state)

Do operation

This is up to you

Timer exceeds

either the delay sensor pops up or you measure this with a property sensor (greater than mode).

Example

I choose a delay sensor and the build-in state machine.

State "idle" (waiting for first key press)

enter image description here

State "waiting" (waiting for second key press)

enter image description here

Be aware when you have other "breaking" events beside timeout you need to add them too (e.g. pressing another key might break the combo).

Remarks

Yes, you can implement the same architecture with Python. There are no additional benefits.

$\endgroup$
  • $\begingroup$ thanks for your answer, but I want to make it in python. I'll try using delay sensors since I just know about it. I'll also learn state in python. $\endgroup$ – Rendra Rukmono Jun 15 '16 at 6:37
  • $\begingroup$ it is the exact same design. If you implement it in Python or not does not matter. See section Remarks $\endgroup$ – Monster Jun 19 '16 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.