大家好,我又来找你们了,需要帮助。今天我正在使用精灵并制作一个系统,该系统将分割精灵表并将所有切片放入 python 和 pygame 的列表中。当我想将该列表传递给精灵类以更新它并接受玩家移动时,问题就来了。当我尝试在 idel 中运行我的代码时,它告诉我,
slef.image = sprites[frame]
TypeError: 'Group' object does not support indexing
而且我不知道如何才能使其正常运行。
这是我用来切割精灵表的系统:
class sprite_system(object):
def slice_sheet(self,sprite_sheet,sprite_start_x,sprite_start_y,sprite_size_x,sprite_size_y,sheet_frames):
frame = []
sheet_start = 1
frame_pos = 0
sprite_start_x_base = sprite_size_x
while sheet_start <= sheet_frames:
sheet = sprite_sheet
sheet.set_clip(pygame.Rect(sprite_start_x,sprite_start_y,sprite_size_x,sprite_size_y))
sprite_clip = sheet.subsurface(sheet.get_clip())
frame.append(sprite_clip)
sprite_start_x += sprite_start_x_base
sheet_start += 1
return frame
这是我用来制作和更新精灵的系统:
class player(pygame.sprite.Sprite):
def __init__ (self,sprites,*groups):
super(player,self).__init__(*groups)
frame = 0
slef.image = sprites[frame]
self.rect = pygame.rect.Rect((320,240),sprites.get_size())
def update(self):
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
self.rect.x -= 10
frame += 1
if key[pygame.K_RIGHT]:
self.rect.x += 10
frame += 1
if key[pygame.K_UP]:
self.rect.y -= 10
frame += 1
if key[pygame.K_DOWN]:
self.rect.y += 10
frame += 1
if frame > 3:
frame = 0
最后是我用来将所有这些放在一起的主游戏循环:
class Game(object):
def main(self, screen):
sprite = pygame.image.load('test.png')
image = sprite_system().slice_sheet(sprite,0,0,64,64,4)
sprites = pygame.sprite.Group()
self.player = player(sprites)
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
return
screen.fill((200, 200, 200))
sprites.update()
sprites.draw(screen)
pygame.display.flip()
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((640, 480))
Game().main(screen)
我希望这足以获得一些帮助,如果你需要知道其他任何事情,我很乐意提供帮助,谢谢~Eddy