I'm a beginner when it comes to python and I'm making a pygame game and want to have the player's sprite change to a different image frame when it goes up and down and return back to its original image afterwards. This is my code at the moment:
# Define the Player object by extending pygame.sprite.Sprite
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
self.surf = pygame.image.load("Sprites/Characters/tile_0002.png").convert() # load image for sprite
self.surf.set_colorkey((0, 0, 0), RLEACCEL) # set the colour to the image's values
self.surf = pygame.transform.scale(self.surf, [25, 25]) # scale sprite image down
self.rect = self.surf.get_rect(center=(-SCREEN_WIDTH + 50, SCREEN_HEIGHT,)) # where the sprite starts on the screen
# when the sprite is going up or down
def vertical(self):
self.surf = pygame.image.load("Sprites/Characters/tile_0003.png").convert()
self.surf.set_colorkey((0, 0, 0), RLEACCEL)
self.surf = pygame.transform.scale(self.surf, [25, 25])
# Move the sprite based on user keypresses
def update(self, pressed_keys):
if pressed_keys[K_UP]:
self.rect.move_ip(0, -5)
self.vertical() # change the image frame
if pressed_keys[K_DOWN]:
self.rect.move_ip(0, 5)
self.vertical() # change the image frame
if pressed_keys[K_LEFT]:
self.rect.move_ip(-5, 0)
if pressed_keys[K_RIGHT]:
self.rect.move_ip(5, 0)
# Keep player on the screen
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > SCREEN_WIDTH:
self.rect.right = SCREEN_WIDTH
if self.rect.top <= 0:
self.rect.top = 0
if self.rect.bottom >= 500:
self.rect.bottom = 500
as you can see I thought that making a function for changing the frame would change it when going up, but switch it back once you either stop moving or move left or right to look like this: normal frame up and down frame
Making another function for making it the original frame might work but is there a simpler way of doing this?
Create functions that set the sprite dependency on an argument and change the image depending on the key pressed, e.g.:
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
self.surf_horizontal = pygame.image.load("Sprites/Characters/tile_0002.png").convert()
self.surf_horizontal.set_colorkey((0, 0, 0), RLEACCEL)
self.surf_horizontal = pygame.transform.scale(self.surf_horizontal, [25, 25])
self.surf_vertical = pygame.image.load("Sprites/Characters/tile_0003.png").convert()
self.surf_vertical.set_colorkey((0, 0, 0), RLEACCEL)
self.surf_vertical = pygame.transform.scale(self.surf_vertical, [25, 25])
self.surf = self.surf_horizontal
self.rect = self.surf.get_rect(center=(-SCREEN_WIDTH + 50, SCREEN_HEIGHT,))
# when the sprite is going up or down
def change_image(self, image_type):
if image_type == "vertical":
self.surf = self.surf_vertical
else:
self.surf = self.surf_horizontal
# Move the sprite based on user keypresses
def update(self, pressed_keys):
image_type = "horizontal"
if pressed_keys[K_UP]:
self.rect.move_ip(0, -5)
image_type = "vertical"
if pressed_keys[K_DOWN]:
self.rect.move_ip(0, 5)
image_type = "vertical"
if pressed_keys[K_LEFT]:
self.rect.move_ip(-5, 0)
if pressed_keys[K_RIGHT]:
self.rect.move_ip(5, 0)
self.change_image(image_type)