Search code examples
pythontkintercoordinatescollision-detection

Unpack Coordinates of Variable


So after asking about collision detection before, Collision Detection in a List, I added in the

 find_overlapping 

function. The collision detection works fine but since I'm creating a brick breaker style game I have to change the direction variables depending on which side of the brick the ball hits. The brick that is hit is saved in the variable 'brick', but when I try to run the program an error comes up, "ValueError: need more than 0 values to unpack". I'm guessing this means it's trying to find the coordinates of 'brick' but since there is no brick variable yet it can't run? Can someone please help explain.

overlap = drawpad.find_overlapping(x1,y1,x2,y2)
length = len(overlap)
if length > 1: 
     listPlace = overlap[1] - 3
     brick = bricklist[listPlace]
     drawpad.delete(brick)
     bx1,by1,bx2,by2 = drawpad.coords(brick)
     if x1 <= bx1 or x2 >= bx2:
            angle = -angle
     if y1 <= by1 or by2 >= y2:
            direction = -direction

I also rewrote the code to below, however I think I only complicated it because the same error message appeared.

overlap = drawpad.find_overlapping(x1,y1,x2,y2)
length = len(overlap)
if length > 1: 
      listPlace = overlap[1] - 3
      brick = bricklist[listPlace]
      drawpad.delete(brick)
      for x in bricklist:
            if x == brick: 
                bx1,by1,bx2,by2 = drawpad.coords(x)
                if x1 <= bx1 or x2 >= bx2:
                    angle = -angle
                if y1 <= by1 or by2 >= y2:
                    direction = -direction

Solution

  • Get the coordinates before you remove the object:

    if length > 1: 
        listPlace = overlap[1] - 3
        brick = bricklist[listPlace]
        # Get the coordinates
        bx1,by1,bx2,by2 = drawpad.coords(brick)
        # Now delete it
        drawpad.delete(brick)
        if x1 <= bx1 or x2 >= bx2:
            angle = -angle
        if y1 <= by1 or by2 >= y2:
            direction = -direction