1

I'm attempting to iterate over a list of class objects/module objects to check collision and this error is thrown:

  File "C:/Users/travi/PycharmProjects/game/main.py", line 81, in <module>
if collision.collision1.is_collision(player.player1.vel, player.player1.x1, player.player1.x2, player.player1.y1, player.player1.y2, environmentVector[i].x1, environmentVector[i].x2, environmentVector[i].y1, environmentVector[i].y2):
TypeError: list indices must be integers or slices, not environment

heres the code (from main.py)

    for i in environmentVector:
        if collision.collision1.is_collision(player.player1.vel, player.player1.x1, player.player1.x2, player.player1.y1, player.player1.y2, environmentVector[i].x1, environmentVector[i].x2, environmentVector[i].y1, environmentVector[i].y2):
            print("collision")

environment.py:

import pygame

class environment():
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.x1 = x - 16
        self.x2 = x + 16
        self.y1 = y - 16
        self.y2 = y + 16

collision.py:

import pygame
import player
import enemy
import environment

class collision():
    def __init__(self):
        self.collision = False

    def is_collision(self, moveSpeed, x1, x2, y1, y2, X1, X2, Y1, Y2):
        if (((x2 + moveSpeed >= X1) and (x2 <= X2)) and (((y2 >= Y1) and (y2 <= Y2)) or ((y1 <= Y2) and (y1 >= Y1)))) or (((x1 - moveSpeed <= X2) and (x1 >= X1)) and (((y2 >= Y1) and (y2 <= Y2)) or ((y1 <= Y2) and (y1 >= Y1)))):
            return True
        else:
            return False
3
  • 2
    environmentVector[i].x1 -> i.x1 Commented Nov 26, 2018 at 20:27
  • 1
    Python loops are for-each loops. In every iteration, i is the environment object itself, not a counter. Commented Nov 26, 2018 at 20:30
  • That’s a lot of redundant (...) Commented Nov 26, 2018 at 20:31

1 Answer 1

3

Python for loops are "for-each" type loops, unlike the traditional for loops in C and some other languages. In every iteration, loop variable (i in your case) is the next element in the container. Assuming vec is an std::vector<int> type, this in C++:

for (size_t i = 0; i != vec.size(); ++i){
    std::cout << vec[i] << std::endl;
}

Is roughly the equivalent of this in Python (lst is assumed to be a list object):

for i in lst:
    print(i)

As you can see i is the element itself, not its index.

Having said that, this is how your for loop could be fixed:

for i in environmentVector:
    if collision.collision1.is_collision(player.player1.vel, player.player1.x1, player.player1.x2, player.player1.y1, player.player1.y2, i.x1, i.x2, i.y1, i.y2):
        print("collision")

Notice that I replaced environmentVector[i]s with just is.

If you absolutely need indexes, you may use enumerate:
(lst is again assumed to be list object):

for indx, obj in enumerate(lst):
    print(indx, obj)

Here, in every iteration, you'll get the index assigned to indx and the object itself assigned to obj

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.