【发布时间】:2019-08-07 14:09:23
【问题描述】:
所以我一直在测试这段代码,我找到了一个关于如何在 pygame 中添加 spritesheets 的教程,并决定尝试这个: 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 屏幕。
【问题讨论】:
-
"图片无法加载" 你确定图片在应用的工作目录中吗?出于调试原因使用图像的绝对路径。
-
可以在 self.sheet 赋值后尝试打印它吗?你有任何异常吗?
-
问题解决了吗?
标签: python python-3.x pygame sprite-sheet