I’m been making a Tetris game in pygame, and the line clear function is not working. Basically, when one line (or more) gets cleared, it clears all blocks on the grid, and all future blocks that get placed.
For new pieces after the line clear, once they reach a certain point on the grid they get sucked down to the bottom and removed. This point gets higher and higher after each new piece, until blocks are getting sucked down as soon as they spawn.
The function:
def line_clear_check():
global grid
global past_blocks
lines_filled = 0
for y in range(20):
squares_filled = 0
for x in range(10):
if grid[y][x] == 1:
squares_filled += 1
if squares_filled == 10:
lines_filled += 1
for block in past_blocks.copy():
if block.position[1] == y:
past_blocks.remove(block)
if block.position[1] < y:
block.position[1] += 1
Global variables in the function:
-
grid:
grid = [] for x in range(20): row = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] grid.append(row)
-
past_blocks (this is how it’s modfied, as it’s creation is just an empty list) :
for block in current_piece.block_list.copy(): past_blocks.append(block) current_piece.block_list.remove(block)
I thought that the problem just stemmed from the list being modified and iterated over, so I turned it the past_blocks list in the function into a copy, but this still gave me the same results.
Any help is greatly appreciated. This is my first post on stack overflow, so if any more information is needed please let me know.
Paulo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.