Tetris - Problem with creating a 'jerky' movement

I’m actually trying to reproduce the Tetris game while using the Blender Game Engine and some Python.
For now, I simply have a keyboard input to add a cube to the scene and that cube acts that way :

import bge
import Rasterizer
import time

cont = bge.logic.getCurrentController()
tetrocube = cont.owner # It's a Dynamic object. Btw, gravity = 0
# get the scene for mouse and other things
scene = bge.logic.getCurrentScene()
# get the keyboard
keyboard = bge.logic.keyboard

# get the mouse (I just like to have it when I'm testing my code)
mouse = bge.logic.mouse

# get the touch sensor
touch = cont.sensors["Collision"]

def main():

def wait_and_apply(object): # I don't know why but if I do (while touch.positive is False:), the loop overload.
    a = 0 # Corresponds to the number of movements (equal to the number of 'squares' crossed by the cube)
    while a<20:
        if touch.positive is False:
            a = 20
def Move(object):
    object.applyMovement([0, 0, -1], False)

But when I play the game, Blender doesn’t respond until the cube reached the bottom of the scene (= until touch.positive is True), so I can’t see that ‘jerky’ movement like in Tetris.
Does anyone can help me ?

Thanks in advance.

never never never ever call sleep()!

Separate User input.
Separate block selection/generation
Separate falling
Separate turning.
Separate hitting the ground.
Separate line completion detection.
Separate winning detection
Separate loosing detection.

Thank you for the quick answer.
I’ve also tried to use the Timer function from the threading module but it doesn’t seem to work in the Game Engine.
Perhaps I should try to do it with several properties.

I suggest you check theBGE guides first. It gives you some overview over the BGE internals.

import bge

cont = bge.logic.getCurrentController()
own = cont.owner
if own['Timer']==0:

updated - now Timer=120 for python

equivalent completely, logic vs python


Tetris.blend (433 KB)

Thanks for the code BluePrint, it works well.
I think I will avoid the while loop because it’s out of control and since the script loops itself, I will do it with if-statements.