Lesson Block · Detail

Tetris with Matrices and Rotations

This building block reveals the modeling of a game field as a matrix as well as the algorithmic processing of movement, rotation, and collision. Didactically relevant is the step-by-step connection of data structure, state logic, and visual implementation in a clearly traceable application context.

Time 120 minutes
Format Pygame
Language Python 3
Tasks 4
Preview Image: Tetris with Matrices and Rotations

Introduction (Original Extract)

Tetris is a good example to combine a matrix as data structure, rotation of shapes, and simple game logic in pygame. You move falling stones in a grid, place them on the ground, and remove complete rows.

Didactic Classification

Subjective Target Setting

In the center of this module is the modeling of a game field as a two-dimensional list. This introduces a central information concept: representing a discrete grid by a matrix structure. Based on this, stones are described as smaller matrices and their position is managed through coordinates. Learners link abstract data models to a concrete visual representation in the game. The rotation of forms requires a structured rearrangement of matrix entries and makes algorithmic transformations visible. The connection between model and implementation shows itself particularly in that every movement, collision, and row deletion directly results from the chosen data structure.

Competence development

Didactic added value in the lesson

Lesson flow

1

Game field matrix and keyboard control

At the beginning, the game field is secured as a matrix and supplemented with basic keyboard control. The task combines the representation of the grid with initial movement rules and introduces the logic for when a stone can fall again or must be newly generated. This creates the functional basis for all further game mechanisms.

  • Didactic focus: matrix model and state-based control
  • Typical challenge: consistent testing of boundary conditions during movements
2

Rotating Tetrominos as matrices

In the second step, various forms are introduced and processed as small matrices. The rotation requires a targeted rearrangement of the matrix entries and is linked with the same collision test as movement. This makes it clear that different game actions rely on common logical rules.

  • Didactic focus: matrix transformation and rule consistency
  • Typical challenge: valid rotation without exceeding the game field
3

Deleting rows, points, and speed

Subsequently, the game logic is extended to recognize and remove complete rows. Based on this, points, lines, and levels are introduced as additional states from which a changed fall speed can be derived. This step combines data processing with a comprehensible rule development in the course of the game.

  • Didactic focus: Evaluation of matrix states and progress logic
  • Typical challenge: Correct removal and addition of rows in the game field
4

Full game

The final task serves a consolidating consideration of a complete reference game. In the foreground is not the addition of new code, but the recognition of previously developed mechanisms in a cohesive overall application. This reflects the development of competence and secures it professionally.

  • Didactic focus: Integration and reflection of the overall context
  • Typical challenge: Individual components in the overall system to be safely assigned

Work assignment (excerpt)

  1. Model the game field as a matrix and add a controllable fall movement of the active stone.
  2. Extend the game with various Tetrominos and rotation based on matrix operations.
  3. Consequently check movements and rotations through uniform collision logic.
  4. Implement deleting complete rows, points, lines, and increasing speed.

Example (excerpt)

The following excerpt is didactically relevant because it makes the rotation as targeted reordering of a matrix visible, thus highlighting a central core of the module.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def drehe_matrix_rechts(form):
    hoehe = len(form)
    breite = len(form[0])
    neue_form = []
    for x in range(breite):
        neue_zeile = []
        for y in range(hoehe - 1, -1, -1):
            neue_zeile.append(form[y][x])
        neue_form.append(neue_zeile)
    return neue_form

Hints for classroom practice

This module combines matrix modeling, rotation, and game logic into a step-by-step and technically well-understandable implementation.

The clear task structure supports classroom organization and facilitates systematic result verification. Extension tasks enable proper differentiation in the further course.

Request a demo access and test the module in your own course context.