Trouble writing simply python bge script

Hey everyone. I am new to python and bge and am trying to write a simple script to move a block within bge using python. The block moves to the “a” (left button) and “d” (right button) keyboard buttons at the beginning but after one of the direction buttons is pressed, the other direction can only bring the block to a halt and not continue to move the block in the other direction. It might just be easier to take a look at the attached code (btw i havnt posted since they updated the website. is there a way to attach files anymore?).

import PhysicsConstraints
import GameLogic
# get the controller the script it attached to
controller = GameLogic.getCurrentController()
# get a list of sensors connected to the python controller
senList = controller.sensors
# get a list of actuators connected to the python controller
actList = controller.actuators
 
# get the keyboard sensor named leftsensor
l = senList["leftsensor"]
# get the keyboard sensor named rightsensor
r = senList["rightsensor"]
# get the motion actuator named moveleft
ml = actList["moveleft"]
# get the motion actuator named moveright
mr = actList["moveright"]
# l.positive == True means a key was pressed
# l.positive == False means a key was released
# Run indented code on when a key is pressed
if l.positive == True:
    # turn the motion actuator on
 controller.activate(ml)
# Run indented code on when d key is pressed
if r.positive == True:
    # turn the motion actuator on
 controller.activate(mr)

Thanks a lot. I greatly appreciate the help.

amartin7211

If i understood well what you want to get, you should use something like


if l.positive: #sensor.positive returns True or False, you don't need to write the ==
 if r.positive:
  controller.activate("the actuator that stops the cube*")
 else:
  controller.activate(ml)
elif r.positive:
 if l.positive:
  controller.activate("the actuator that stops the cube*")
 else:
  controller.activate(mr)
else:
 controller.activate("the actuator that stops the cube*")


  • in the actuator you can’t use a value of 0.000 to stop the cube, but you must set at least to 0.001

Hope it helps

what exactly do you mean by set the actuator to 0.001? what attribute of the motion actuator do i set to this value?