0

I am relatively new to Python and started messing around with pygame a few days ago. I made a sprite sheet in photoshop and I am trying to use it to animate the player in the game. The sprite sheet has 8 images, 4 for walking left and 4 for walking right. I want to show these animations at the correct time. I have tried many different methods I've found online but I haven't managed to get any working and I am still quite confused about it.

This is the Char class that controls the character's movement and sprite. I tried indexing the sprite and changing the index in the update method but I understand this isn't possible but I've left it in to show what I was trying to do. Any help would be appreciated!

import pygame, sys
from pygame.sprite import Sprite

class Char(Sprite):

    def __init__(self, pf_game):

        """initialise char and set its starting location"""
        self.screen = pf_game.screen
        self.settings = pf_game.settings
        self.screen_rect = pf_game.screen.get_rect()


        self.dog_sprite = pygame.image.load('images/dog_sprite_sheet.bmp').convert()
        self.current_sprite = 0
        self.dog_image = self.dog_sprite[self.current_sprite]
        self.rect = self.dog_image.get_rect()

        # start new char at the bottom of centre of the screen
        self.rect.midbottom = self.screen_rect.midbottom

        #store a decimal value for the ships horizontal position
        self.x = float(self.rect.x)
        self.y = float(self.rect.y)

        #movement flags
        self.moving_right = False
        self.moving_left = False
        self.is_jump = False

    def update(self):
        """Update the chars position based on movement flags"""
        #update the chars x value and create animation for moving right
        if self.moving_right and self.rect.right<self.screen_rect.right:
            if self.current_sprite == 7:
                self.current_sprite = 4
            self.dog_image = self.dog_sprite[self.current_sprite]
            self.current_sprite+=1
            self.x+= self.settings.char_speed

        #update the chars x value and create animation for moving left
        if self.moving_left and self.rect.left>0:
            if self.current_sprite == 3:
                self.current_sprite = 0
            self.dog_image = self.dog_sprite[self.current_sprite]
            self.current_sprite+=1
            self.x-= self.settings.char_speed

        

        #update rect object from self.x
        self.rect.x = self.x
        self.rect.y = self.y

    def blitme(self):
        """Draw the char at its curretn loaction"""
        self.screen.blit(self.dog_image, self.rect)

edit: I found a spritesheet class that seems to do what I want on http://www.pygame.org/wiki/Spritesheet. I am running into an error though when I try and input the coordinates. I get the error images_at() got multiple values for argument 'colorkey'. When i remove the colorkey I get the error images_at() takes from 2 to 3 positional arguments but 5 were given. This is the code I am using.

        self.dog_sprite=spritesheet.spritesheet('dog_sprite_sheet.bmp')
        self.left_images=[]
        self.right_images=[]
        self.left_images=self.dog_sprite.images_at((0,0,19,19),(20,0,19,19),(39,0,19,19),(58,0,19,19), colorkey=(255, 255, 255))
        self.right_images=self.dog_sprite.images_at((77,0,19,19),(96,0,19,19),(115,0,19,19),(134,0,19,19), colorkey=(255, 255, 255))
Valortome
  • 11
  • 5

1 Answers1

0

this may help:

def anim(anim_count):
    if anim_count < 6:
        screen.blit('anim1.png', player_position)
    if anim_count > 6 and anim_count < 12:
        screen.blit('anim2.png', player_position)

you should call anim at every frame, and then apply 1 to anim_count

xmurik
  • 1
  • Thanks, this will definitely help once I get the sprites in a list, but I am struggling to do that at the moment – Valortome Mar 28 '21 at 12:42
  • you are welcome. i just can't remember do pygame have a tool for cutting sprite sheet into an individual sprites – xmurik Mar 28 '21 at 13:17
  • As far as I know, it doesn't. If you look at my edit I found a spritesheet class that cuts sprite sheets and puts the individual sprites into a list but I'm getting errors when I try to do this. – Valortome Mar 28 '21 at 13:25
  • then do that manually or choose one of the game engines, there are have more functionality – xmurik Mar 28 '21 at 13:58
  • Ah, I managed to get it working, the example on http://www.pygame.org/wiki/Spritesheet was incorrect as there were a set of parentheses missing. – Valortome Mar 28 '21 at 15:03
  • i'm glad for you – xmurik Mar 28 '21 at 15:37