pygame sprite sheet cutter返回了地下错误,但是大小似乎是准确的

时间:2013-11-30 03:26:46

标签: python python-2.7 pygame sprite sprite-sheet

所以我有这个来自OpenGameArt.org enter image description here

的图像文件

它是576 X 256像素,所以我有这个功能从中提取图像:

def sprite_sheet_load(colorKey, spriteLocX, spriteLocY, spriteSizeX, spriteSizeY, fileName):
    '''Purpose: to extract a sprite from a sprite sheet at the chosen location'''
    '''credit to Stackover flow user hammyThePig for original code'''

    sheet = pygame.image.load(fileName).convert() #loads up the sprite sheet. convert makes sure the pixel format is coherent
    sheet.set_colorkey(colorKey) #sets the color key

    sprite = sheet.subsurface(pygame.Rect(spriteLocX, spriteLocY, spriteSizeX, spriteSizeY)) #grabs the sprite at this location

    return sprite

在我的游戏的Player类中,我有这个代码在for循环中调用上面的函数

    playerImages = []   #lists for the player images
    playerDeathImages = [] # ""

    ###Image Gathering Section VVV ####

    spriteXLoc = 0 #starting x location for the sprite
    spriteYLoc = 0 #starting y location for the sprite
    spriteXSize = 64
    spriteYSize = 64

    #Grab the images for the main character's walking poses
for y in range(0,9): #handle the columns of sprite sheet
    for x in range(0,4): #handle the rows of sprite sheet
                playerImages.append(sprite_sheet_load(black, spriteXLoc, spriteYLoc, spriteXSize, spriteYSize, "mainCharacter.png"))
                spriteXLoc += spriteXSize
    spriteXLoc = 0 #reset the inital x value
    spriteYLoc += spriteYSize #increment the y value

SpriteLocX达到576,即图像的最大长度,到那时,它应该重置并执行下一行,但相反,我得到一个地下错误,说明它已经过去,或者更具体地说:

    Traceback (most recent call last):
      File "game.py", line 97, in <module>
        player = Player() #create a player object
      File "game.py", line 63, in __init__
        playerImages.append(sprite_sheet_load(black, spriteXLoc, spriteYLoc, spriteXSize, spriteYSize, "mainCharacter.png"))
      File "game.py", line 33, in sprite_sheet_load
        sprite = sheet.subsurface(pygame.Rect(spriteLocX, spriteLocY, spriteSizeX, spriteSizeY)) #grabs the sprite at this location
    ValueError: subsurface rectangle outside surface area

            spriteXLoc = 0 #reset the inital x value
            spriteYLoc += spriteYSize #increment the y value

我之前使用过此代码没有任何问题。出了什么问题?另外,除了将宽度和高度分别除以列数和行数之外,还有更准确的方法来获得Spirte Size X和Y尺寸吗?

1 个答案:

答案 0 :(得分:0)

您尝试获得9行和4列 - 将x范围替换为y范围

for y in range(0,4): #handle the rows of sprite sheet
    for x in range(0,9): #handle the colums of sprite sheet