1

I would like to detect the previous movement of the character to display an animation in relation of the previous mouvement. For example, if the previous mouvement is 'move_right', when the character stopped, I would like to display an image turned to the right. And the same way for the left. Can you help me ?

EDIT

I'll put you a portion of the concerned code, hope it will help you.

import pygame
import constants
import time


clock = pygame.time.Clock()



class Player(pygame.sprite.Sprite):

    def __init__(self):
        super().__init__()
        self.velocity = 5
        self.HeatBox = constants.S_PLAYER_HEATBOX
        self.rect = self.HeatBox.get_rect()
        self.rect.x = 500
        self.rect.y = 300
        self.WalkCount = 0

    def move_right(self):
        self.rect.x += self.velocity

    def move_left(self):
        self.rect.x -= self.velocity

    def move_up(self):
        self.rect.y -= self.velocity

    def move_down(self):
        self.rect.y += self.velocity


def draw_game():

    global SURFACE_MAIN, Player

    SURFACE_MAIN.fill(constants.COLOR_DEFAULT_BG)

    SURFACE_MAIN.blit(constants.S_BACKGROUND, (0, 0))

    SURFACE_MAIN.blit(constants.S_PLAYER_HEATBOX, game.player.rect)

    if game.player.WalkCount + 1 >= 40:
         game.player.WalkCount = 0

    if game.pressed.get(pygame.K_RIGHT): 

    SURFACE_MAIN.blit(constants.S_PLAYER_W_RIGHT[game.player.WalkCount//10], 
    game.player.rect)
    game.player.move_right()

    if game.pressed.get(pygame.K_LEFT):

    SURFACE_MAIN.blit(constants.S_PLAYER_W_LEFT[game.player.WalkCount//10], 
    game.player.rect)  
    game.player.move_left()

    if game.pressed.get(pygame.K_UP):
        game.player.move_up()

    if game.pressed.get(pygame.K_DOWN):
        game.player.move_down()


    pygame.display.flip()



def game_main_loop():

    game_quit = False

    while not game_quit:

        clock.tick(60)

        events_list = pygame.event.get()

        for event in events_list:
            if event.type == pygame.QUIT:
                game_quit = True

            elif event.type == pygame.KEYDOWN:
                game.pressed[event.key] = True
            elif event.type == pygame.KEYUP:
                game.pressed[event.key] = False

        draw_game()

    pygame.quit()
    exit()


def game_initialize():

    global SURFACE_MAIN, Player

    pygame.init()

    pygame.display.set_caption("RogueLike")
    SURFACE_MAIN = pygame.display.set_mode((constants.GAME_WIDTH, 
    constants.GAME_HEIGHT))

class gameplay:

    def __init__(self):
        self.player = Player()
        self.pressed = {}

game = gameplay()

if __name__ == '__main__':
game_initialize()
game_main_loop()

In a file named constants.py I have all my sprites

S_BACKGROUND = pygame.image.load('Sprites/Map/Map_Test_3.png')                              
S_PLAYER_HEATBOX = pygame.image.load('Sprites/Heros/Knight/HeatBox.png')
S_PLAYER_RIGHT = 
             [pygame.image.load('Sprites/Heros/Knight/Right/sprite_0.png'),
              pygame.image.load('Sprites/Heros/Knight/Right/sprite_1.png'),
              pygame.image.load('Sprites/Heros/Knight/Right/sprite_2.png'),
              pygame.image.load('Sprites/Heros/Knight/Right/sprite_3.png')]
S_PLAYER_LEFT = 
             [pygame.image.load('Sprites/Heros/Knight/Left/sprite_0.png'),
              pygame.image.load('Sprites/Heros/Knight/Left/sprite_1.png'),
              pygame.image.load('Sprites/Heros/Knight/Left/sprite_2.png'),
              pygame.image.load('Sprites/Heros/Knight/Left/sprite_3.png')]
S_PLAYER_W_RIGHT = 
             [pygame.image.load('Sprites/Heros/Knight/W_Right/sprite_0.png'),
              pygame.image.load('Sprites/Heros/Knight/W_Right/sprite_1.png'),
              pygame.image.load('Sprites/Heros/Knight/W_Right/sprite_2.png'),
              pygame.image.load('Sprites/Heros/Knight/W_Right/sprite_3.png')]
S_PLAYER_W_LEFT = 
             [pygame.image.load('Sprites/Heros/Knight/W_Left/sprite_0.png'),
              pygame.image.load('Sprites/Heros/Knight/W_Left/sprite_1.png'),
              pygame.image.load('Sprites/Heros/Knight/W_Left/sprite_2.png'),
              pygame.image.load('Sprites/Heros/Knight/W_Left/sprite_3.png')]

# Left : means sprites turned to the right when the character is stopped.
# Right : means sprites turned to the left when the character is stopped.
# W_Left : means sprites turned to theleft when the character is walking.
# W_Right : means sprites turned to the right when the character is walking.
# S_ : means Sprites
Dyrockw
  • 13
  • 4

1 Answers1

0

My understanding of your question is that you wish the Player sprite to "face" the direction of the most-previous movement. For example, if the player last moved left, the sprite should display as facing left.

This sort of thing is quite easily achieved by setting the Sprite image value to the correct bitmap. There's no explanation for it, so I don't quite understand what a "HeatBox" represents, so I will ignore that. Both image and rect are special within a PyGame Sprite Class. They are used by the sprite library code to draw, and detect collisions for the sprite. It would be better to use another variable name for some other purpose.

class Player(pygame.sprite.Sprite):
    DIR_LEFT = 0 
    DIR_RIGHT= 1       # This might be better as an Enumerated Type
    DIR_UP   = 2
    DIR_DOWN = 3

    def __init__(self):
        super().__init__()
        self.velocity  = 5
        self.heat_box  = constants.S_PLAYER_HEATBOX
        self.heat_rect = self.HeatBox.get_rect()
        self.image     = S_PLAYER_LEFT[0]             # Initially face left
        self.rect      = self.image.get_rect()
        self.rect.x    = 500
        self.rect.y    = 300
        self.walk_count= 0                            # animation frame count
        self.last_dir  = Player.DIR_LEFT              # direction last walked

    def move_right(self):
        if ( self.last_dir != Player.DIR_RIGHT ):     # changed direction?
            self.walk_count = -1                      # so next-frame is 0
            self.last_dir = Player.DIR_RIGHT          # now face right
        # continue walking right
        self.rect.x += self.velocity
        self.walk_count += 1
        if ( walk_count >= len( S_PLAYER_W_RIGHT ) ): # loop the animation
            self.walk_count = 0
        self.image = S_PLAYER_W_RIGHT[ self.walk_count ]

    def move_left(self):
        if ( self.last_dir != Player.DIR_LEFT ):      # changed direction?
            self.walk_count = -1                      # so next-frame is 0
            self.last_dir = Player.DIR_LEFT           # now face left
        # continue walking left
        self.rect.x -= self.velocity
        self.walk_count += 1
        if ( walk_count >= len( S_PLAYER_W_LEFT ) ):  # loop the animation
            self.walk_count = 0
        self.image = S_PLAYER_W_LEFT[ self.walk_count ]

    # TODO: Much the same for Up/Down too

When the direction changes, the animation-loop counter (walk_count) is reset, and the bitmap for the new direction assigned into Player.image, and remembered in Player.last_dir.

So basically the Player class remembers both the direction the sprite is facing, and which animation frame has been drawn previously. If the player continues to move in the same direction the animation is stepped-through for each movement. If the direction has changed, the animation resets for the other directional-set of bitmaps.

To draw your player to the screen, add it to a sprite group, and tell the group to draw itself in your main loop. As a bonus, you can then use all the sprite-group collision functions too.

player_group = pygame.sprite.GroupSingle()
player_group.add( Player() )                  # create the player

...

# main loop
player_group.draw( screen )

Note: CamelCase variable names changed to lower_case to match PEP-8 Guidelines.

Kingsley
  • 14,398
  • 5
  • 31
  • 53