所以我一直在测试这段代码,我找到了一个关于如何在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屏幕。你知道吗
您要做的第一件事是将en事件循环添加到应用程序的主循环中。
} 事件。为主循环
pygame.event
从队列中删除挂起的事件消息并返回它。 至少应该处理^{False
设置控制变量的值:精灵表中的瓷砖大小不相等。将
cells
列表限制为具有相同大小的工作表的某些部分。请尝试以下操作:
确保图像位于应用程序的工作目录中。} 的
如果要绘制spritsheet的子图像,则必须将^{
area
参数(第3个参数)设置为子图像的矩形区域:你必须改变精灵的位置。处理
KEYDOWN
事件。存储精灵的位置(px
,py
)。按K_UP
、K_DOWN
、K_LEFT
或K_RIGHT
键时更改位置:相关问题 更多 >
编程相关推荐