gpt4 book ai didi

python - blit 错误的目标位置无效,不知道如何处理

转载 作者:太空宇宙 更新时间:2023-11-03 21:08:51 25 4
gpt4 key购买 nike

我正在编写一个基本上使用 Sprite 的游戏,我在这段代码中使用了 Sprite 表,最终得到了

"invalid destination for blit"

错误。

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)

这基本上是我的全部代码,但我遇到了问题

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

不断地给出错误,这是blit的无效目标位置,我还注意到我的索引也对其有影响,但我不知道该怎么办。

最佳答案

第二个(目标)参数为 pygame.Surface必须是一个位置(元组 (x, y))或矩形(元组 (x, y, h, w))。

你要做的就是传递一个元组(x, y, list):

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

如果你想通过位置(x + self.handle[handle][0], y + self.handle[handle][1])以及宽度和高度来设置目的地self.cells[cellIndex],那么它必须是:

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

关于python - blit 错误的目标位置无效,不知道如何处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55199591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com