所以只是有点尝试,我正在寻求帮助,将一个列表传递给一个精灵类,你们帮忙,但我遇到了另一面墙。当我尝试循环精灵列表时,问题就来了。
这是精灵类:
class Player(pygame.sprite.Sprite):
def __init__ (self,frames,*groups):
self.frame = 0
super(Player,self).__init__(*groups)
self.images = []
self.images.append(frames[self.frame])
self.image = self.images[self.frame]
self.rect = pygame.rect.Rect((320,240),self.image.get_size())
def update(self):
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
self.rect.x -= 10
self.frame += 10
if key[pygame.K_RIGHT]:
self.rect.x += 10
self.frame += 1
if key[pygame.K_UP]:
self.rect.y -= 10
self.frame += 1
if key[pygame.K_DOWN]:
self.rect.y += 10
self.frame += 1
if self.frame > 3:
self.frame = 0
这是主要的游戏循环:
class Game(object):
def main(self, screen):
sprite = pygame.image.load('test.png')
frames = sprite_system().slice_sheet(sprite,0,0,64,64,4)
sprites = pygame.sprite.Group()
self.player = Player(frames,sprites)
clock = pygame.time.Clock()
while 1:
clock.tick(30)
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()
pygame.sprite.Group.sprites
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((640, 480))
Game().main(screen)
当我设置 self.frame 时,我认为问题出现了,每次调用某些东西时它都会将其设置为 0,但我只是看不到它是什么,也许你们可以提供帮助
self.frame 在此处设置为 0 self.frame = 0 super(Player,self)。初始化(*组)
self.images = []
self.images.append(frames[self.frame])
self.image = self.images[self.frame]
self.rect = pygame.rect.Rect((320,240),self.image.get_size())
并且在这里也需要循环播放动画:
def update(self):
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
self.rect.x -= 10
self.frame += 10
if key[pygame.K_RIGHT]:
self.rect.x += 10
self.frame += 1
if key[pygame.K_UP]:
self.rect.y -= 10
self.frame += 1
if key[pygame.K_DOWN]:
self.rect.y += 10
self.frame += 1
if self.frame > 3:
self.frame = 0
编辑 :
精灵系统类
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