script problem

i added some self.accept lines but then it crashes. the error from the console should speak for itself
console

C:\Documents and Settings\Ben\FPS_template>DirectStart: Starting the game.
Warning: DirectNotify: category 'Interval' already exists
Known pipe types:
  wglGraphicsPipe
(3 aux display modules not yet loaded.)
:util(warning): Adjusting global clock's real time by 0.778319 seconds.
:util(warning): Adjusting global clock's real time by -0.440856 seconds.
Traceback (most recent call last):
  File "test.py", line 146, in ?
    run()
  File "C:\Panda3D-1.3.2\direct\src\showbase\ShowBase.py", line 2028, in run
    self.taskMgr.run()
  File "C:\Panda3D-1.3.2\direct\src\task\Task.py", line 839, in run
    self.step()
  File "C:\Panda3D-1.3.2\direct\src\task\Task.py", line 787, in step
    self.__stepThroughList(taskPriList)
  File "C:\Panda3D-1.3.2\direct\src\task\Task.py", line 721, in __stepThroughLis
t
    ret = self.__executeTask(task)
  File "C:\Panda3D-1.3.2\direct\src\task\Task.py", line 644, in __executeTask
    ret = task(task)
  File "test.py", line 98, in gameLoop
    if self.keys["deaccel"]:
KeyError: 'deaccel'

script
f

rom pandac.PandaModules import loadPrcFileData
loadPrcFileData("", "show-frame-rate-meter 1") 

from pandac.PandaModules import WindowProperties


from direct.showbase.DirectObject import DirectObject # To listen for Events
from direct.task import Task # To use Tasks 
from direct.actor import Actor # To use animated Actors
from pandac.PandaModules import Vec3,Vec4
import direct.directbase.DirectStart
from pandac.PandaModules import ConfigVariableString 
import sys
import math 

import sys




class World(DirectObject):
  def __init__(self):

        props = WindowProperties()
        props.setCursorHidden(True)
        base.win.requestProperties(props)


	base.setBackgroundColor(0, 0, 0)    
	base.disableMouse()                 
	camera.setPos ( 0, 0, 45 )          
	camera.setHpr ( 0, 0, 0 )

	self.heading = 180
	self.pitch = 0
	
	self.keys = {"turnLeft" : 0, "turnRight": 0,
				 "accel": 0, "fire": 0}

	self.accept("escape", sys.exit)            #Escape quits
	#Other keys events set the appropriate value in our key dictionary
	self.accept("a",     self.setKey, ["turnLeft", 1])
	self.accept("a-up",  self.setKey, ["turnLeft", 0])
	self.accept("d",    self.setKey, ["turnRight", 1])
	self.accept("d-up", self.setKey, ["turnRight", 0])
	self.accept("w",       self.setKey, ["accel", 1])
	self.accept("w-up",    self.setKey, ["accel", 0])
	self.accept("s",       self.setKey, ["deaccel", 1])
	self.accept("s-up",    self.setKey, ["deaccel", 0])


	self.gameTask = taskMgr.add(self.gameLoop, "gameLoop")

	self.gameTask.last = 0         

	# create dummy cam to which the gun can be attached
	self.c_node = render.attachNewNode("camera_dummy_node") 
	
	#Position the camera dummy node. 
	self.c_node.setPos(-5, -10, 8)
	
	# Set agles
	self.c_node.setHpr(0, 0, 0)
	  
	# Attach the camera to the dummy node. 
	camera.reparentTo(self.c_node)
	
	# Position the camera 
	camera.setPosHpr(Vec3(0,0,0),Vec3(0,0,0))

	self.prevtime = 0


	self.loadModels()
	
  def setKey(self, key, val):
	  self.keys[key] = val

  def loadModels(self):


	environ = loader.loadModel("models/environment") # using the model from the panda folder
	environ.reparentTo(render)
	environ.setScale(1,1,1)
	environ.setPos(0,0,0)

  def gameLoop(self, task):
	self.elapsed = task.time - self.prevtime
	if self.keys["turnLeft"]:

		self.turn(-1)
	if self.keys["turnRight"]:

		self.turn(1)
	if self.keys["accel"]:
		self.walk()

	if self.keys["deaccel"]:
        
		self.walk()

	self.MouseTask()
		
	self.prevtime = task.time
		
	return Task.cont

	
  def turn(self,dir):
	  elapsed = self.elapsed
	  strafe = self.c_node.getNetTransform().getMat().getRow3(0)
	  strafe.setZ(0)
	  strafe.normalize()
	  self.c_node.setPos(self.c_node.getPos() + strafe*(elapsed*45*dir))
  

  def walk(self):
	  elapsed = self.elapsed
	  backward = self.c_node.getNetTransform().getMat().getRow3(1)
	  backward.setZ(0)
	  backward.normalize()
	  self.c_node.setPos(self.c_node.getPos() + backward*(elapsed*45))
    
  def back(self):
	  elapsed = self.elapsed
	  backward = self.c_node.getNetTransform().getMat().getRow3(1)
	  backward.setZ(0)
	  backward.normalize()
	  self.c_node.setPos(self.c_node.getPos() + backward*(elapsed*45))

  def MouseTask(self):
	  try:
		x = base.win.getPointer(0).getX()
		y = base.win.getPointer(0).getY()

		if base.win.movePointer(0, 100, 100):
				self.heading = self.heading - (x - 100)*0.2
				self.pitch = self.pitch - (y - 100)*0.2
		if (self.pitch < -45): self.pitch = -45
		if (self.pitch >  45): self.pitch =  45
		self.c_node.setHpr(self.heading,self.pitch,0)
	  except: pass


w = World()
run()

By the looks of it, it is just an indenting issue.

The code for init is as follows:

props = WindowProperties() 
props.setCursorHidden(True) 
base.win.requestProperties(props)

What follows it is outside of your init function, which includes your accept calls. Indenting this properly should fix it.

No, it’s not the indentation. It’s the incomplete dictionary keys.
the erroneous line :

   if self.keys["deaccel"]:

but, you don’t have key “deaccel” in the dictionary.

   self.keys = {"turnLeft" : 0, "turnRight": 0,
             "accel": 0, "fire": 0}

You should put “deaccel”:0 in there too, if you want to be able to use it.

Actually, when you press s/s-up, DirectObject would run the given function, which is setKey, with argument [“deaccel”, 0/1].
What setKey function does

  def setKey(self, key, val):
     self.keys[key] = val

is changing the key value of your keys dictionary above, so clearly this is what actually happen :

self.keys["deaccel"] = 0/1

Of course there is automation possible to simplify this, for example :
in init :
you can even have some alternative keys for a single action

   self.simultaneousKeyPressMap = {
        'moveFwd'   : [[
                        'w',
                       ], self.moveForward],
        'moveBwd'   : [[
                        's',
                       ], self.moveBackward],
        'moveLeft'  : [[
                        'a',
                       ], self.moveLeft],
        'moveRight' : [[
                        'd',
                       ], self.moveRight],
   }
   self.activateSimultaneousKeys()

functions :

  def activateSimultaneousKeys(self):
      self.keyPressed={}
      for k in self.simultaneousKeyPressMap:
          self.keyPressed[k]=0
          for alternate in self.simultaneousKeyPressMap[k][0]:
              self.accept(alternate, self.setKeyPressed, [k,1])
              self.accept(alternate+'-up', self.setKeyPressed, [k,0])

  def setKeyPressed(self,key,value):
      self.keyPressed[key]=value

in your game loop :

   for k in self.simultaneousKeyPressMap:
       if self.keyPressed[k]:
          self.simultaneousKeyPressMap[k][1]

PS. : I use different dictionary name and functions name in there, don’t be confused.

okay its working but i get weird black boxes flickering on the screen

oh nevermind. that was because of some desktopX widgets interfering with the engine. :blush: