def __init__(self, collider: ICollider, drawable: IDrawable,
                 mass: float, velocity: Vector2D, acceleration: Vector2D = None):
        self._collider: ICollider = collider
        self._drawable: IDrawable = drawable
        self._mass: float = mass
        self._velocity: Vector2D = velocity
        self._acceleration: Vector2D = acceleration if acceleration is not None else Vector2D(0, 0)

        # position shift for collision handling
        self._position_shift = None
    def shift_to_intersection_with_rectangle(self,
                                             other: 'Rectangle') -> Vector2D:
        d_left = other.left - self.right  # distance from left to right >= 0
        d_right = other.right - self.left  # distance from right to left <= 0
        d_top = other.top - self.bottom  # distance from top to bottom >= 0
        d_bottom = other.bottom - self.top  # distance from bottom to top <= 0

        shift = Vector2D(Vector2D.INF, Vector2D.INF)

        if d_left >= 0:
            shift.x = d_left
        elif d_right <= 0:
            shift.x = d_right

        if d_top >= 0:
            shift.y = d_top
        if d_bottom <= 0:
            shift.y = d_bottom

        return shift
示例#3
0
 def draw(self, surface: Surface, position: Vector2D):
     surface.blit(self.image, position.as_tuple())
示例#4
0
 def scale(self, scale: Vector2D):
     self.image = pygame.transform.scale(self.image, scale.as_tuple())
示例#5
0
 def __init__(self, offset: Vector2D = None):
     self.offset: Vector2D = offset if offset is not None else Vector2D(0, 0)
    def start(self):
        W, H = self.screen.get_size()
        T = 60
        WALLS_COLOR = (randint(0, 255), randint(0, 255), randint(0, 255))

        image = Image('adventure_game\\res\\player\\hd1.png')
        image.scale(Vector2D(64, 64))
        image.set_transparent_color(Color(255, 255, 255))
        self.player = KineticBody(collider=RectangleCollider(
            Rectangle(W // 2, H // 2, 64, 64)),
                                  drawable=image,
                                  mass=5,
                                  velocity=Vector2D(0, 0),
                                  acceleration=Vector2D(0, 1))

        self.kinetic_bodies: list[KineticBody] = [self.player]

        self.elastic_bodies = []

        self.collidables: list[ICollidable] = [
            Wall(RectangleCollider(Rectangle(0, 0, W, T)),
                 RectangleDrawable(W, T, Color(*WALLS_COLOR))),
            Wall(RectangleCollider(Rectangle(0, H - T, W, T)),
                 RectangleDrawable(W, T, Color(*WALLS_COLOR))),
            Wall(RectangleCollider(Rectangle(0, T, T, H - T - T)),
                 RectangleDrawable(T, H - T - T, Color(*WALLS_COLOR))),
            Wall(RectangleCollider(Rectangle(W - T, T, T, H - T - T)),
                 RectangleDrawable(T, H - T - T, Color(*WALLS_COLOR))),
            self.player
        ]

        self.not_elastic_collidables = [] + self.collidables

        self.updatables: list[IUpdatable] = [self.player]

        image = Image('adventure_game\\res\\test\\bg1.jpg')
        image.scale(Vector2D(W, H))
        self.entities: list[IEntity] = [
            Wall(RectangleCollider(Rectangle(0, 0, W, H)), image)
        ] + self.collidables

        for _ in range(5):
            r = randint(40, 60)
            x, y = randint(T + T, W - T - T), randint(T + T, H - T - T)
            v = Vector2D(randint(-30, 30), randint(-30, 30))
            color = Color(randint(0, 255), randint(0, 255), randint(0, 255))

            ball = ElasticBody(RectangleCollider(Rectangle(x, y, 2 * r,
                                                           2 * r)),
                               OffsetCircleDrawable(r,
                                                    color,
                                                    offset=Vector2D(r, r)),
                               mass=r * 2,
                               velocity=v,
                               acceleration=Vector2D(0, 1))

            self.kinetic_bodies.append(ball)
            self.elastic_bodies.append(ball)
            self.entities.append(ball)
            self.collidables.append(ball)
            self.updatables.append(ball)

        for i in range(len(self.elastic_bodies)):
            for j in range(i + 1, len(self.elastic_bodies)):
                if self.elastic_bodies[i].collides_with(
                        self.elastic_bodies[j]):
                    raise Exception('NOPE')
 def __init__(self, left_x: float, top_y: float, width: float,
              height: float):
     self.left_top: Vector2D = Vector2D(left_x, top_y)
     self.width: float = width
     self.height: float = height
 def center(self) -> Vector2D:
     return Vector2D(self.left_top.x + self.width / 2,
                     self.left_top.y + self.height / 2)