Hi All,
So ive been reading up on alot of theory recently about game development and programming etc.
I thought I’d get back into the BGE and try my hand at python, I tried to make a super simply movment script but everytime I use it, my object just spins out of control. It should move similar to the asteroids ship of old.
I’ve tried various fixes and would honestly just like another set of eyes on the code to help me out.
############################################################
### Make a super simple block game to learn some python ###
############################################################
import GameLogic
import random
############################################################
cont = GameLogic.getCurrentController()
playerModel = cont.owner
move = cont.actuators["move"]
pressUp = playerModel.sensors["up"]
pressRight = playerModel.sensors["right"]
pressDown = playerModel.sensors["down"]
pressLeft = playerModel.sensors["left"]
pressShift = playerModel.sensors["shift"]
pressCNTRL = playerModel.sensors["cntrl"]
upPressed = False
rightPressed = False
downPressed = False
leftPressed = False
playerRotation = playerModel.localOrientation.to_euler()
playerRotationX = playerRotation.x
playerRotationY = playerRotation.y
playerRotationZ = playerRotation.z
############################################################
def changeDirection(y, x , z):
rotation = [ y, x, z]
playerModel.applyRotation( rotation , True )
def updatePosition():
randomDirection = random.randint(1,360) ## 360 degree
newRotation = playerRotationZ + randomDirection
changeDirection(0,0,newRotation)
speed = playerModel["vel"]
if pressUp.positive or pressRight.positive or pressLeft.positive or pressDown.positive:
if pressUp.positive: ## this will be our "Thrust" button
playerModel["vel"] = playerModel["vel"] + 0.01
if pressDown.positive: ## our decrease speed
playerModel["vel"] = playerModel["vel"] - 0.01
speed = playerModel["vel"]
if pressRight.positive: #turn Right
playerRotationZ = playerRotationZ + 0.1
if pressLeft.positive: #turn left
playerRotationZ = playerRotationZ - 0.1
speed = playerModel["vel"]
move.dLoc = [0, speed, 0.0]
move.dRot = [0.0, 0.0, playerRotationZ]
cont.activate(move)
print(playerModel.getPosition())