pygame sprite sheet 切割器返回地下错误,但尺寸似乎准确
Posted
技术标签:
【中文标题】pygame sprite sheet 切割器返回地下错误,但尺寸似乎准确【英文标题】:pygame sprite sheet cutter is returning subsurface error, but size seems to be accurate 【发布时间】:2013-12-16 06:19:33 【问题描述】:所以我有这个从 OpenGameArt.org 获得的图像文件
它是 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 尺寸吗?
【问题讨论】:
dividing
是您不知道精灵大小的唯一方法。
使用print
查看spriteLocX
、spriteLocY
【参考方案1】:
您尝试获得 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
【讨论】:
以上是关于pygame sprite sheet 切割器返回地下错误,但尺寸似乎准确的主要内容,如果未能解决你的问题,请参考以下文章