列表索引超出范围语法错误pygame

时间:2017-12-11 11:49:40

标签: python list pygame velocity

我正在通过pygame创建一个2D格斗游戏。目前我正在使用速度列表实现跳转到player1。然而,player1跳转并降落我想要的对象,但当它回到平台上时会出现错误信息: self.y + = velocity [self.velocity_index],列表索引超出范围。

这是我主要代码中的速度列表:

    velocity = list([-10, -9.5, -9, -8.5, -8, -7.5, -7, -6.5, -6, -5.5, -5, -4.5, -4, -3.5, -3, -2.5, -2, -1.5, -1, 0.5, 1, 1.5, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10])

这是我的player1类中的def_init子例程:

     self.velocity_index = 0

这段代码也在player1类中,并且是错误发生的地方:

def do_jump(self):
    global velocity
    if self.jumping :
        self.y += velocity[self.velocity_index]
        self.velocity_index += 1
        if self.velocity_index >=len(velocity) - 1:
            self.velocity_index
        if self.y - self.rect.h == 362: #self.rect is the surface that the player should land on.
            self.pos=0
        elif self.y == 362-self.rect.h:
            self.jumping = False

0 个答案:

没有答案