我无法让Spritesheet在pygame中工作

时间:2019-06-22 11:22:34

标签: python pygame sprite-sheet

我得到一个:

  

spritesheet对象没有属性totalCellCount

这是我第一次使用精灵。

class spritesheet:
    def __init__(self,filename,columns,rows):
        sheet = image.load(filename)
        totalCellCount = columns * rows
        rect = sheet.get_rect()
        spriteWidth = cellWidth = rect.width / columns
        spriteHeight = cellHeight = rect.height / rows
        halfSpriteWidth, halfSpriteHeight = cellCenter = (spriteWidth/2,spriteHeight/2)
        cells = list([(index % columns * spriteWidth, index / columns * spriteHeight,spriteWidth,spriteHeight)for index in range(totalCellCount)])
        handle = list([
            (0,0), (-1*halfSpriteWidth,0),(-1*spriteWidth,0),
            (0,-1*halfSpriteHeight),(-1*halfSpriteWidth,-1*halfSpriteHeight),
            (-1*spriteWidth,-1*halfSpriteHeight),(0,-1*spriteHeight),
            (-1*halfSpriteWidth,-1*spriteHeight),(-1*spriteWidth,-1*spriteHeight)])
    def draw(surface,cellIndex, x,y,handle = 0):
        surface.blit(sheet(x + handle[handle][0], y + handle[handle][1]),cells[cellIndex])

下面的部分在游戏循环中仅供参考

player.draw(screen,Index % player.totalCellCount, halfSpriteWidth,halfSpriteHeight,centerHandle)
    draw.circle(screen,WHITE,(halfSpriteWidth,halfSpriteHeight),2,0)

0 个答案:

没有答案