Home > Software engineering >  how to create an illusion of animations in pygame 2.1.2
how to create an illusion of animations in pygame 2.1.2

Time:02-23

I am making a simple platformer game using pygame 2.1.2 and python 3.10. Everything was going well until I ran into a puzzle, which was simply switching between 2 images.

I've been trying to get it working for about 2 days and haven't gotten anything but the same thing, with only one showing at a time.

Here is my full code:

# import library/libraries
import pygame as pg
pg.init ()

# creates the window
display = pg.display.set_mode ((960, 720))

# window details
pg.display.set_caption ("Pygame Window")

# player
player_img_idle = pg.image.load ("player_idle.png")
player_img_left1 = pg.image.load ("player_left1.png")
player_img_right1 = pg.image.load ("player_right1.png")
player_img_left2 = pg.image.load ("player_left2.png")
player_img_right2 = pg.image.load ("player_right2.png")
player_img = player_img_idle
player_x = 10
player_y = 10
player_x_change = 0
player_y_change = 0.5

# definitions

def player (x, y):
    display.blit (player_img, (x, y))

# keeps the window open
execute = True
while execute:
    # sets window color (red, green, blue)
    display.fill ((255, 255, 255))
    # events
    for event in pg.event.get ():
        # checks if the X button has been pressed
        if event.type == pg.QUIT:
            execute = False

        # checks if a key has been pressed
        if event.type == pg.KEYDOWN:
            if event.key == pg.K_w and player_y == 675:
                player_y_change = -2

            if event.key == pg.K_a:
                player_x_change = -0.5

            if event.key == pg.K_d:
                player_x_change = 0.5

        # checks if a key has been released
        if event.type == pg.KEYUP:
            if event.key == pg.K_a:
                player_x_change = 0
                player_img = player_img_idle

            if event.key == pg.K_d:
                player_x_change = 0
                player_img = player_img_idle

    # moves actors
    player_x = player_x   player_x_change
    player_y = player_y   player_y_change
    player_y_change = player_y_change   0.0075
    # border
    if player_x > 915:
        player_x = 915
    if player_x < 0:
        player_x = 0
    if player_y > 675:
        player_y = 675
    # loads actors
    player (player_x, player_y)
    # updates game window
    pg.display.update ()

I am not certain where or if my code is supposed to be inside the branch of the events but there aren't any places where I think It would've worked.

Also, if you need the dimensions for the player, it is 45x45.

CodePudding user response:

It has already been answered here. Look, the top answer explains how to deal with animation in pygame: Animated sprite from few images

CodePudding user response:

Choose the image in the player function depending on player_x_change. If player_x_change < 0 use one of the left images, if > 0 use one of the right images, else use player_img_idle. Add a counter that increases with each frame (frame_count). Calculate an animation counter that increases less frequently (animation_count = walk_count // 10). Make a list for the right and left images. Use the % (modulo) operator to get an image from the lists depending on animation_count (The % computes the remainder of an integral division):

left_images = [player_img_left1, player_img_left2]
right_images = [player_img_right1, player_img_right2]
frame_count = 0

def player(x, y):
    global frame_count, player_img

    frame_count  = 1
    animation_count = frame_count // 10

    if player_x_change < 0:
        player_img = left_images[animation_count % len(left_images)]
    elif player_x_change > 0:
        player_img = right_images[animation_count % len(right_images)]
    else:
        player_img = player_img_idle

    display.blit(player_img, (x, y))
  • Related