Check Keypress and Collisions

Hi everybody, i have two question.
First, I wrote a code for check keypress, but don’t works. What’s wrong, please tell me. My game runs, but when I press Left Arrow, nothing happens. My code is here.

class character:
   def __init__(self):
      self.accept('arrow_left', self.Look)
      
   def Look(self):
    sys.exit

Second, Anyone can tell me the basic collisions with basic codes?

Maybe your character class should inherit from DirectObject?
About the collisions, what’s the specific question? I guess the manual is a good starting point. IIRC it has some nice snippets =)

It’s sys.exit(), not simply sys.exit. But yes, you also need to inherit from DirectObject for “accept” to work.

How to inherit from DirectObject?

Anyone can answer me?

In Python, I believe that inheritance is specified by putting the name of the parent class in brackets after the class declaration (if you have more than one parent for a given class, separate them with commas.

For example, if you have a class A, and want to create a class B that inherits from A, you might declare B like so:

class B(A):
    def __init__(self):
        # Your initialisation code here
class character(DirectObject.DirectObject):
   def __init__(self):
      self.accept('arrow_left', self.Look)
      
   def Look(self):
    sys.exit()

I tried like this before your answers, but that’s didn’t work, nothing happens.

You must use:

class Blah(DirectObject.DirectObject):
      def __init__(self):
           DirectObject.DirectObject.__init__(self)

Otherwise, DirectObject.DirectObject won’t be initialized and you won’t be able to accept;

I got this error when run.

IndentationError: unindent does not match any outer indentation level

**** End of process output ****

You need to get in the habit of using a consistent indentation, since Python uses indentation as part of the syntax. For example, if you use 3 spaces of indentation on one line and 4 spaces on the next, this will result in an IndentationError like the one you’ve gotten. The Python style guide recommends using 4 spaces (per level) for indentation.

How do i use consistent indentation. And sorry for my questions, I am newbie on Panda3D and Python.

In general, when a line ends with a colon:
it means that the next section of code should be indented for a particular reason.
Any code that comes after that section would be
un-indented to one of the previous levels of indentation.

This page might help: http://www.diveintopython.net/getting_to_know_python/indenting_code.html

Also, if nothing happened when running your earlier code, the problem may be that you forgot to instantiate the character() class before calling run().

I would recommend picking up some basic Python skills before attempting to get familiar with Panda3D; this will make it a lot easier for you to learn Panda3D.

Hmm, can you do that on my code(sorry)?

import direct.directbase.DirectStart
from direct.interval.IntervalGlobal import *
from pandac.PandaModules import *
from direct.showbase.DirectObject import DirectObject
from direct.showbase import DirectObject
from panda3d.core import *
import sys

base.disableMouse

scene=loader.loadModel("models/environment")
scene.reparentTo(render)
scene.setScale(0.025,0.025,0.025)
scene.setPos(0,0,0)


s = loader.loadModel('smiley')
s.reparentTo(render)
s.setH(-90)
s.hprInterval(10,Vec3(180,0,0)).start()  

class character(DirectObject.DirectObject):
     def __init__(self):
 DirectObject.DirectObject.__init__(self)
 self.accept('arrow_left', self.Look)
    

def Look(self):
       sys.exit()
       
run()

put () after baee.disableMouse and indent the lines that belong to init of Look

I don’t understand indenting, anybody can tell detailed?

Indenting in Python is used to group statements, for example into a function, class, if-statement or for-loop.

If you’re completely new at Python, it’s better that you learn it for a bit before jumping into Panda3d. The official tutorial is a good start: docs.python.org/2/tutorial/

As for the keypress isssue, I would avoid inheriting DirectObject entirely: it is good practice to favour composition over inheritance anyway.

So first, import the class:

from direct.showbase.DirectObject import DirectObject

Then you create a DirectObject:

keyboard_manager = DirectObject()

You can then use this object to register key strokes using the accept method:

keyboard_manager.accept("escape", sys.exit)  

Of course, you can also use your own methods insteadd of just sys.exit. Suppose I want to call self.do_something() if the “a” key is hit:

keyboard_manager.accept("a", self.do_something)  

You can also provide arguments to the method via the extraArgs argument.

Hope this has helped!

Thanks for reply, but didn’ t work for me :/.

At the risk of being a broken record, you really should become more familiar with Python first. It shouldn’t take more than a week to get a good grasp of proper Python syntax. Python is a fairly popular language and there are many great resources available for free online. You won’t be able to learn either Python or Panda3D very well if people are fixing your code for you and you are not learning why it was broken.

Anybody haves a example for only this question?