Skip to content

(Dampening Effect, Lateral Dampening)

Yoann Berenguer edited this page Apr 17, 2022 · 5 revisions

DAMPENING EFFECT

dampening(
        object surface_,
        int frame_,
        int display_width,
        int display_height_,
        float amplitude_=50.0,
        int duration_=30,
        float freq_=20.0)

    """
    DAMPENING EFFECT

    Cython cpdef function, this function can be called directly and do not require a
    hook function.

    Compatible with image 24-32 bit
    The length of the effect equal duration_ * freq_

    e.g :
    surf, xx, yy = dampening(BCK, frame, width, height,
    amplitude_=100, duration_=40, freq_=15)
    SCREEN.blit(surf, (xx, yy))

    :param surface_       : pygame.Surface (compatible 24 - 32 bit)
    :param frame_         : integer; Frame number (linear variable changing overtime)
    :param display_width  : integer; Size of your game display (width)
    :param display_height_: integer; size of your game display (height)
    :param amplitude_     : float; Amplitude of the dampening effect  (default is 50)
    :param duration_      : integer; Duration of the effect (default value is 30)
    :param freq_          : float; change the speed of the effect default value is 20.0.
    A small value will decrease
    the overall timing of the effect while a larger value will increase the duration of the effect.
    :return               : Tuple values containing the Surface and the position (x, y)
    with x & y are the top
     left corner of the
    image
    """

e.g:

WIDTH = 1024
HEIGHT = 768
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT), vsync=True)
SCREEN.convert(32, RLEACCEL)
SCREEN.set_alpha(None)

BACKGROUND = pygame.image.load("../Assets/background.jpg")
BACKGROUND = pygame.transform.smoothscale(BACKGROUND, (WIDTH, HEIGHT))
image = BACKGROUND.copy()
pygame.display.set_caption("Test dampening")

FRAME = 0
CLOCK = pygame.time.Clock()
GAME = True
t = time.time()
while GAME:

    pygame.event.pump()
    for event in pygame.event.get():

        keys = pygame.key.get_pressed()

        if keys[pygame.K_ESCAPE]:
            GAME = False
            break

    surf, xx, yy = dampening(
        BACKGROUND,
        FRAME,
        WIDTH,
        HEIGHT,
        amplitude_=15.0,
        duration_=10,
        freq_=25.0)

    surf.convert(32, RLEACCEL)
    SCREEN.blit(surf, (xx, yy), special_flags=0)

    pygame.display.flip()
    CLOCK.tick()
    FRAME += 1

    pygame.display.set_caption(
        "Test dampening %s fps "
        "(%sx%s)" % (round(CLOCK.get_fps(), 2), WIDTH, HEIGHT))

    image = BACKGROUND.copy()
    if time.time() - t > 5:
        break

demo https://www.youtube.com/watch?v=XgLF2BWP0Rs#?t=14s


LATERAL DAMPENING

lateral_dampening(int frame_, float amplitude_=50.0,
                  int duration_=30, float freq_=20.0)
    """
    DAMPENING EFFECT

    * This method return the lateral displacement (x)

    e.g:
    tm = lateral_dampening(frame, amplitude_=50.0, duration_=35, freq_=5.0)
    SCREEN.blit(BCK, (tm, 0), special_flags=0)

    Cython cpdef function, this function can be called directly and do not require a
    hook function.

    The length of the effect equal duration_ * freq_

    :param frame_    : integer; Your game frame number
    :param amplitude_: float; Represent the amplitude of the dampening effect.
                       An amplitude of 1.0 will have no effect.Default value is 50.0
    :param duration_ : float; This represent the duration of the effect, default value is 30
    :param freq_     : float; change the speed of the effect default value is 20.0.
                       A small value will decrease
                       the overall timing of the effect while a larger value will increase the 
                       duration of the effect.
    :return          : Return a float corresponding to the lateral displacement (x)
    """

e.g:

BACKGROUND = pygame.image.load("../Assets/background.jpg")
BACKGROUND = pygame.transform.smoothscale(BACKGROUND, (WIDTH, HEIGHT))
image = BACKGROUND.copy()
pygame.display.set_caption("Test lateral_dampening")

FRAME = 0
CLOCK = pygame.time.Clock()
GAME = True
t = time.time()

while GAME:

    pygame.event.pump()
    for event in pygame.event.get():

        keys = pygame.key.get_pressed()

        if keys[pygame.K_ESCAPE]:
            GAME = False
            break

    tm = lateral_dampening(FRAME, amplitude_=10.0, duration_=10, freq_=50.0)
    SCREEN.blit(BACKGROUND, (tm, 0), special_flags=0)

    pygame.display.flip()
    CLOCK.tick()
    FRAME += 1

    pygame.display.set_caption(
        "Test lateral_dampening %s fps "
        "(%sx%s)" % (round(CLOCK.get_fps(), 2), WIDTH, HEIGHT))

    image = BACKGROUND.copy()
    if time.time() - t > 5:
        break

demo https://www.youtube.com/watch?v=XgLF2BWP0Rs#?t=16s


Clone this wiki locally