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

标签 python python-3.x pygame python-requests

我正在编写一个基本上使用 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/

相关文章:

python - 是否有更短的方法或 pythonic 方法来生成遵循使用 BeautifulSoup 模式的自定义 html?

macos - 让 pygame 在 Macos 上显示除空白屏幕以外的任何内容的问题

python - CNTK 将标签索引转换为单热向量表示

java - 从 java 作为子进程运行 python 脚本

python - 将值附加到python中的两个关键defaultdict

python - Pygame 中的透明图像

python - 如何使用 pygame.MOUSEBUTTONDOWN?

python - 如何在python中附加子列表的元素

python - repr 调用会产生堆栈帧吗?

python-3.x - 如何使用Python提取点的x坐标