Now to actually see this on the screen we need to call the function from main().
# This should go inside the while loop right BELOW draw_window()# Near the end of the loopdraw_next_shape(next_piece,win)pygame.display.update()
Clearing the Rows
As we all know in Tetris when all of the positions in a row are filled that row is cleared and every row about it is shifted down. The function clear_rows() will be where we do this. Essentially what we will do is check if a row is full. If it is we will delete that row. This will shift each row down one but leave us with one less row then before. To compensate for this we will add one row to the top of our grid.
For a detailed explanation please watch the video starting at 10:00.
defclear_rows(grid,locked):# need to see if row is clear the shift every other row above down oneinc=0foriinrange(len(grid)-1,-1,-1):row=grid[i]if(0,0,0)notinrow:inc+=1# add positions to remove from lockedind=iforjinrange(len(row)):try:dellocked[(j,i)]except:continueifinc>0:forkeyinsorted(list(locked),key=lambdax:x)[::-1]:x,y=keyify<ind:newKey=(x,y+inc)locked[newKey]=locked.pop(key)
Now we need to call clear_rows() from within the main() function.
Inside the while loop it will go in the following position.
defmain():whilerun:...# IF PIECE HIT GROUNDifchange_piece:forposinshape_pos:p=(pos,pos)locked_positions[p]=current_piece.colorcurrent_piece=next_piecenext_piece=get_shape()change_piece=False# call four times to check for multiple clear rowsclear_rows(grid,locked_positions)# < ---------- GOES HERE