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

2024-04-25 05:22:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在通过pygame创建一个2D格斗游戏。目前,我正在使用速度列表实现跳转到player1。但是,player1会按我所希望的方式跳跃和降落对象,但当它降落回平台时,会出现错误消息: self.y+=速度[自速度指数],列表索引超出范围。你知道吗

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

    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\u 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

Tags: the代码rectself列表indexifdef