TetrisGame class

A complete Tetris game: board, pieces, scoring, levels and timing.

Listen to it (it is a ChangeNotifier) to rebuild UI, or subscribe to events for sound effects, haptics and animations.

final game = TetrisGame(lockDelay: const Duration(milliseconds: 500));
game.start();
Inheritance

Constructors

TetrisGame({int boardRows = 20, int boardCols = 10, int nextQueueSize = 5, int startLevel = 1, int linesPerLevel = 10, Duration lockDelay = const Duration(milliseconds: 500), int maxLockResets = 15, Duration gravityCurve(int level)?, int? seed, TetrisStatistics? statistics, ReplayRecorder? recorder, void onScoreChanged(int score)?, void onGameOver()?, void onLevelUp(int score)?, void onLinesCleared(int score)?})

Properties

boardCols → int
final
boardRows → int
final
events → Stream<TetrisEvent>
Stream of everything that happens in the game. See TetrisEvent.
no setter
gravityCurve → Duration Function(int level)?
Optional custom gravity: the delay between automatic drops at a given level. Defaults to LevelState.gravityMs.
final
gravityInterval → Duration
Current delay between automatic drops.
no setter
hashCode → int
The hash code for this object.
no setterinherited
hasListeners → bool
Whether any listeners are currently registered.
no setterinherited
lockDelay → Duration
How long a piece may rest on the stack before it locks.
final
maxLockResets → int
Maximum number of lock-delay resets before a grounded piece locks immediately. Prevents stalling forever by spinning a piece in place.
final
nextQueueSize → int
final
onGameOver ↔ void Function()?
Called once when the game ends.
getter/setter pair
onLevelUp ↔ void Function(int score)?
Called with the new level after a level up.
getter/setter pair
onLinesCleared ↔ void Function(int score)?
Called with the number of lines cleared (1–4).
getter/setter pair
onScoreChanged ↔ void Function(int score)?
Called after every lock with the current score.
getter/setter pair
playTime → Duration
Time spent playing the current game, excluding pauses.
no setter
recorder ↔ ReplayRecorder?
When set, every action (including gravity steps and lock-delay expiry) is recorded, and recording restarts on every start. Playing the result back with ReplayPlayer.play and its seed reproduces the game exactly.
getter/setter pair
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
seed → int
Seed of the piece randomizer for the current game. Starting a game with the same seed produces the same piece sequence.
no setter
startLevel → int
Level a new game starts at.
final
state → GameState
no setter
statistics → TetrisStatistics
Statistics accumulated across games. Pass the same instance to several games (or restore it with TetrisStatistics.loadFromJson) to keep lifetime stats.
final
useInternalClock ↔ bool
Whether gravity and lock delay are driven by the game's own timers.
getter/setter pair

Methods

addListener(VoidCallback listener) → void
Register a closure to be called when the object changes.
inherited
applyGravity() → void
Performs one gravity step: moves the piece down a row, or starts the lock delay if it is resting on the stack.
dispose() → void
Discards any resources used by the object.
override
hardDrop() → void
Drops the piece straight down and locks it, for 2 points per cell.
holdPiece() → void
Swaps the active piece with the held piece (once per piece).
loadFromJson(Map<String, dynamic> json) → void
Restores a game saved with toJson.
lockActivePiece() → void
Locks the active piece if it is resting on the stack or floor.
moveLeft() → void
Moves the active piece one column left, if possible.
moveRight() → void
Moves the active piece one column right, if possible.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notifyListeners() → void
Call all the registered listeners.
inherited
pause() → void
Pauses gravity, lock delay and the play clock.
removeListener(VoidCallback listener) → void
Remove a previously registered closure from the list of closures that are notified when the object changes.
inherited
restart() → void
Starts a new game with the same settings.
resume() → void
Resumes a paused game.
rotateCCW() → void
Rotates counter-clockwise using SRS wall kicks.
rotateCW() → void
Rotates clockwise using SRS wall kicks.
softDrop() → void
Moves the piece down one row for 1 point. If the piece is already resting on the stack it locks immediately.
start({int? seed}) → void
Starts a new game. Pass seed to get a reproducible piece sequence.
togglePause() → void
Toggles between playing and paused.
toJson() → Map<String, dynamic>
Serializes the current game state. Restore it with loadFromJson.
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited