1

thought it would be a good idea to get some programming done during the Coronavirus lockdown. I'm trying to make an Undertale/Earthbound-type game in Pygame and simultaneously learn a bit of Python, but I'm struggling with importing spritesheets and performing character animation.

Here is my spritesheet class code:

class spritesheet:
    def __init__(self, fileName, rows, columns):
        self.sheet = pygame.image.load(fileName).convert_alpha()
        self.rows = rows
        self.columns = columns
        self.spriteNum = rows * columns
        self.sheetRect = self.sheet.get_rect()
        self.spriteWidth = self.sheetRect.width/columns
        self.spriteHeight = self.sheetRect.height/rows
        self.sprites = []
        for self.i in range(1, self.rows):
            for self.j in range(1, self.columns):
                self.sprites[self.i*self.j] = (self.j*self.spriteWidth,self.i*self.spriteHeight,self.spriteWidth,self.spriteHeight)

emilyWalk = spritesheet('Emily Walk Sheet.png', 1, 16)

I'm trying to create a list/array (self.sprites) that iterates through each "cell" in my spritesheet and indexes it using the counters i and j. I am thinking my syntax might be incorrect here.

Next, I have a player class with a "draw" method, where I am attempting to blit the appropriate rect within the emilyWalk spritesheet (using "emilyWalk.sprites[x]") to a surface:

class player(object):
    def __init__(self, x, y, width, height, vel):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vel = vel

    def draw(self, surface):
        if moveDown:
            surface.blit(emilyWalk.sheet, (self.x, self.y, emilyWalk.sprites[1]))

In my main loop I'm using pygame.key.get_pressed() to set moveDown = True, moveUp = True, etc. using W,A,S,D. When I press any key I get an error within my draw method: list index out of range.

I think the issue is that I haven't stored my array of rects properly. I believe once I have the rects in an indexed list I can use the "area" parameter of the blit function to pass an indexed rect from my list so that the correct cell from my spritesheet is displayed.

What am I doing wrong here?

Thanks for your time!

cbart
  • 13
  • 4

0 Answers0