Пример #1
0
 def __init__(self):
     self.x = BALL_X
     self.y = BALL_Y
     self.width = BALL_SIZE
     self.height = BALL_SIZE
     self.color = RED
     self.velocity = Velocity()
Пример #2
0
    def on_key_press(self, key: int, modifiers: int):
        """
        Puts the current key in the set of keys that are being held.
        You will need to add things here to handle firing the bullet.
        """
        if self.ship.alive:
            self.held_keys.add(key)

            # Fire death blossom
            if (key == arcade.key.SPACE and
                    modifiers == arcade.key.MOD_SHIFT and
                    self.score >= DEATH_BLOSSOM_REQUIRED_SCORE):
                death_blossom = DeathBlossom(Point(x=self.ship.center.x, y=self.ship.center.y),
                                             Velocity(dx=self.ship.velocity.dx, dy=self.ship.velocity.dy),
                                             angle=self.ship.angle)
                self.bullets.append(death_blossom)
                self.score -= DEATH_BLOSSOM_REQUIRED_SCORE

            # Fire bullet
            elif key == arcade.key.SPACE:
                # TODO: Fire the bullet here!
                bullet = Bullet(Point(x=self.ship.center.x, y=self.ship.center.y),
                                Velocity(dx=self.ship.velocity.dx, dy=self.ship.velocity.dy),
                                angle=self.ship.angle)
                self.bullets.append(bullet)
Пример #3
0
class FlyingObject:
    """The FlyingObject has all the"""
    def __init__(self, starting_point):
        self.alive = True
        self.velocity = Velocity(0, 0)
        self.center = starting_point

    def isAlive(self):
        return self.alive

    def setPoint(self, point):
        self.center = point

    def setVelocity(self, velocity=None, dx=None, dy=None):
        if velocity != None:
            self.velocity = velocity
        elif dx != None and dy != None:
            self.velocity.set_dx(dx)
            self.velocity.set_dy(dy)

    def advance(self):
        self.center.add_x(self.velocity.dx)
        self.center.add_y(self.velocity.dy)

    def kill(self):
        self.alive = False

    def is_off_screen(self, screen_width, screen_height):
        if self.center.x < 0 or self.center.x > 600 or self.center.y < 0 or self.center.y > 500:
            return True
        else:
            return False
Пример #4
0
 def __init__(self, point):
     self.point = point
     self.velocity = Velocity(0, 0, 0)
     self.states = [self.__repr__()]
     self.orbit_time = None
     self.initial_point = point.copy()
     self.initial_velocity = Velocity(0, 0, 0)
     self.do_x = True
     self.do_y = True
     self.do_z = True
Пример #5
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
Пример #6
0
    def split(self):
        """
        If a large asteroid gets hit, it breaks apart and becomes 
        two medium asteroids (1,2) and one small one (3): 
            1. The first medium asteroid has the same velocity as the 
                original large one plus 2 pixel/frame in the up direction.
            2. The second medium asteroid has the same velocity as the 
                original large one plus 2 pixel/frame in the down direction.
            3. The small asteroid has the original velocity plus 
                5 pixels/frame to the right.
        """
        # This method will return a collection of these objects.
        # Why are these here and not in the game class? Consider moving.
        # Does having it here better fit the python ethos of having the
        # objects take care of object matters? Does this break the object
        # model? Objects in RL split or break all the time; in their
        # deaths rise up other objects; this happens at the object level,
        # like die() or fire() or hit(). Also taking care of it at the
        # object level let's us take whatever they send back and stick
        # it in the rocks collection all in one swoop.
        if (constants.DEBUG):
            print(f'\n\nBigRock: split: split!!')
        # create the return collection.
        shards = set()
        rock = MediumRock(Point.__deepcopy__(self.center),
                          Velocity.__deepcopy__(self.velocity))
        # y axis is up.
        rock.velocity.dy += 2
        if (constants.DEBUG):
            print('BigRock.split(): 1st rock: ', rock)
        shards.add(rock)
        # 2nd med. rock:
        rock = MediumRock(Point.__deepcopy__(self.center),
                          Velocity.__deepcopy__(self.velocity))
        # y axis is up.
        rock.velocity.dy -= 2
        if (constants.DEBUG):
            print('BigRock.split(): 2nd rock: ', rock)
        shards.add(rock)

        rock = SmallRock(Point.__deepcopy__(self.center),
                         Velocity.__deepcopy__(self.velocity))
        rock.velocity.dx += 5
        if (constants.DEBUG):
            print('BigRock.split(): 3rd rock: ', rock)
        shards.add(rock)

        return shards
Пример #7
0
 def __init__(self):
     self.center = Point()
     self.velocity = Velocity()
     self.center.x = BALL_RADIUS
     self.center.y = random.uniform(BALL_RADIUS, SCREEN_HEIGHT - BALL_RADIUS)
     self.velocity.dx = random.uniform(1, 5)
     self.velocity.dy = random.uniform(1, 5)
Пример #8
0
    def __init__(self, screen_width, screen_height):
        """
        Initialize a BigRock
        :param screen_width: int
        :param screen_height: int
        """

        # Define acceptable range for big rocks to start (far enough away from the ship that
        # the ship is not instantly killed)
        acceptable_start_x = [
            x for x in range(screen_width)
            if not screen_width / 3 < x < 2 * screen_width / 3
        ]
        acceptable_start_y = [
            y for y in range(screen_height)
            if not screen_height / 3 < y < 2 * screen_height / 3
        ]
        super().__init__(
            center=Point(x=random.choice(acceptable_start_x),
                         y=random.choice(acceptable_start_y)),
            velocity=Velocity(
                dx=math.cos(random.uniform(0, 2 * math.pi)) * BIG_ROCK_SPEED,
                dy=math.sin(random.uniform(0, 2 * math.pi)) * BIG_ROCK_SPEED,
                da=BIG_ROCK_SPIN),
            radius=BIG_ROCK_RADIUS)
Пример #9
0
 def __init__(self):
     self.x = PLAYER_X
     self.y = PLAYER_Y
     self.width = PLAYER_WIDTH
     self.height = PLAYER_HEIGHT
     self.color = RED
     self.velocity = Velocity()
Пример #10
0
    def __init__(self, start_point, angle, inherited_velocity):
        # adds Bullet_speed to this velocity
        super().__init__()
        self._damage = 1
        self.angle = angle
        self.life = Bullet.BULLET_LIFE
        # this velocity assignment is taken from the instructions in the
        # assignment:
        #   "Bullets are should [sic] start with the same velocity of the ship
        #    (speed and direction) plus 10 pixels per frame in the direction
        #    the ship is pointed."
        # Let me know if this is not what you want. It makes sense logically,
        # but in cases where the ship is going faster than bulllet speed, the
        # bullet appears to have a negative direction.#
        self.velocity = inherited_velocity + Velocity.velocity_from_speed_angle(
            Bullet.BULLET_SPEED, self.angle)
        # old calculation below.
        #self.velocity = Velocity.velocity_from_speed_angle(inherited_velocity.speed + Bullet.BULLET_SPEED, (self.angle))

        # bullet center should be the ship's center plus the length of the ship's radius in the
        # proper direction. Taken care of in ship. Bullet is ignorant of ship.
        self.center = start_point
        # originally this was not a texture asset. it was just a small dot bullet.
        # having implemented this after implementing the other texture assets (rocks & ship)
        # I realize that texture should have been made a part of flyer. For this milestone
        # anyhow, it will remain individual extended class attributes rather than inherited.
        # self.texture = arcade.load_texture(constants.PATH_IMAGES + 'laserBlue01.png')
        self.texture = arcade.load_texture(constants.PATH_IMAGES +
                                           'laserBlue01.png')
        self.radius = Bullet.BULLET_RADIUS
Пример #11
0
    def break_apart(self):
        """
        Define what happens when something collides with a MediumRock object
        :return: a list of smaller Rock objects
        """
        debris1 = SmallRock(
            Point(self.center.x, self.center.y),
            Velocity(self.velocity.dx + 1.5, self.velocity.dy + 1.5,
                     SMALL_ROCK_SPIN))
        debris2 = SmallRock(
            Point(self.center.x, self.center.y),
            Velocity(self.velocity.dx - 1.5, self.velocity.dy - 1.5,
                     SMALL_ROCK_SPIN))

        self.kill()

        return [debris1, debris2]
Пример #12
0
    def break_apart(self):
        """
        Define what happens when something collides with a BigRock
        :return: a list of smaller Rock objects
        """
        debris1 = MediumRock(
            Point(self.center.x, self.center.y),
            Velocity(self.velocity.dx, self.velocity.dy + 2, MEDIUM_ROCK_SPIN))
        debris2 = MediumRock(
            Point(self.center.x, self.center.y),
            Velocity(self.velocity.dx, self.velocity.dy - 2, MEDIUM_ROCK_SPIN))
        debris3 = SmallRock(
            Point(self.center.x, self.center.y),
            Velocity(self.velocity.dx + 5, self.velocity.dy, SMALL_ROCK_SPIN))
        self.kill()

        return [debris1, debris2, debris3]
Пример #13
0
 def __init__(self,
              screen,
              amp=0,
              rad=0,
              angle=0,
              pos=(0, 0),
              radius=10,
              width=0,
              max_speed=20,
              color=(0, 0, 0)):
     Velocity.__init__(self, amp, rad, angle, max_speed)
     self.ball_color = color
     self.ball_acc = Velocity(0.3, angle=90)  # 小球加速度
     self.centerx = pos[0]
     self.centery = pos[1]
     self.radius = radius
     self.width = width
     self.screen = screen
Пример #14
0
 def __init__(self):
     """
     Initializes all essential variables for a flying object.
     """
     self.center = Point()
     self.velocity = Velocity()
     self.speed = 0
     self.radius = 0
     self.angle = 90
Пример #15
0
class Moon:
    def __init__(self, point):
        self.point = point
        self.velocity = Velocity(0, 0, 0)
        self.states = [self.__repr__()]
        self.orbit_time = None
        self.initial_point = point.copy()
        self.initial_velocity = Velocity(0, 0, 0)
        self.do_x = True
        self.do_y = True
        self.do_z = True

    def __repr__(self):
        return f'pos={self.point}, vel={self.velocity}'

    def x_is_same_as_initial(self):
        return self.point.x == self.initial_point.x and self.velocity.x == self.initial_velocity.x

    def y_is_same_as_initial(self):
        return self.point.y == self.initial_point.y and self.velocity.y == self.initial_velocity.y

    def z_is_same_as_initial(self):
        return self.point.z == self.initial_point.z and self.velocity.z == self.initial_velocity.z

    def apply_gravity(self, other_moon):
        if self.do_x:
            if (self.point.x > other_moon.point.x):
                self.velocity.x -= 1
            elif (self.point.x < other_moon.point.x):
                self.velocity.x += 1

        if self.do_y:
            if (self.point.y > other_moon.point.y):
                self.velocity.y -= 1
            elif (self.point.y < other_moon.point.y):
                self.velocity.y += 1
        if self.do_z:
            if (self.point.z > other_moon.point.z):
                self.velocity.z -= 1
            elif (self.point.z < other_moon.point.z):
                self.velocity.z += 1

    # def record_state(self):
    #     if self.__repr__() in self.states:
    #         self.orbit_time = len(self.states)
    #         print(self.orbit_time)
    #     else:
    #         self.states.append(self.__repr__())

    def apply_velocity(self):
        self.point.apply_velocity(self.velocity)
        # if self.orbit_time == None:
        #     self.record_state()

    def get_energy(self):
        return self.point.energy() * self.velocity.energy()
Пример #16
0
 def __init__(self):
     self.center = Point()
     self.velocity = Velocity()
     self.alive = True
     self.img = ""
     self.radius = 0.0
     self.angle = 0.0
     self.speed = 0.0
     self.direction = 0.0
     self.alpha = 255
Пример #17
0
 def __init__(self):
     self.center = Point()  # initially 0 for x and y
     self.velocity = Velocity()  # initially 0 for dx and dy
     self.alive = True  # always start being alive
     self.height = 0  # figure height
     self.width = 0  # figure width
     self.texture = arcade.load_texture("images/playerShip1_orange.png")  # import the texture
     self.rotation = 0  # how much the figure is rotation
     self.alpha = 1000  # will not be transparent
     self.radius = 0
Пример #18
0
    def __init__(self,center=None):
        self.alive = True
        self.velocity = Velocity()
        self.isWrapX = False
        self.isWrapY = False

        if center == None:
            self.center = Point()
        else:
            self.center = center.copy()
Пример #19
0
 def __init__(self):
     super().__init__()
     if random.random() * 100 % 2 > 1:
         # so center will have an x = 0 and velocity will need to be x +
         self.center = Point(0, random.random() * constants.SCREEN_HEIGHT)
         self.velocity = Velocity(2.0, 2.0)
     else:
         # so center will have an x = SCREEN_WIDTH and velocity will need to be x -
         self.center = Point(constants.SCREEN_WIDTH,
                             random.random() * constants.SCREEN_HEIGHT)
         self.velocity = Velocity(-2.0, 2.0)
     if random.random() * 100 % 2 > 1:
         self.texture = arcade.load_texture(constants.PATH_IMAGES +
                                            'alien_ship_green-sm.png')
     else:
         self.texture = arcade.load_texture(constants.PATH_IMAGES +
                                            'alien_ship_green.png')
     self.radius = self.texture.width // 2 * 0.75
     self.angle = 0
Пример #20
0
 def __init__(self, x=0, y=0, dx=1, dy=1):
     """
     Initialize flying object
     :param x:
     :param y:
     :param dx:
     :param dy:
     """
     self.center = Point(x, y)
     self.velocity = Velocity(dx, dy)
Пример #21
0
    def fire(self, target):
        """
        Returns an AlienBullet with a trajectory towards the target's'
        current location.
        """
        angle = math.atan2(target.center.y - self.center.y,
                           target.center.x - self.center.x)
        angle = angle * 180 / math.pi  #convert to degrees
        # put the origin point of the bullet far enough
        # away it does not photon itself.
        radius_adjust = self.texture.height / 2 * 1.25
        laser_barrel_end = Point(radius_adjust * Velocity.cosine(angle),
                                 radius_adjust * Velocity.sine(angle))
        laser_barrel_end = laser_barrel_end + self.center

        p = Point(laser_barrel_end.x, laser_barrel_end.y)
        v = Velocity(self.velocity.dx, self.velocity.dy)

        return AlienBullet(p, angle, v)
Пример #22
0
 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
Пример #23
0
 def __init__(self):
     #create a new ball with a point at the center and a velocity
     self.center = Point()
     self.velocity = Velocity()
     #set x to the balls radius so that it appears on the screen
     self.center.x = BALL_RADIUS
     #set y to a random number that is between the balls radius and the highest point subtract the radius
     self.center.y = random.uniform(BALL_RADIUS,
                                    SCREEN_HEIGHT - BALL_RADIUS)
     #assign the ball a random velocity
     self.velocity.dx = random.uniform(0.25, 5)
     self.velocity.dy = random.uniform(0.25, 5)
Пример #24
0
 def __init__(self):
     # while Flyer should be considered abstract, this lays out
     # the framework here. See notes above.
     self.name = self.__class__.__name__
     self.center = Point()
     self.velocity = Velocity()
     self.radius = 1
     self.alive = True
     # all flyers have a color. Here to back up any that don't specify
     self.color = arcade.color.PINK
     self.angle = 0
     # loading an empty image for this. Re-evaluate if performance hit.
     self.texture = None  # arcade.load_texture(constants.PATH_IMAGES + 'stub.png')
Пример #25
0
    def __init__(self, startPoint=Point()):
        super().__init__(startPoint)
        self.texture = arcade.load_texture(constants.PATH_IMAGES +
                                           'meteorGrey_big1.png')
        self.radius = self.texture.width / 2
        #TODO FIX! self.radius = BigRock.BIG_ROCK_RADIUS
        self.spin = BigRock.BIG_ROCK_SPIN

        #only bigrocks get an initial velocity
        self.velocity = Velocity.velocity_from_speed_angle(
            BigRock.BIG_ROCK_SPEED, self.angle)
        self.points = BigRock.BIG_ROCK_POINTS
        self._damage = 5
Пример #26
0
 def __init__(self, angle=0.0, velocity=Velocity(0, 0)):
     super().__init__()
     self._angle = angle
     self.dx = velocity.dx
     self.dy = velocity.dy
     self.focal_point = Point()
     # the momentum in the rotation
     self.delta_angle = 0
     # drag represents the inertia decay of movements. if this is
     # set to AngularVelocity.MAX_VIABILITY, rotation will have no inertia.
     self._drag = .85
     # percentage by which drag decays from it's maximum each cycle.
     self.decay = 0.05
Пример #27
0
 def __init__(self, startPoint=None, startVelocity=None):
     super().__init__()
     #arcade image        
     self.texture = None #arcade.load_texture(None)
     self.spin = 0
     if startPoint is None:            
         self.center = Point(random.random() * constants.SCREEN_WIDTH, random.random() * constants.SCREEN_HEIGHT)
     else:
         self.center = Point(startPoint.x, startPoint.y)
     if startVelocity is not None:
         self.velocity = Velocity(startVelocity.dx, startVelocity.dy)
     self.angle = random.random() * 360
     self.points = 0
     self._damage = 10
Пример #28
0
class Ball:
    def __init__(self):                            ##constructor
        self.center = Point()
        self.velocity = Velocity()
        self.center.reset_ball()
        self.center.x = 200
        self.center.y = 150

    def draw(self):                                 ##to draw the ball object
        arcade.draw_circle_filled(self.center.x, self.center.y, BALL_RADIUS, (random.randint(0, 256), random.randint(0, 256), random.randint(0, 256)))

    def advance(self):                               ##to handle the ball object movement
        self.center.x += self.velocity.dx
        self.center.y += self.velocity.dy

    def bounce_horizontal(self):                     ## to handle the bounce of the ball in horizontal direction
        self.velocity.dx *= -1

    def bounce_vertical(self):                       ##to handle the bounce of the ball in vertical direction
        self.velocity.dy *= -1

    def restart(self):                               ##to restrat the ball in the game
        self.center.reset_ball()
        self.velocity.reset_velocity()
Пример #29
0
 def __init__(self,
              center=Point(),
              velocity=Velocity(),
              radius=10,
              angle=0):
     """
     Initialize a flying rock
     :param center:
     :param velocity:
     :param radius:
     :param angle:
     """
     super().__init__(center=center,
                      velocity=velocity,
                      radius=radius,
                      angle=angle)
     self.spin = 0
Пример #30
0
 def __init__(self,
              center=Point(),
              velocity=Velocity(),
              radius=10,
              angle=0):
     """
     Initialize flying object
     :param center: Point object
     :param velocity: Velocity object
     :param radius: int
     :param angle: int in degrees
     """
     self._center = center
     self._velocity = velocity
     self._radius = radius
     self._angle = angle
     self._alive = True
Пример #31
0
    def __init__(self, image):

        # Status
        self.center = Point()
        self.velocity = Velocity()
        self.alive = True

        # Movement
        self.speed = 1
        self.angle = 1.0
        self.direction = 1

        # Dimensions/View
        self.img = image
        self.texture = arcade.load_texture(self.img)
        self.height = self.texture.height
        self.width = self.texture.width
        self.radius = SHIP_RADIUS
Пример #32
0
    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()
Пример #33
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
Пример #34
0
class FlyingObject:
    def __init__(self, starting_point):
        self.alive = True
        self.starting_point = starting_point
        self.velocity = Velocity(0, 0)
        self.center = starting_point

    def isAlive(self):
        return self.alive

    def setPoint(self, point):
        self.center = point

    def getPoint(self):
        return self.center

    def setVelocity(self, velocity=None, dx=None, dy=None):
        if velocity != None:
            self.velocity = velocity
        elif dx != None and dy != None:
            self.velocity.set_dx(dx)
            self.velocity.set_dy(dy)

    def getVelocity(self):
        return self.velocity

    def advance(self):
        self.center.add_x(self.velocity.get_dx())
        self.center.add_y(self.velocity.get_dy())

    def kill(self):
        self.alive = False

    def is_off_screen(self, screen_width, screen_height):
        if self.center.get_center_x() < 0 or self.center.get_center_x(
        ) > 600 or self.center.get_center_y() < 0 or self.center.get_center_y(
        ) > 500:
            return True
        else:
            return False
Пример #35
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
Пример #36
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