尝试运行我的代码时遇到2个错误,但可能还有更多错误,因为这是我第一次使用spritesheets。
第一个错误是,在调用Spritesheet时,它给了我一个错误,说我只需要3个参数时就给出了4个参数。我似乎只看到3个参数。第二个错误是此代码行(-1*halfSpriteWidth,-1*spriteHeight),(-1*spriteWidth,-1*spriteHeight)])
无法调用'tuple'对象。有人可以帮我解决这个问题。
class spritesheet:
def __init__(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(self.sheet(x + handle[handle][0], y + handle[handle][1]),cells[cellIndex])
player = spritesheet('player.png',4,4)
centerHandle = 4
index = 0
答案 0 :(得分:1)
您完全忘记了self
参数。您的__init__
方法应为:
def __init__(self,filename,columns,rows):
还有:
def draw(self,surface,cellIndex, x,y,handle = 0):
并且不要忘记,实例应记住的属性也需要在self
之前。
您可能想要:
def __init__(filename,columns,rows):
self.sheet = image.load(filename)
self.totalCellCount = columns * rows
self.rect = self.sheet.get_rect()
#...
self.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)])
,当您引用列表时,在draw
中也self.handle
中。顺便说一下,也不需要list
调用,方括号就足够了。
元组错误是由于self.handle
列表中两个元组之间缺少逗号引起的:
(0,-1*spriteHeight)(-1*halfSpriteWidth,-1*spriteHeight),
^^^ insert comma here