为什么我在pygame游戏中收到IndexError: list index out of range
错误消息?
我正在做一个简单的清酒游戏,但是每次蛇吃一个苹果时我增加蛇的长度时,都会出错。
这是碰撞函数
def collision(x1, y1, x2, y2, bsize):
if x2 + bsize >= x1 >= x2:
if y2 + bsize >= y1 >= y2:
return True
return False
这是吃掉的苹果,如果与自身功能发生冲突就会死掉。
def eat(self): # if it eats the apple
for i in range(self.length):
if collision(apple.x, apple.y, snake.x[i], snake.y[i], apple.width): # entering the variables/coordinates
self.length += 1
apple.x_multiplier = random.randrange(row)
apple.y_multiplier = random.randrange(row)
apple.x = (r_distance * apple.x_multiplier) + 1 # spawning the apple
apple.y = (r_distance * apple.y_multiplier) + 1 # at a new coordinate
def collide(self): # if colide with itself
for i in range(2, self.length):
if collision(snake.x[0], snake.y[0], snake.x[i], snake.y[i], self.width): # enetring the variables/coordinates
self.alive = False # killing the snake