0

I'm working on a tetris clone and everything is practically done but I've encountered a bug that I have no idea how to fix. I have spent several days on it to no avail.

What happens is that after playing for a while a piece will land and fill up all the squares above it all the way to the top. You can see what I mean in this picture:

enter image description here

I have narrowed down the issue to these lines:

for row in range(4):
  for col in range(4):
    if array[row][col] != 0:
      setDead(row+self.block.pos[0], col+self.block.pos[1], 
      array[row][col])

setDead(row,col,val) sets array[row][col] = val in a 2-d array I have.

Before these lines everything is normal, but after these lines the extra squares are added. What these lines are supposed to be doing is copying from the block array to the board.

I would be very appreciative of some help as I have said, I have spent several hours on this.

def setDead(row, col, val):
  global dead_blocks
  dead_blocks[row][col] = val

dead_blocks is the 2-d array where I store all the dead blocks

Here is some console output, these are print statements before dead_blocks[row][col] = val and after. It looks like on the first assignment it is setting all the blocks above it in the column to the same value.

Console Ouput

As you can see only dead_blocks[8][3] should be set to 2 but instead the squares from dead_blocks[8][3] to dead_blocks[0][3] are all being set to 2.

T-Fox
  • 1
  • 3
  • I don't think the nested loop you've shown us can be the cause of your problem, because it calls `setDead` _at most_ 16 times, and your photo appears to have far more than 16 unwanted purple "pixels" (although we don't know what it _should_ have looked like...). If you're _sure_ the problem appears when this nested loop executes, then one likely culprit is `setDead`... Too many "pixels" are appearing, so either `setDead` is setting more than one per call, or this whole nested loop is getting executed more than once. – Kevin J. Chase Jan 23 '15 at 04:34
  • This loop seems to be converting the (row, col) offsets of a sprite's "pixel" to the game display's (row, col) scheme... but it doesn't validate the calculated row or column. How would `setDead` handle being handed co-ordinates that are "off the board"? Also, if converting from sprite-location to game-location and back happens a lot, you might want to hide that math inside methods like `spriteOffsetOf(gameRow, gameCol)` to ensure the conversions are done the same every time. You could then add all the error-checking and logging you like to those methods. They'd be easier to test, too. – Kevin J. Chase Jan 23 '15 at 05:06
  • Could you post your entire `setDead` method? – sloth Jan 23 '15 at 07:42
  • I added the entire setDead method to my post. – T-Fox Jan 23 '15 at 15:08
  • The reason I know these lines are the problem is because when I print out the array before these lines everything is normal but after these lines the extra squares are added. I really don't understand how that is even possible. – T-Fox Jan 23 '15 at 15:17

0 Answers0