Tetris - Problem with creating a 'jerky' movement

Hello,
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
Rasterizer.showMouse(True)


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


def main():
    wait_and_apply(tetrocube)


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:
            time.sleep(1)
            Move(object)
            a+=1
        else:
            a = 20
    
def Move(object):
    object.applyMovement([0, 0, -1], False)
    
main()

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.
etc.

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:
    own.applyMovement((0,0,-1),0)
    own['Timer']=120
else:
    own['Timer']=own['Timer']-1   
        



updated - now Timer=120 for python

equivalent completely, logic vs python

Attachments

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.