1
$\begingroup$

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

$\endgroup$
0
$\begingroup$

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.

enter image description here

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)
$\endgroup$
  • $\begingroup$ Thank you for the help! I used your code and instructions, and it works, but the ball still stops when the keys aren't being pressed. I don't want the ball to be able to stop once it has started moving, only change directions. $\endgroup$ – arduinoob Jul 14 '16 at 15:01
  • $\begingroup$ Here is the file pasteall.org/blend/42712 It keeps moving for me.... ??? $\endgroup$ – batFINGER Jul 14 '16 at 15:38
  • $\begingroup$ Oh... Oops, I accidentally unchecked the always sensor... Never mind! Thanks again for the help! $\endgroup$ – arduinoob Jul 14 '16 at 15:48

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.