Blender Game Engine python loop issue I have a python script that moves a sphere around. This is basically the start of a snake game. The problem is that the ball stops moving if the keys aren't constantly held down. What I need is for the ball to keep moving in the last direction given by the keys. I think it has something to do with the fact that the script is restarted and the variables reset each tick...

This is the code I'm using:

import bge
U = False
D = False
L = False
R = False
def main():
    global U
    global D
    global L
    global R
    cont = bge.logic.getCurrentController()
    own = cont.owner
    keyboard = bge.logic.keyboard

    wKey = bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.WKEY]
    sKey = bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.SKEY]
    aKey = bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.AKEY]
    dKey = bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.DKEY]

    if wKey and (D == False): 
        U = True
        D = False
        L = False
        R = False
    elif sKey and (U == False): 
        U = False
        D = True
        L = False
        R = False
    elif aKey and (R == False): 
        U = False
        D = False
        L = True
        R = False
    elif dKey and (L == False): 
        U = False
        D = False
        L = False
        R = True
    if U:
        own.applyMovement([0,0.2,0],True)
    if D:
        own.applyMovement([0,-0.2,0],True)
    if L:
        own.applyMovement([-0.2,0,0],True)
    if R:
        own.applyMovement([0.2,0,0],True)
main()


This is a link to the file on my Google Drive:

https://drive.google.com/open?id=0B6_5f6jmSaHOUW1wTGVlRXFkVUE
I'd suggest if  you are going to use global variables, then run script as a module, otherwise would need set up Boolean properties on object and use them to keep tabs on U, D, L, R. 



Slight change to code, the controller is automatically passed to the module, which needs to have the .py extension.

import bge
U = False
D = False
L = False
R = False
def main(cont):
    global U
    global D
    global L
    global R

    own = cont.owner

    keyboard = bge.logic.keyboard

    wKey = bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.WKEY]
    sKey = bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.SKEY]
    aKey = bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.AKEY]
    dKey = bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.DKEY]

    if wKey and (D == False): 
        U = True
        D = False
        L = False
        R = False
    elif sKey and (U == False): 
        U = False
        D = True
        L = False
        R = False
    elif aKey and (R == False): 
        U = False
        D = False
        L = True
        R = False
    elif dKey and (L == False): 
        U = False
        D = False
        L = False
        R = True
    # test print 
    print(U, D, L, R)
    if U:
        own.applyMovement([0,0.2,0],True)
    if D:
        own.applyMovement([0,-0.2,0],True)
    if L:
        own.applyMovement([-0.2,0,0],True)
    if R:
        own.applyMovement([0.2,0,0],True)