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 =)
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
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.
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.
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.
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.
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.