blit 错误的无效目标位置,不知道如何 [英] Invalid destination position for blit error, not seeing how
问题描述
我正在编写一个基本上使用精灵的游戏,我为此代码使用了精灵表并最终获得了
<块引用>blit 的目的地无效"
错误.
类精灵表:def __init__(self, filename, cols, rows):self.sheet = pygame.image.load(filename).convert_alpha()self.cols = colsself.rows = 行self.totalCellCount = cols * rowsself.rect = self.sheet.get_rect()w = self.cellWidth = self.rect.width/colsh = self.cellHeight = self.rect.height/行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)中心手柄 = 4指数 = 0#主循环运行 = 真运行时:s.draw(DS, 索引 % s.totalCellCount, HW, HH, CENTER_HANDLE)指数 +=1pygame.draw.circle(DS, WHITE, (HW, HW), 2, 0)pygame.display.update()时钟滴答(FPS)DS.fill(黑色)
这基本上是我的全部代码,我遇到了问题
<块引用> 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]))
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.
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]))
这篇关于blit 错误的无效目标位置,不知道如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!