Pygame窗口崩溃和精灵表问题

时间:2019-03-16 19:04:23

标签: python python-3.x pygame sprite-sheet

因此,我一直在测试此代码,我找到了一个有关如何在pygame中添加精灵表的教程,并决定尝试以下代码: https://www.spriters-resource.com/3ds/dragonballzextremebutoden/sheet/67257/

我按照视频中的说明进行操作,并计算了列和行,这是我的代码:

pygame.init()
CLOCK = pygame.time.Clock()
DS = pygame.display.set_mode((W, H))
FPS = 60

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][2], self.cells[cellIndex][3]))


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

CENTER_HANDLE = 6

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), 20, 10)
    DS.blit(bg,(0,0))

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

s = spritesheet("Number18.png", 58, 6)行的数字为 58、6 ,基本上是我在此spritesheet块上计算的行数和列数,但是我遇到了诸如pygame窗口之类的问题在“不响应”上,图片无法加载,并且我无法移动pygame屏幕。

1 个答案:

答案 0 :(得分:1)

  

我遇到诸如“未响应”的pygame窗口之类的问题,[...]

首先要做的是将事件循环添加到应用程序的主循环中。
pygame.event从队列中删除未决事件消息并返回。 至少您应该处理QUIT事件。设置主循环False的控制变量的值:

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

精灵表中的图块大小不相等。将cells列表限制为工作表中具有相同大小的某些部分。
请尝试以下操作:

class spritesheet:
    def __init__(self, filename, py, tw, th, tiles):
        self.sheet = pygame.image.load(filename).convert_alpha()

        self.py = py
        self.tw = tw
        self.th = th
        self.totalCellCount = tiles

        self.rect = self.sheet.get_rect()
        w, h = tw, th
        hw, hh = self.cellCenter = (w / 2, h / 2)

        self.cells = [(1+i*tw, self.py, tw-1, th-1) for i in range(tiles)]
        self.handle = list([
            (0,0), (-hw, 0), (-w, 0),
            (0, -hh), (-hw, -hh), (-w, -hh),
            (0, -h), (-hw, -h), (-w, -h),])
s = spritesheet('Number18.png', 1085, 80, 134, 8)

  

[...]图片无法加载[...]

确保图像位于应用程序的工作目录中。
如果要绘制电子表格的子图像,则必须将pygame.Surface.blitarea参数(第3个参数)设置为子图像的矩形区域:

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

  

[...]我无法移动[...]

您必须更改子画面的位置。处理KEYDOWN事件。存储精灵的位置(pxpy)。按下K_UPK_DOWNK_LEFTK_RIGHT键时更改位置:

run = True
px, py, speed = HW, HH, 10
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                py -= speed
            elif event.key == pygame.K_DOWN:
                py += speed
            elif event.key == pygame.K_LEFT:
                px -= speed
            elif event.key == pygame.K_RIGHT:
                px += speed