所以,我正在寻求帮助,将列表作为精灵类传递给你们,但是你们帮忙了,但我遇到了另一面墙。当我尝试循环精灵列表时出现问题。
这是精灵类:
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 超级(播放机,自)。的初始化(*基团)
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
答案 0 :(得分:1)
sprite_system().slice_sheet()
在哪里?此行无效
pygame.sprite.Group.sprites