您如何解决:“ IndexError:列表索引超出范围”?

时间:2019-08-17 01:43:46

标签: pygame python-3.7.4

我正在制作平台游戏,如果玩家击中盒子,就会有加电盒。我希望当玩家从底部击打时发出能量。我有开机动画的帧索引。但是,每当我打方框时,都会出现此错误:“ IndexError:列表索引超出范围”。谁能解决这个问题?

Traceback (most recent call last):
  File "/Users/qingduliu/PycharmProjects/Platformer/Video Game 
    Copy.py", line 1399, in <module>
    main()
  File "/Users/qingduliu/PycharmProjects/Platformer/Video Game . 
    Copy.py", line 1361, in main
    current_level.update()
  File "/Users/qingduliu/PycharmProjects/Platformer/Video Game 
    Copy.py", line 863, in update
    self.powerup_box_list.update()
  File "/Users/qingduliu/Platformer/lib/python3.7/site- 
     packages/pygame/sprite.py", line 463, in update
     s.update(*args)
  File "/Users/qingduliu/PycharmProjects/Platformer/Video Game 
     Copy.py", line 601, in update
     self.handle_states()
  File "/Users/qingduliu/PycharmProjects/Platformer/Video Game 
     Copy.py", line 610, in handle_states
    self.bumped()
  File "/Users/qingduliu/PycharmProjects/Platformer/Video Game 
     Copy.py", line 643, in bumped
     self.group.add(HealthPlus(self.rect.centerx, self.rect.y))
  File "/Users/qingduliu/PycharmProjects/Platformer/Video Game 
     Copy.py", line 759, in __init__
     super(HealthPlus, self).__init__(x, y, name)
  File "/Users/qingduliu/PycharmProjects/Platformer/Video Game 
     Copy.py", line 675, in __init__
    self.image = self.frames[self.frame_index]
IndexError: list index out of range

class Powerup(pygame.sprite.Sprite):
    def __init__(self, x, y, contents=None, group=None):
    super().__init__()
    self.sprite_sheet = SpriteSheet('tiles_spritesheet.png')

    self.frames = []
    self.setup_frames()
    self.frame_index = 0
    self.image = self.frames[self.frame_index]

    self.rect = self.image.get_rect()
    self.rect.x = x
    self.rect.y = y

    self.animation_timer = 0
    self.first_half = True  # First half of animation cycle
    self.state = RESTING
    self.rest_height = y
    self.gravity = 1.2
    self.y_vel = 0

    self.items_available = 1

    self.contents = contents
    self.group = group

    def update(self):
        self.handle_states()

    def handle_states(self):
        if self.state == RESTING:
            self.resting()
        elif self.state == BUMPED:
            self.bumped()
        elif self.state == OPENED:
            self.opened()

    def resting(self):
        now = pygame.time.get_ticks()
        if self.first_half:
            if self.frame_index == 0:
                if (now - self.animation_timer) > 600:
                    self.frame_index += 1
                    self.animation_timer = now
                    self.first_half = False
        else:
            if self.frame_index == 1:
                if (now - self.animation_timer) > 300:
                    self.frame_index -= 1
                    self.first_half = True
                    self.animation_timer = now

        self.image = self.frames[self.frame_index]

    def bumped(self):
        self.rect.y += self.y_vel
        self.y_vel += self.gravity

        if self.rect.y > self.rest_height + 5:
            self.rect.y = self.rest_height
            self.state = OPENED
            if self.contents == 'health plus':
                 self.group.add(HealthPlus(self.rect.centerx, 
                 self.rect.y))
            elif self.contents == 'double attack':
                self.group.add(DoubleAttack(self.rect.centerx, 
                self.rect.y))
            elif self.contents == 'half damage':
                self.group.add(HalfDamage(self.rect.centerx, 
                self.rect.y))

         self.frame_index = 2
         self.image = self.frames[self.frame_index]

    def start_bump(self):
        self.y_vel = -6
        self.state = BUMPED

        if self.contents == 'coin':
            self.group.add(Coin(self.rect.centerx, self.rect.y,))

    def opened(self):
        pass


class Powerup(pygame.sprite.Sprite):
    def __init__(self, x, y, name):

        super(Powerup, self).__init__()

        self.sprite_sheet = SpriteSheet('items1_spritesheet.png')
        self.frames = []
        self.frame_index = 0
        self.image = self.frames[self.frame_index]
        self.rect = self.image.get_rect()
        self.rect.centerx = x
        self.rect.y = y
        self.state = REVEALED
        self.y_vel = -1
        self.x_vel = 0
        self.direction = RIGHT
        self.box_height = y
        self.gravity = 1
        self.max_y_vel = 8
        self.animate_timer = 0
        self.name = name

    def setup_powerup(self, x, y, name, create_frames):
        self.sprite_sheet = SpriteSheet('items1_spritesheet')
        self.frames = []
        self.frame_index = 0
        create_frames()
        self.image = self.frames[self.frame_index]
        self.rect = self.image.get_rect()
        self.rect.centerx = x
        self.rect.y = y
        self.state = REVEALED
        self.y_vel = -1
        self.x_vel = 0
        self.direction = RIGHT
        self.box_height = y
        self.gravity = 1
        self.max_y_vel = 8
        self.animate_timer = 0
        self.name = name

    def get_image(self, x, y, width, height):

        image = pygame.Surface([width, height]).convert()
         rect = image.get_rect()

        image.blit(self.sprite_sheet.sprite_sheet, (0, 0), (x, y, 
                   width, height))
        image.set_colorkey(BLACK)

        image = pygame.transform.scale(image, (int(rect.width * 0.9), 
                                       int(rect.height * 0.9)))

        return image

    def update(self, game_info):
        self.handle_states()

    def handle_state(self):
        pass


    def revealing(self):

        self.rect.y += self.y_vel

        if self.rect.bottom <= self.box_height:
            self.rect.bottom = self.box_height
            self.y_vel = 0
            self.state = SLIDE

    def sliding(self):
        if self.direction == RIGHT:
            self.x_vel = 3
        else:
            self.x_vel = -3

    def falling(self):
        if self.y_vel < self.max_y_vel:
            self.y_vel += self.gravity


class HealthPlus(Powerup):

    def __init__(self, x, y, name='health plus'):
        super(HealthPlus, self).__init__(x, y, name)
        self.setup_powerup(x, y, name, self.create_frames)

    def create_frames(self):
        self.frames.append(self.get_image(144, 218, 70, 70))

    def handle_state(self):
        if self.state == REVEALED:
            self.revealing()
        elif self.state == SLIDE:
            self.sliding()
        elif self.state == FALL:
            self.falling()


class Player(pygame.sprite.Sprite):

    def __init__(self)
        super().__init__()
        # Attributes

    def update(self):
        powerup_box_hit_list = pygame.sprite.spritecollide(self, 
                             self.level.powerup_box_list, False)
        for powerup_box in powerup_box_hit_list:
            if self.change_y > 0:
            self.rect.bottom = powerup_box.rect.top
        elif self.change_y < 0:
            self.rect.top = powerup_box.rect.bottom
            powerup_box.start_bump()

        self.change_y = 0

1 个答案:

答案 0 :(得分:1)

您已经初始化了frames数组,但是没有添加任何值。 如果不将值添加到框架数组,则会收到索引超出范围错误。

self.image = self.frames [self.frame_index]

在上一行中,您尝试从self.frames的索引self.frame_index访问值。但是数组中没有可用的值。