Keyboard.

I make one game but I have problem with the code for the keyboard.It dosen`t work at all.

I have used keyboard input with my game so far without difficulty, so maybe I could help. Could you post the code here, and explain more in detail what you are trying to do, and what’s actually happening? I’ll take a quick look, and see if I can tell you what’s wrong.

Ok I will post ir later.And I work on laptop and my keyboard is “little” crappy.

import sys 
from direct.task import Task
from direct.showbase.ShowBase  import ShowBase 
from math import cos , sin ,pi 
class MyApp(ShowBase):
   
   def __init__(self):
         
	 self.accept('escape', sys.exit)
  	
	 self.accept('up', self.mods)
	 self.mods = self.environ.setPos(0,0,0) 
	 ShowBase.__init__(self)
     
	 self.environ = self.loader.loadModel("models/igrataegg")
	 self.environ.reparentTo(self.render)
	 self.environ.setScale(0.25, 0.25, 0.25)
	 self.environ.setPos(-8, 42, 0)
	 self.taskMgr.add(self.spinCameraTask, "SpinCameraTask" )
	    
	
   def spinCameraTask(self, task):
     angleDegrees = task.time * 6.0
     angleRadians = angleDegrees * (pi / 180.0)	 
     self.camera.setPos(50 * sin(angleRadians), 20.0 * cos(angleRadians), 30)
     self.camera.setHpr(angleDegrees, 0, 0)
     return Task.cont

app = MyApp()
app.run()

Ok.The problem is in line №11.What is the problem?

Ah, I believe I understand. If the problem is with this line:

    self.accept('up', self.mods) 

Then there it’s nothing major, just a few little problems. First, I assume that “up” means the up arrow key, right? That key generates an event called “arrow_up”, and not just “up”. You can find a list of all the events triggered by different keys here:

panda3d.org/manual/index.php … rd_Support

Also, the second argument in the accept() function has to be a method or a function, so you’ll need to create a second method called mods for changing the position.

Third, you’ve inserted all your own code above the code already there, but sometimes the order matters, so it’s best if you define that key after defining the model you’re using.

I’ve modified your code a little, and now it looks like this:

from math import pi, sin, cos 
from direct.showbase.ShowBase import ShowBase
from direct.task import Task
import sys

class MyApp(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)
 
        self.environ = self.loader.loadModel("models/environment")
        self.environ.reparentTo(self.render)
        self.environ.setScale(0.25, 0.25, 0.25)
        self.environ.setPos(-8, 42, 0)

        self.accept('escape', sys.exit) 
        self.accept('arrow_up', self.mods, [self.environ])

        self.taskMgr.add(self.spinCameraTask, "SpinCameraTask")

    def mods(self, model):
        model.setPos(0, 0, 0)

    def spinCameraTask(self, task):
        angleDegrees = task.time * 6.0
        angleRadians = angleDegrees * (pi / 180.0)
        self.camera.setPos(20 * sin(angleRadians), -20.0 * cos(angleRadians), 3)
        self.camera.setHpr(angleDegrees, 0, 0)
        return Task.cont
 
app = MyApp()
app.run()

You see how I did that with the accept key? First define the model; then define the accept function for the keyboard input; then define the method that changes the model’s position, and that is called by the accept() function. You could set the escape key at the start, if you wanted, but I find it’s best to have all the keyboard functions in one place.

Note that I passed self.environ to the mods function as another parameter; I found this code worked better.

When I run this with one of Panda’s default environment models, the code works fine. I am not sure whether it will work with your model, but it ought to, assuming everything with the model is normal.

Thank you!