Pygame窗口崩溃和精灵床单问题

2024-05-14 01:02:13 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我一直在测试这段代码,我找到了一个关于如何在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屏幕。你知道吗


Tags: rectselfindexhhdspygamesheetrows
1条回答
网友
1楼 · 发布于 2024-05-14 01:02:13

I'm getting problems such as the pygame window on "not responding", [...]

您要做的第一件事是将en事件循环添加到应用程序的主循环中。
pygame.event从队列中删除挂起的事件消息并返回它。 至少应该处理^{}事件。为主循环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)

[...] the image does not load up [...]

确保图像位于应用程序的工作目录中。
如果要绘制spritsheet的子图像,则必须将^{}area参数(第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])

[...] I can't move [...]

你必须改变精灵的位置。处理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

相关问题 更多 >