22

Yes, this question has been asked before. No, none of the answers I read could fix the problem I have.

I'm trying to create a little Bounce game. I've created the bricks like this:

def __init__(self,canvas):
    self.canvas = canvas
    self.brick1 = canvas.create_rectangle(0,0,50,20,fill=random_fill_colour(),outline=random_fill_colour())
    self.brick2 = canvas.create_rectangle(50,0,100,20,fill=random_fill_colour(),outline=random_fill_colour())
    self.brick3 = canvas.create_rectangle(100,0,150,20,fill=random_fill_colour(),outline=random_fill_colour())
    self.brick4 = canvas.create_rectangle(150,0,200,20,fill=random_fill_colour(),outline=random_fill_colour())
    self.brick5 = canvas.create_rectangle(200,0,250,20,fill=random_fill_colour(),outline=random_fill_colour())
    self.brick6 = canvas.create_rectangle(250,0,300,20,fill=random_fill_colour(),outline=random_fill_colour())
    self.brick7 = canvas.create_rectangle(300,0,350,20,fill=random_fill_colour(),outline=random_fill_colour())
    self.brick8 = canvas.create_rectangle(350,0,400,20,fill=random_fill_colour(),outline=random_fill_colour())
    self.brick9 = canvas.create_rectangle(400,0,450,20,fill=random_fill_colour(),outline=random_fill_colour())
    self.brick10 = canvas.create_rectangle(450,0,500,20,fill=random_fill_colour(),outline=random_fill_colour())
    self.bricksId = [self.brick1,self.brick2,self.brick3,self.brick4,self.brick5,self.brick6,self.brick7,self.brick8,self.brick9,self.brick10]

And I'm trying to reference the ID of bricksId[0] over here:

self.hit_brick(pos,self.bricks.bricksId[0])

Earlier, in the __init__, I define bricks as bricks, which is defined as Brick(canvas). However, the error states:

TypeError: 'Brick' object does not support indexing

In the answers to the other questions of this subject, I cannot find any that help me access bricks.bricksId[0].

8
  • 1
    And what is the full traceback? It appears that self.bricks.bricksId is no longer a list if the self.bricks.bricksId[0] throws an exception. Commented Mar 17, 2014 at 19:44
  • Traceback (most recent call last): File "C:\Users\Jonathan\Documents\Python\Bounce!.py", line 106, in <module> ball.draw() File "C:\Users\Jonathan\Documents\Python\Bounce!.py", line 47, in draw self.hit_brick(pos,self.bricks.bricksId[0]) File "C:\Users\Jonathan\Documents\Python\Bounce!.py", line 61, in hit_brick brick_pos = self.canvas.coords(self.bricks[brickId].id) TypeError: 'Brick' object does not support indexing Commented Mar 17, 2014 at 19:50
  • 1
    self.bricks[brickId] is not the same thing as self.bricks.bricksId[0]. Did you mean to use self.bricks.bricksId[brickId].id there perhaps? Commented Mar 17, 2014 at 19:52
  • How do you initialize self.bricks?
    – Silas Ray
    Commented Mar 17, 2014 at 19:52
  • @MartijnPieters I just fixed that, but I get a new error: Traceback (most recent call last): File "C:\Users\Jonathan\Documents\Python\Bounce!.py", line 106, in <module> ball.draw() File "C:\Users\Jonathan\Documents\Python\Bounce!.py", line 47, in draw self.hit_brick(pos,self.bricks.bricksId[0]) File "C:\Users\Jonathan\Documents\Python\Bounce!.py", line 61, in hit_brick brick_pos = self.canvas.coords(self.bricks.bricksId[brickId].id) AttributeError: 'int' object has no attribute 'id' Commented Mar 17, 2014 at 19:54

1 Answer 1

45

In order for the Brick object to be indexable, you must implement the methods:

  • __getitem__
  • __setitem__
  • __delitem__

You don't need all of them, only the ones you use.

However, this seems like a case of self.bricks being a brick instead of a list of bricks. A list of bricks is indexable; however, a brick itself is not unless you implement the methods above.

Check this for reference.


In order to be able to call self.bricks.bricksId[number] when I needed:

def __getitem__(self,index):
    return self.bricks.bricksId[index]

def __setitem__(self,index,value):
    self.bricks.bricksId[index] = value
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.