Blit错误的目标位置无效,无法查看 - python

我正在编写一个基本上使用精灵的游戏,我为此代码使用了精灵表,最终得到了

  “ 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的无效目标位置,我也注意到我的索引也对此产生了影响,但我不知道该怎么做。

python参考方案

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 sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…

如何在PyQt4的动态复选框列表中检查stateChanged - python

所以我要从PyQt4的列表中添加复选框。但是我找不到在Window类中对每个状态使用stateChanged的方法。这是从列表元素添加它们的功能: def addCheckbox(self): colunas = Graphic(self.caminho).getColunas() for col in colunas: c = QtGui.QCheckBo…

Python pytz时区函数返回的时区为9分钟 - python

由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

如何打印浮点数的全精度[Python] - python

我编写了以下函数,其中传递了x,y的值:def check(x, y): print(type(x)) print(type(y)) print(x) print(y) if x == y: print "Yes" 现在当我打电话check(1.00000000000000001, 1.0000000000000002)它正在打印:<…