Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
522 views
in Technique[技术] by (71.8m points)

python - Invalid destination position for blit error, not seeing how

I am programming a game that's basically using sprites, I was using a sprite sheet for this code and ended up getting the

"invalid destination for blit"

error.

class spritesheet:
    def __init__(self, filename, cols, rows):
        self.sheet = pygame.image.load(filename).convert_alpha()

        self.cols = cols
        self.rows = rows
        self.totalCellCount = cols * rows

        self.rect = self.sheet.get_rect()
        w = self.cellWidth = self.rect.width / cols
        h = self.cellHeight = self.rect.height / rows
        hw, hh = self.cellCenter = (w / 2, h / 2)

        self.cells = list([(index % cols * w, index / cols * h, w, h) for index in range(self.totalCellCount)])
        self.handle = list([
            (0,0), (-hw, 0), (-w, 0),
            (0, -hh), (-hw, -hh), (-w, -hh),
            (0, -h), (-hw, -h), (-w, -h),])

    def draw(self, surface, cellIndex, x, y, handle = 0):
        surface.blit(self.sheet, (x + self.handle[handle][0], y + self.handle[handle][1], self.cells[cellIndex]))


s = spritesheet('Number18.png', 6, 58)


CENTER_HANDLE = 4

Index = 0

#mainloop
run = True
while run:

    s.draw(DS, Index % s.totalCellCount, HW, HH, CENTER_HANDLE)
    Index +=1

    pygame.draw.circle(DS, WHITE, (HW, HW), 2, 0)

    pygame.display.update()
    CLOCK.tick(FPS)
    DS.fill(BLACK)

This is basically my whole code and I'm getting a problem on the line

 surface.blit(self.sheet, (x + self.handle[handle][0], y + self.handle[handle][1], self.cells[cellIndex]))

constantly giving out the error that this is an invalid destination position for the blit, i've also noticed that my index has an effect on it too but i don't know what to do.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The 2nd (dest) parameter to pygame.Surface has to be either a position (a tuple (x, y)) or a rectangle (a tuple (x, y, h, w)).

What yo do is to pass a tuple (x, y, list):

surface.blit(self.sheet, 
    (x + self.handle[handle][0], y + self.handle[handle][1], self.cells[cellIndex]))

If you want to set the destination by the position (x + self.handle[handle][0], y + self.handle[handle][1]) and the width and height of self.cells[cellIndex], then it has to be:

 hdl = self.handle[handle]
 cell = self.cells[cellIndex]
 surface.blit(self.sheet, (x + hdl[0], y + hdl[1], cell[2], cell[3]))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...