Dinosaur Game Issues

Can anyone help me? My actor is not acting. Here is the code:

from direct.showbase.ShowBase import ShowBase
from direct.task import Task
from direct.actor.Actor import Actor
from random import randint

class Game(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)
        
        #self.disableMouse()
        
        self.scene = self.loader.loadModel("models/environment")
        self.scene.setScale(0.25, 0.25, 0.25)
        self.scene.setPos(-8, 42, 0)
        self.scene.reparentTo(self.render)
        
        self.rex = Actor("models/trex", {"eat":"models/trex-eat", "run":"models/trex-run"})
        self.rex.reparentTo(self.render)
        
        self.rex.loop("run")
        
        self.taskMgr.add(self.thirdPersonController, "ThirdPersonController")
        
        self.keyMap = {"front" : False, "back" : False, "left" : False, "right" : False}
        
        self.accept("w", self.updateKeyMap, ["front", True])
        self.accept("w-up", self.updateKeyMap, ["front", False])
        self.accept("a", self.updateKeyMap, ["left", True])
        self.accept("a-up", self.updateKeyMap, ["left", False])
        self.accept("s", self.updateKeyMap, ["back", True])
        self.accept("s-up", self.updateKeyMap, ["back", False])
        self.accept("d", self.updateKeyMap, ["right", True])
        self.accept("d-up", self.updateKeyMap, ["right", False])
        
        self.taskMgr.add(self.move, "move")
        
        babyDinos = {}
        bigDinos = {}
        
        for i in range(10):
            xpos = randint(-80, 80)
            ypos = randint(-80, 80)
            babyDinos["dino" + str(i)] = Actor("models/babyd", {"anim" : "models/babydani"})
            babyDinos["dino" + str(i)].setPos(xpos, ypos, 25)
            babyDinos["dino" + str(i)].reparentTo(self.render)
            babyDinos["dino" + str(i)].loop("anim")
            
        for i in range(10):
            xpos = randint(-80, 80)
            ypos = randint(-80, 80)
            bigDinos["dino" + str(i)] = Actor("models/bigd", {"anim" : "models/bigdani"})
            bigDinos["dino" + str(i)].setPos(xpos, ypos, 0)
            bigDinos["dino" + str(i)].reparentTo(self.render)
            bigDinos["dino" + str(i)].loop("anim")
        
    def updateKeyMap(self, key, value):
        self.keyMap[key] = value
    
    def move(self, task):
        if self.keyMap["front"]:
           self.rex.setY(self.rex, -1.25)
        if self.keyMap["left"]:
           self.rex.setH(self.rex, 1.25)
        if self.keyMap["back"]:
           self.rex.setY(self.rex, 1.25)
        if self.keyMap["right"]:
           self.rex.setH(self.rex, -1.25)        
        
        return Task.cont
        
    def thirdPersonController(self, task):
        self.camera.setX(self.rex, 1)
        self.camera.setY(self.rex, 40)
        self.camera.setZ(self.rex, 64)
        self.camera.setH(self.rex.getH() + 180)
        self.camera.setP(-45)
        return Task.cont

game = Game()
game.run()

Are you having this problem with “self.rex”, or with the other dinosaurs, or with both?

Offhand, “self.rex” looks fine. If you’re having a problem with it, what happens when you load both it and it’s animations into PView? Do the animations play there?

As to the other dinosaurs, the problem there may simply be that you’re losing your references to them.

You see, Actors have a quirk in that they require that you keep references to them in your code. If you don’t, they appear onscreen, but don’t animate.

Now, you’re putting your non-rex dinosaurs into dictionaries, which should work–save for the fact that the dictionaries that you’re using are local variables. (There’s no “self.” to attach them to the current instance of the class.) Local variables are discarded at the end of their scope (in this case the __init__ function), meaning that your references to the Actors are discarded, too.

If you simply make the dictionaries into instance variables–that is, put “self.” at their start–thus making them available throughout a given instance of the class, then you should retain the references and thus the Actors should act.

Thank you sooooooooooooooooooooooooooooooooo much! It worked!

1 Like

Now I am having a problem. My collision is not getting detected:

from direct.showbase.ShowBase import ShowBase
from direct.task import Task
from direct.actor.Actor import Actor
from random import randint
from panda3d.core import CollisionTraverser, CollisionHandlerPusher
from panda3d.core import CollisionNode, CollisionSphere, CollisionInvSphere

class Game(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)
        
        self.disableMouse()
        
        self.cTrav = CollisionTraverser()
        self.pusher = CollisionHandlerPusher()
        
        self.scene = self.loader.loadModel("models/environment")
        self.scene.setScale(0.25, 0.25, 0.25)
        self.scene.setPos(-8, 42, 0)
        self.scene.reparentTo(self.render)
        
        sceneBounds = self.scene.getBounds()
        sceneCenter = sceneBounds.getCenter()
        sceneRad = sceneBounds.getRadius()
        
        cNode = CollisionNode("environment")
        cNode.addSolid(CollisionInvSphere(sceneCenter, sceneRad * 0.7))
        sceneC = self.render.attachNewNode(cNode)
        
        self.rex = Actor("models/trex", {"eat":"models/trex-eat", "run":"models/trex-run"})
        self.rex.reparentTo(self.render)
        
        cNode = CollisionNode("rex")
        cNode.addSolid(CollisionSphere(0, 0, 0, 5))
        rexC = self.rex.attachNewNode(cNode)
        
        self.pusher.addCollider(rexC, self.rex)
        self.cTrav.addCollider(rexC, self.pusher)
        
        self.rex.loop("run")
        
        self.taskMgr.add(self.thirdPersonController, "ThirdPersonController")
        
        self.keyMap = {"front" : False, "back" : False, "left" : False, "right" : False}
        
        self.accept("w", self.updateKeyMap, ["front", True])
        self.accept("w-up", self.updateKeyMap, ["front", False])
        self.accept("a", self.updateKeyMap, ["left", True])
        self.accept("a-up", self.updateKeyMap, ["left", False])
        self.accept("s", self.updateKeyMap, ["back", True])
        self.accept("s-up", self.updateKeyMap, ["back", False])
        self.accept("d", self.updateKeyMap, ["right", True])
        self.accept("d-up", self.updateKeyMap, ["right", False])
        
        self.taskMgr.add(self.move, "move")
        
        self.babyDinos = {}
        self.bigDinos = {}
        
        for i in range(10):
            xpos = randint(-80, 80)
            ypos = randint(-80, 80)
            self.babyDinos["dino" + str(i)] = Actor("models/babyd", {"anim" : "./models/babydani"})
            self.babyDinos.get("dino" + str(i)).setPos(xpos, ypos, 5)
            self.babyDinos.get("dino" + str(i)).reparentTo(self.render)
            
            cNode = CollisionNode("babyd" + str(i))
            cNode.addSolid(CollisionSphere(0, 0, 0, 5))
            babydC = self.babyDinos.get("dino" + str(i)).attachNewNode(cNode)
            
            self.pusher.addCollider(babydC, self.babyDinos.get("dino" + str(i)))
            self.cTrav.addCollider(babydC, self.pusher)
            
            self.babyDinos.get("dino" + str(i)).loop("anim")
            
        for i in range(10):
            xpos = randint(-80, 80)
            ypos = randint(-80, 80)
            self.bigDinos["dino" + str(i)] = Actor("models/bigd", {"anim" : "./models/bigdani"})
            self.bigDinos.get("dino" + str(i)).setPos(xpos, ypos, 5)
            self.bigDinos.get("dino" + str(i)).reparentTo(self.render)
            
            cNode = CollisionNode("bigd" + str(i))
            cNode.addSolid(CollisionSphere(0, 0, 0, 5))
            bigdC = self.bigDinos.get("dino" + str(i)).attachNewNode(cNode)
            
            self.pusher.addCollider(bigdC, self.bigDinos.get("dino" + str(i)))
            self.cTrav.addCollider(bigdC, self.pusher)
            
            self.bigDinos["dino" + str(i)].loop("anim")
        
    def updateKeyMap(self, key, value):
        self.keyMap[key] = value
    
    def move(self, task):
        if self.keyMap["front"]:	
           self.rex.setY(self.rex, -1.25)
        if self.keyMap["left"]:
           self.rex.setH(self.rex, 1.25)
        if self.keyMap["back"]:
           self.rex.setY(self.rex, 1.25)
        if self.keyMap["right"]:
           self.rex.setH(self.rex, -1.25)        
        
        return Task.cont
        
    def thirdPersonController(self, task):
        self.camera.setX(self.rex, 1)
        self.camera.setY(self.rex, 40)
        self.camera.setZ(self.rex, 64)
        self.camera.setH(self.rex.getH() + 180)
        self.camera.setP(-45)
        return Task.cont

game = Game()
game.run()

By the way, it may be better to start a new thread for each issue–that way others searching for answers to similar problems will be more likely to find them.

As to this issue, have you tried making your collision-nodes visible? That may give us more information as to what’s going wrong.

To do this, simply call “show()” on the NodePaths holding your collision-nodes. Like this:

rexC.show()