Пример #1
0
 def addCar(self):
     pos = self.spos[len(self.carlist) % len(self.spos)]
     tempvel = Velocity(500 - pos[0], 500 - pos[1])
     newcar = Car(pos[0], pos[1], tempvel.getD(), len(self.carlist) % 4)
     self.carlist.append(newcar)
     if self.index >= 0 and self.index == len(self.carlist) - 1:
         tempvel.setDM(newcar.model.getH(), -75)
         camera.setPos(newcar.model.getX() + tempvel.x, newcar.model.getY() + tempvel.y, newcar.model.getZ() + 40)
         camera.lookAt(newcar.model)
         camera.setP(camera.getP() + 5)
         newcar.makeCollisionSolid(base.cTrav, self.cHandler, self.index)
     elif self.index == 0:
         newcar.makeCollisionSolid(base.cTrav, self.cHandler, len(self.carlist) - 1)
     return newcar
Пример #2
0
    def move(self, task):
        if self.go:
            elapsed = task.time - self.prevtime
            for i in range(len(self.carlist)):
                self.carlist[i].move(elapsed)
                if self.carlist[i].hp <= 0:
                    self.carlist[i].smoke3.p.hide()
                    self.carlist[i].smoke2.p.hide()
                    self.carlist[i].smoke1.p.hide()
                    self.carlist[i].s1 = False
                    self.carlist[i].s2 = False
                    self.carlist[i].s3 = False
                    pos = self.spos[i % len(self.spos)]
                    self.carlist[i].model.setPos(pos[0], pos[1], 0)
                    tempvel = Velocity(500 - pos[0], 500 - pos[1])
                    self.carlist[i].model.setH(tempvel.getD())
                    self.carlist[i].vel.setXY(0, 0)
                    self.carlist[i].deaths += 1
                    self.carlist[i].hp = 100
                    if i == self.index:
                        self.explosionSound.play()

            if self.index >= 0 and self.index < len(self.carlist):
                tempvel = Velocity()
                tempvel.setDM(self.carlist[self.index].model.getH(), -75 * MULCAM)
                tempvel.addDM(self.carlist[self.index].vel.getD(), self.carlist[self.index].vel.getM() * -10 / MULCAM)
                camera.setPos(
                    self.carlist[self.index].model.getX() + tempvel.x,
                    self.carlist[self.index].model.getY() + tempvel.y,
                    self.carlist[self.index].model.getZ() + 40 + self.carlist[self.index].vel.getM() * -5 / MULCAM,
                )
                camera.lookAt(self.carlist[self.index].model)
                camera.setP(camera.getP() + 5)

            self.prevtime = task.time
        return Task.cont
Пример #3
0
class Car():
    """This is a car."""
    def __init__ (self, x=0, y=0, h=0):
        self.model = Actor("models/panda-model")
        self.model.reparentTo(render)
        self.model.setScale(.005)
        
        #things that matter
        self.model.setPos(x, y, 0)
        self.model.setH(h)
        self.vel = Velocity()
        self.hp = 100
        self.input = [False for i in range(5)]#left, right, up, down, space
        
        #taskMgr.add(self.move, "outtaThaWayImDrivingHere")
        #self.prevtime = 0
        
    def move(self, elapsed):
        #elapsed = task.time - self.prevtime
        
        #all these numbers need to be tested
        if self.input[0]:#left
            self.model.setH(self.model.getH() + elapsed * 200)#maybe multiply by speed?
        if self.input[1]:#right
            self.model.setH(self.model.getH() - elapsed * 200)
        if self.input[2]:#up
            self.vel.addDM(self.model.getH(), elapsed * 5)
            self.vel.setDM(self.vel.getD(), min(self.vel.getM(), 2))#speed cap
        if self.input[3]:#down
            self.vel.addDM(self.model.getH(), elapsed * -55)
            self.vel.setDM(self.vel.getD(), min(self.vel.getM(), 2))#speed cap
        self.vel.setDM(self.vel.getD(), self.vel.getM()*(1-.02-.18*self.input[4]))#friction
            
        self.model.setPos(self.model.getX() + self.vel.x, self.model.getY() + self.vel.y, 0)
        
        #self.prevtime = task.time
        #return Task.cont
Пример #4
0
class Car():
    """This is a car."""
    def __init__ (self, x=0, y=0, h=0, car=0):
        #mydir = os.path.abspath(sys.path[0])
        #mydir = Filename.fromOsSpecific(mydir).getFullpath()
        if car == 0:
            self.model = loader.loadModel("cars/bluecar.egg")
        elif car == 1:
            self.model = loader.loadModel("cars/redcar.egg")
        elif car == 2:
            self.model = loader.loadModel("cars/greencar.egg")
        else:
            self.model = loader.loadModel("cars/yellowcar.egg")
        #self.model = Actor("models/panda-model")
        self.model.reparentTo(render)
        #self.model.setScale(.005)
        self.model.setScale(5.7)
        
        #things that matter
        self.model.setPos(x, y, 0)
        self.model.setH(h)
        self.vel = Velocity()
        self.turn = 0
        self.hp = 100
        self.deaths = 0
        self.input = [False for i in range(5)]#left, right, up, down, space

        #Attach Smoke
        self.s1 = False
        self.s2 = False
        self.s3 = False
        self.smoke1 = smoke_emitter(self.model, 1, 1, 1)
        self.smoke2 = smoke_emitter(self.model, -1, 0, 1)
        self.smoke3 = smoke_emitter(self.model, 0, 1, 0)
        
        
        
        #taskMgr.add(self.move, "outtaThaWayImDrivingHere")
        #self.prevtime = 0
        
        self.setUpHeadlights()
    
    def makeCollisionSolid(self, cTrav, cHandler, num):
        cSphere = CollisionSphere((0,0,0), 3)
        cNode = CollisionNode("car%d"%num)
        cNode.addSolid(cSphere)
        cNodePath = self.model.attachNewNode(cNode)
        #cNodePath.show()
        #registers a from object with the traverser with a corresponding handler
        cTrav.addCollider(cNodePath, cHandler)
    
    def setUpHeadlights(self):
        self.headlights = Spotlight("headlights")
        self.headlights.setColor(VBase4(1.2, 1.2, 1.2, 1))
        #self.headlights.setShadowCaster(True, 512, 512)
        self.headlights.setAttenuation(Point3(0.0001, 0, 0.00001))
        print self.headlights.getAttenuation().getX()
        print self.headlights.getAttenuation().getY()
        print self.headlights.getAttenuation().getZ()
        lens = PerspectiveLens()
        lens.setFov(70, 90)
        lens.setNear(2.0)
        self.headlights.setLens(lens)
        slnp = self.model.attachNewNode(self.headlights)
        slnp.setPos(0, -0.35, 1)
        slnp.setHpr(0,-2.5,0)
        render.setLight(slnp)
        self.overlights = DirectionalLight("overhead lights")
        self.overlights.setColor(VBase4(1, 1, 1, 1))
        oslnp = self.model.attachNewNode(self.overlights)
        oslnp.setHpr(180,-75,0)
        self.model.setLight(oslnp)
        self.lightsOn = True
    
    def toggleHeadlights(self):
        if self.lightsOn:
            self.headlights.setColor(VBase4(0, 0, 0, 1))
            self.overlights.setColor(VBase4(0, 0, 0, 1))
            self.lightsOn = False
        else:
            self.headlights.setColor(VBase4(1.2, 1.2, 1.2, 1))
            self.overlights.setColor(VBase4(1, 1, 1, 1))
            self.lightsOn = True
    
    def setHeadlights(self, val):
        if val != self.lightsOn:
            if self.lightsOn:
                self.headlights.setColor(VBase4(0, 0, 0, 1))
                self.overlights.setColor(VBase4(0, 0, 0, 1))
                self.lightsOn = False
            else:
                self.headlights.setColor(VBase4(1, 1, 1, 1))
                self.overlights.setColor(VBase4(1, 1, 1, 1))
                self.lightsOn = True
                
    def takeDamage(self, num):
        self.hp -= num
    
    def move(self, elapsed):
        #all these numbers need to be tested
        if self.hp < 25 and not self.s3:
            self.smoke3.p.show()
            self.s3 = True
        if self.hp < 50 and not self.s2:
            self.smoke2.p.show()
            self.s2 = True
        if self.hp < 75 and not self.s1:
            self.smoke1.p.show()
            self.s1 = True
        
        #position change
        self.model.setPos(self.model.getX() + self.vel.x * elapsed/.02, self.model.getY() + self.vel.y * elapsed/.02, 0)
        tempmag = self.vel.getM()
        self.vel.addDM(self.model.getH(), elapsed * 1)
        self.vel.setDM(self.vel.getD(), tempmag)
        if self.vel.getM() > 0:
            self.vel.setDM(self.vel.getD(), max(self.vel.getM() - (elapsed * (.5 + 2.5*self.input[4])),0))
        if self.input[2]:#up
            self.vel.addDM(self.model.getH(), elapsed * 4)
            self.vel.setDM(self.vel.getD(), min(self.vel.getM(), 5))#speed cap
        if self.input[3]:#down
            self.vel.addDM(self.model.getH(), elapsed * -4)
            self.vel.setDM(self.vel.getD(), min(self.vel.getM(), 5))#speed cap
        
        #turning
        self.model.setH(self.model.getH() + self.turn * elapsed/.02)
        if self.input[0]:#left
            self.turn += elapsed * (100 + self.vel.getM()*100/4) / 4
            self.turn = min(.02 * (100 + self.vel.getM()*100/4), self.turn)
            #self.model.setH(self.model.getH() + elapsed * (100 + self.vel.getM()*100/4))
        elif self.input[1]:#right
            self.turn -= elapsed * (100 + self.vel.getM()*100/4) / 4
            self.turn = max(-.02 * (100 + self.vel.getM()*100/4), self.turn)
            #self.model.setH(self.model.getH() - elapsed * (100 + self.vel.getM()*100/4))
        else:
            self.turn -= math.copysign(elapsed, self.turn) * (100 + self.vel.getM()*100/4) / 4
            if abs(self.turn) <= (elapsed * (100 + self.vel.getM()*100/4) / 4):
                self.turn = 0