Beispiel #1
0
    def __init__(self, world, cl, res: Resources, pos, game_object_group,
                 *groups):
        super().__init__(world, cl, game_object_group, *groups, connects=False)

        self.ignore_ray_casting = True
        self.draw_shadow = False

        image = res.image_tire_particle
        self.set_init_image(image)
        self.rect.center = pos

        shape = b2CircleShape()
        shape.radius = self.image.get_size()[0] / 2 / PPM
        fd = B2Factory.create_fixture(shape, 1, 0.1, 0.5, True)
        self.body = B2Factory.create_body(world, b2_dynamicBody, fd,
                                          b2_coords(pos) / PPM)
        self.body.userData = self
        self.body.linearDamping = 3
        self.body.fixedRotation = True
        impulse = 0.5
        self.body.ApplyLinearImpulse((random.uniform(
            -impulse, impulse), random.uniform(-impulse, impulse)),
                                     self.body.GetWorldPoint((0, 0)), True)

        self.timer = 0
        self.timeout = 0.2
Beispiel #2
0
    def __init__(self, world, cl, res: Resources, pos, game_object_group,
                 *groups):
        super().__init__(world, cl, game_object_group, *groups)
        self.set_init_image(res.image_turret)
        self.rect.center = pos
        self.res = res
        self.force = 10**4
        self.base = TurretBase(self.world, self.cl, self.res, pos,
                               self.game_object_group)

        radius = 40
        shape = b2CircleShape()
        shape.radius = radius / PPM
        fd = B2Factory.create_fixture(shape, 1, 0.1, 0.2)
        self.body = B2Factory.create_body(world, b2_dynamicBody, fd,
                                          b2_coords(pos) / PPM)
        shape = b2PolygonShape()
        shape.SetAsBox(28 / 2 / PPM, 81 / 2 / PPM, (0, 81 / 2 / PPM), 0)
        fd = B2Factory.create_fixture(shape, 1, 0.1, 0.2)
        self.body.CreateFixture(fd)
        self.body.userData = self
        self.body.angularDamping = 3

        shape = b2PolygonShape()
        shape.SetAsBox(*b2Vec2(10, 10) / 4 / PPM)
        fd = B2Factory.create_fixture(shape)
        self.center_body = B2Factory.create_body(world, b2_staticBody, fd,
                                                 b2_coords(pos) / PPM)
        jd = b2RevoluteJointDef()
        jd.bodyA = self.body
        jd.bodyB = self.center_body
        self.world.CreateJoint(jd)

        self.shot_timer = 0
        self.shot_timeout = 1
Beispiel #3
0
    def __init__(self, world, cl, image, width,
                 density, friction, restitution, body_type, point1, point2,
                 game_object_group, *groups):
        super().__init__(world, cl, game_object_group, *groups, connects=False)

        point1, point2 = sorted([point1, point2], key=lambda x: x[1])
        size = (width, pygame.Vector2(point1).distance_to(point2))
        self.sin = (point2[0] - point1[0]) / size[1]
        angle = math.asin(self.sin)
        self.size = tuple(map(int, size))
        self.center = (pygame.Vector2(point1) + pygame.Vector2(point2)) / 2
        self.image_stick = pygame.transform.scale(image, self.size).convert_alpha()
        self.width = width
        self.start = point1
        self.end = point2

        # Скругления
        shape = b2PolygonShape()
        shape.SetAsBox(*b2Vec2(size) / 2 / PPM)
        fd1 = B2Factory.create_fixture(shape, density, friction, restitution)
        self.body = B2Factory.create_body(world, body_type, fd1, b2_coords(self.center) / PPM)
        shape = b2CircleShape()
        shape.radius = width / 2 / PPM
        shape.pos = 0, size[1] / 2 / PPM
        fd2 = B2Factory.create_fixture(shape, density, friction, restitution)
        shape = b2CircleShape()
        shape.radius = width / 2 / PPM
        shape.pos = 0, -size[1] / 2 / PPM
        fd3 = B2Factory.create_fixture(shape, density, friction, restitution)
        self.body.CreateFixture(fd2)
        self.body.CreateFixture(fd3)

        self.body.userData = self
        self.body.angle = angle
Beispiel #4
0
    def __init__(self, world, cl, image, car, max_forward_speed,
                 max_backward_speed, max_drive_force, max_lateral_impulse,
                 angular_friction_impulse, linear_friction_impulse, density,
                 restitution, friction, game_object_group, *groups):
        self.shadow_color = PLAYER_CAR_SHADOW_COLOR
        super().__init__(world, cl, game_object_group, *groups)

        size = 10, 24
        self.car = car
        self.set_init_image(image)
        self.rect = pygame.Rect((0, 0), size)

        shape = b2PolygonShape()
        shape.SetAsBox(*b2Vec2(*self.rect.size) / 2 / PPM)
        fd = B2Factory.create_fixture(shape, density, restitution, friction)
        self.body = B2Factory.create_body(world, b2_dynamicBody, fd,
                                          b2_coords(self.rect.center) / PPM)
        self.body.userData = self

        self.control_state = 0
        self.current_traction = 1
        self.grounds = []

        self.acceleration_time = 1
        self.min_acc = 0.3
        self.acceleration = self.min_acc

        self.set_characteristics(max_forward_speed, max_backward_speed,
                                 max_drive_force, max_lateral_impulse,
                                 angular_friction_impulse,
                                 linear_friction_impulse)
Beispiel #5
0
    def __init__(self, world, cl, res: Resources, point1, point2,
                 game_object_group, *groups):
        super().__init__(world, cl, game_object_group, *groups, connects=False)

        self.draw_shadow = False
        self.ignore_ray_casting = True

        width = 10
        point1, point2 = sorted([point1, point2], key=lambda x: x[1])
        size = (width, pygame.Vector2(point1).distance_to(point2) + width * 1)
        center = (pygame.Vector2(point1) + pygame.Vector2(point2)) / 2
        sin = (point2[0] - point1[0]) / (size[1] - width * 1)
        angle = math.asin(sin)
        image = pygame.transform.scale(res.image_wall, tuple(map(int, size)))

        self.start = point1
        self.end = point2
        self.set_init_image(image)
        self.rect.center = center

        shape = b2PolygonShape()
        shape.SetAsBox(*b2Vec2(size) / 2 / PPM)
        fd1 = B2Factory.create_fixture(shape, 2, 0.1, 0.5, True)

        self.body = B2Factory.create_body(world, b2_staticBody, fd1,
                                          b2_coords(center) / PPM)
        self.body.userData = self
        self.body.angle = angle

        self.set_rotated_sprite(self.body, self.get_init_image())
Beispiel #6
0
    def __init__(self, world, cl, res: Resources, pos, vector,
                 game_object_group, *groups):
        super().__init__(world, cl, game_object_group, *groups)

        self.draw_shadow = False
        self.ignore_ray_casting = True
        self.set_init_image(paint_images([res.image_bullet], lambda x: (120, 0, 200, x[3]))[0])
        self.vector = pygame.Vector2(vector).normalize()
        pos = pygame.Vector2(pos) + self.vector * 100
        self.rect.center = pos
        self.res = res

        self.impulse = 2.5
        radius = 12
        shape = b2CircleShape()
        shape.radius = radius / PPM
        fd = B2Factory.create_fixture(shape, 0.3, 0, 1)
        self.body = B2Factory.create_body(
            world, b2_dynamicBody, fd, b2_coords(pos) / PPM)
        self.body.userData = self
        self.body.fixedRotation = True
        self.body.ApplyLinearImpulse(b2_coords(self.vector) * self.impulse, self.body.GetWorldPoint((0, 0)), True)

        self.timer = 0
        self.timeout = 3
        self.collide = False
Beispiel #7
0
    def __init__(self,
                 world,
                 cl,
                 res: Resources,
                 car,
                 vector,
                 explosion_callback,
                 game_object_group,
                 *groups,
                 damage_me=True):
        super().__init__(world, cl, game_object_group, *groups)

        self.draw_shadow = False
        self.ignore_ray_casting = True
        self.animation = Animation(res.animation_fireball, 6, 20)
        self.image = self.animation.get_frame()
        self.vector = pygame.Vector2(vector).normalize()
        pos = car.get_position() + self.vector * 10
        self.rect = pygame.Rect(0, 0, *self.image.get_size())
        self.rect.center = pos
        self.res = res
        self.car = car
        self.callback = explosion_callback
        self.damage_me = damage_me

        self.impulse = 5
        self.force = 15
        radius = 20
        shift = 40, 0

        shape = b2CircleShape()
        shape.radius = radius / PPM
        shape.pos = b2Vec2(shift) / PPM
        fd = B2Factory.create_fixture(shape, 0.3, 0.1, 0.5)
        self.body = B2Factory.create_body(world, b2_dynamicBody, fd,
                                          b2_coords(pos) / PPM)
        self.body.userData = self
        self.body.angle = math.radians(
            pygame.Vector2(self.vector).angle_to((1, 0)))
        self.body.fixedRotation = True
        self.body.linearDamping = 0.3
        self.body.ApplyLinearImpulse(
            b2_coords(self.vector) * self.impulse,
            self.body.GetWorldPoint((0, 0)), True)

        self.timer = 0
        self.collide = False
Beispiel #8
0
    def __init__(self, world: b2World, cl: ContactListener, center,
                 game_object_group: Group, *groups):
        super().__init__(world, cl, game_object_group, *groups)
        radius = 400
        image = pygame.Surface((radius * 2, radius * 2)).convert_alpha()
        image.fill((0, 0, 0, 0))
        pygame.draw.circle(image, (100, 100, 100, 30), (radius, radius),
                           radius)
        self.set_init_image(image)
        self.rect.center = center

        self.ignore_ray_casting = True
        self.draw_shadow = False
        shape = b2CircleShape()
        shape.radius = radius / PPM
        fd = B2Factory.create_fixture(shape, is_sensor=True)
        self.body = B2Factory.create_body(self.world, b2_staticBody, fd,
                                          b2_coords(center) / PPM)
        self.body.userData = self
Beispiel #9
0
    def __init__(self, world: b2World, cl: ContactListener, res: Resources,
                 center, game_object_group: Group, *groups):
        super().__init__(world, cl, game_object_group, *groups)

        self.set_init_image(res.image_bank)
        self.rect.center = center

        render = res.font64.render('$  Банк  $', True, (0, 0, 0))
        self.image.blit(
            render, ((self.image.get_width() - render.get_width()) / 2, 230))

        shape = b2PolygonShape()
        shape.SetAsBox(*b2Vec2(self.rect.size) / 2 / PPM)
        fd = B2Factory.create_fixture(shape)
        self.body = B2Factory.create_body(self.world, b2_staticBody, fd,
                                          b2_coords(center) / PPM)
        self.body.userData = self
        self.set_rotated_sprite(self.body, self.image)
        LootArea(self.world, self.cl, center, self.game_object_group)
Beispiel #10
0
    def __init__(self, world, cl, res: Resources,
                 pos, game_object_group, *groups):
        super().__init__(world, cl, game_object_group, *groups)

        self.ignore_ray_casting = True
        self.set_init_image(res.energy_item)
        self.rect.center = pos

        shape = b2CircleShape()
        shape.radius = self.image.get_width() / 2 / PPM
        fd1 = B2Factory.create_fixture(shape, 0.2, 0.2, 0.2, True)

        self.body = B2Factory.create_body(world, b2_dynamicBody, fd1, b2_coords(pos) / PPM)
        self.body.userData = self
        self.body.fixedRotation = True

        self.timer = 0
        self.timeout = 0.5
        self.is_collecting = False
Beispiel #11
0
    def __init__(self, world: b2World, cl: ContactListener, res: Resources, start, end,
                 game_object_group: Group, *groups):
        super().__init__(world, cl, game_object_group, *groups)

        self.ignore_ray_casting = True
        self.draw_shadow = False
        width = 24
        point1, point2 = sorted([start, end], key=lambda x: x[1])
        size = (width, pygame.Vector2(point1).distance_to(point2))
        sin = (point2[0] - point1[0]) / size[1]
        angle = math.asin(sin)
        size = tuple(map(int, size))
        center = (pygame.Vector2(point1) + pygame.Vector2(point2)) / 2

        shape = b2PolygonShape()
        shape.SetAsBox(*b2Vec2(size) / 2 / PPM, (0, 0), angle)
        fd = B2Factory.create_fixture(shape, is_sensor=True)
        self.body = B2Factory.create_body(self.world, b2_staticBody, fd, b2_coords(center) / PPM)
        self.body.userData = self

        self.collided = False
Beispiel #12
0
    def __init__(self, world, cl, res: Resources, rect: pygame.Rect,
                 friction_modifier, game_object_group, *groups):
        super().__init__(world, cl, game_object_group, *groups)

        self.friction_modifier = friction_modifier
        self.ignore_ray_casting = True
        self.draw_shadow = False

        rect = pygame.Rect(rect)
        image = pygame.transform.scale(res.image_ground, rect.size)
        self.set_init_image(image)
        self.rect = pygame.Rect(rect.topleft, image.get_size())

        shape = b2PolygonShape()
        shape.SetAsBox(*b2Vec2(*rect.size) / 2 / PPM)
        fd1 = B2Factory.create_fixture(shape, 0, is_sensor=True)

        self.body = B2Factory.create_body(world, b2_staticBody, fd1,
                                          b2_coords(rect.center) / PPM)
        self.body.userData = self

        self.set_rotated_sprite(self.body, self.get_init_image())
Beispiel #13
0
    def __init__(self, world, cl, res: Resources, car, explosion_callback,
                 game_object_group, *groups):
        super().__init__(world, cl, game_object_group, *groups, connects=False)

        self.ignore_ray_casting = True
        radius = 26
        pos = car.get_position() + car.get_vector() * -50
        self.res = res
        image = res.image_bomb
        self.set_init_image(image)
        self.rect.center = pos
        self.explosion_callback = explosion_callback

        shape = b2CircleShape()
        shape.radius = radius / PPM
        fd = B2Factory.create_fixture(shape, 0.3, 0.1, 0.5)
        self.body = B2Factory.create_body(world, b2_dynamicBody, fd,
                                          b2_coords(pos) / PPM)
        self.body.userData = self
        self.body.linearDamping = 3
        impulse = 90
        self.body.ApplyTorque(random.randint(-impulse, impulse), True)
        self.timer = 0
Beispiel #14
0
    def __init__(self, world, cl, res: Resources, pos, images, vector, timeout,
                 game_object_group, *groups):
        super().__init__(world, cl, game_object_group, *groups)

        self.ignore_ray_casting = True
        self.draw_shadow = False

        self.set_init_image(random.choice(images))
        self.rect.center = pos

        shape = b2CircleShape()
        shape.radius = self.image.get_size()[0] / 2 / PPM
        fd = B2Factory.create_fixture(shape, 0.1, 0.1, 0.5)
        self.body = B2Factory.create_body(world, b2_dynamicBody, fd,
                                          b2_coords(pos) / PPM)
        self.body.userData = self
        self.body.linearDamping = 1
        self.body.fixedRotation = True
        self.body.ApplyLinearImpulse(
            b2_coords(vector) / PPM, self.body.GetWorldPoint((0, 0)), True)

        self.timer = 0
        self.timeout = timeout
Beispiel #15
0
    def __init__(self, world, cl, res: Resources, center, radius, angle, speed,
                 game_object_group, *groups):
        super().__init__(world, cl, res.image_revolving_wall, 28, 10, 0.1, 0.5,
                         b2_dynamicBody, (center[0] - radius, center[1]),
                         (center[0] + radius, center[1]), game_object_group,
                         *groups)

        self.torque = speed
        self.body.angle = math.radians(angle)
        self.body.linearDamping = 1
        self.body.angularDamping = 1

        shape = b2PolygonShape()
        shape.SetAsBox(*b2Vec2(24, 24) / 4 / PPM)
        fd = B2Factory.create_fixture(shape)
        self.center_body = B2Factory.create_body(world, b2_staticBody, fd,
                                                 b2_coords(center) / PPM)
        jd = b2RevoluteJointDef()
        jd.bodyA = self.body
        jd.bodyB = self.center_body
        self.world.CreateJoint(jd)

        self.create_image()
Beispiel #16
0
    def __init__(self, world, cl, res, skin: CarSkin, pos, angle,
                 game_object_group, *groups):
        super().__init__(world, cl, game_object_group, *groups)

        self.res = res
        self.set_init_image(skin.image_car)
        self.rect = pygame.Rect(pos, self.size)

        self.control_state = 0
        self.color_particles = 50, 50, 50

        # Default characteristics
        # Power characteristics
        self.max_forward_speed = 30
        self.max_backward_speed = -12
        self.max_drive_force = 30
        # Control characteristics
        self.max_lateral_impulse = 0.5
        self.angular_friction_impulse = 0.2
        self.linear_friction_impulse = 0.3

        self.lock_angle = math.radians(35)
        self.speed_tire_rotate = math.radians(180)

        # Body characteristics
        self.density = 1
        self.restitution = 0.02
        self.friction = 0.2

        self.tire_density = 3
        self.tire_restitution = 0.1
        self.tire_friction = 1

        s = b2Vec2(self.rect.size) / 2 / PPM
        box_vert = [(-s[0], -s[1]), (-s[0] / 2, s[1]), (s[0] / 2, s[1]),
                    (s[0], -s[1])]

        shape = b2PolygonShape()
        shape.vertices = box_vert
        fd = B2Factory.create_fixture(shape, self.density, self.restitution,
                                      self.friction)
        self.body = B2Factory.create_body(world, b2_dynamicBody, fd,
                                          b2_coords(pos) / PPM)
        self.body.userData = self
        self.body.angle = math.radians(angle)
        self.body.angularDamping = 0.3

        # Add tires
        def _create_tire(delta, jd):
            tire = Tire(world, self.cl, skin.image_tire, self,
                        self.max_forward_speed, self.max_backward_speed,
                        self.max_drive_force, self.max_lateral_impulse,
                        self.angular_friction_impulse,
                        self.linear_friction_impulse, self.tire_density,
                        self.tire_restitution, self.tire_friction,
                        self.game_object_group)
            tire.body.position = self.body.GetWorldPoint(delta)
            tire.body.angle = self.body.angle
            jd.bodyB = tire.body
            jd.localAnchorA = delta
            return tire, jd

        jd = b2RevoluteJointDef()
        jd.bodyA = self.body
        jd.enableLimit = True
        jd.lowerAngle = 0
        jd.upperAngle = 0
        jd.localAnchorA.SetZero()
        shift = b2Vec2(self.tire_shift) / PPM

        # ForwardLeft, ForwardRight, BackwardLeft, BackwardRight
        self.tires = []
        self.joints = []

        for i in [(-shift.x, shift.y), (shift.x, shift.y),
                  (-shift.x, -shift.y), (shift.x, -shift.y)]:
            tire, jd = _create_tire(b2Vec2(i), jd)
            self.tires.append(tire)
            self.joints.append(world.CreateJoint(jd))

        self.is_broken = False
        self.broken_timer = 0
        self.dispose_timeout = 5