Lesson 09

Clearing lines and keeping score

The payoff loop. When a piece locks, we clear any full rows using the Board logic from Lesson 4, award points that reward multi-line clears, and redraw the settled board.

▸ By the end of this lesson: Full rows clear, everything above drops, and your score climbs — more for multi-clears

9.1Clear on lock

We already wrote board.clear_full_rows(). Now call it during locking and score the result. Update _lock_piece() in main.gd:

main.gd (replace _lock_piece)gdscript
var score: int = 0
var lines_cleared_total: int = 0

func _lock_piece() -> void:
	for cell in active_piece.get_cells():
		board.set_cell(cell.x, cell.y, active_piece.color_id)
	active_piece.queue_free()
	active_piece = null

	var cleared: int = board.clear_full_rows()
	if cleared > 0:
		_award_score(cleared)

	_render_board()
	spawn_piece()

9.2Scoring that rewards multi-line clears

Clearing four rows at once should be worth far more than four singles. A simple, satisfying table:

main.gdgdscript
# Points for clearing N rows with a single piece.
const LINE_SCORES: Array[int] = [0, 100, 300, 500, 800]

func _award_score(cleared: int) -> void:
	cleared = clampi(cleared, 0, 4)
	score += LINE_SCORES[cleared]
	lines_cleared_total += cleared
	print("Cleared %d | score %d | lines %d" % [cleared, score, lines_cleared_total])

Why non-linear

1→100, 2→300, 3→500, 4→800. The jump from 4×100 (=400) to 800 for a "tetris" gives skilled players a reason to build up and clear four at once. clampi keeps the index safe even if logic ever reports 5+.

9.3Verify the collapse visually

  1. Run with F5. Build a nearly-full bottom row, then drop a piece to complete it.
  2. The full row vanishes and everything above shifts down by one.
  3. Watch the Output panel — score and line counts update on each clear.
  4. Complete two rows at once and confirm you get 300, not 200.

Redraw after clearing

_render_board() runs after clear_full_rows(), so the screen always reflects post-collapse data. If you ever redraw before clearing, cleared rows briefly linger. Order matters.

Checkpoint — expected state