Configuration & Customization

Tetris-SQL is highly configurable. Because the game engine is entirely defined by SQL logic, you can easily tweak the game mechanics, scoring system, and framerate by modifying the constant values at the top of the SQL file. Additionally, the Python input listener provides CLI arguments for custom database connections.

1. Input Listener Options (input.py)

The Python script uses the standard argparse library to accept database connection variables. This is crucial if you are running the game against a remote database, a non-standard port, or with specific user credentials.

Argument Short Description Default
--host -H Database host (IP or hostname) localhost
--port -P Database port 5432
--database -d Database name postgres
--user -u Database user role postgres
--password -p Database password postgres

Example: Connecting to a custom local instance:

python3 input.py -H 127.0.0.1 -P 5433 -d my_games_db -u nuno -p supersecret

(Note: While you technically can connect input.py to a remote database like AWS RDS, the network latency of the UPDATE statements will make the game feel incredibly unresponsive. Localhost is highly recommended.)

2. Game Engine Constants (game.sql)

To change how the game plays, open game.sql in any text editor. At the very beginning of the WITH RECURSIVE main AS (...) query, you will find a CTE named const.

This CTE acts as the configuration dictionary for the entire game.

WITH const AS (
    SELECT
        -- board width (Standard Tetris is 10)
        10 AS width,
        -- board height (Standard Tetris is 20)
        20 AS height,
        -- frames per second the game loop runs at
        60 AS fps,
        -- initial interval at which a piece drops one line (seconds)
        48/60.0 AS init_drop_delta,
        -- minimum interval between piece drops (seconds) - max speed
        6/60.0 AS min_drop_delta,
        -- amount to decrease the drop interval per each level (seconds)
        2/60.0 AS drop_delta_decrease,
        -- number of lines to clear to increase one level
        10 AS lines_per_level,
        -- weight given to the current level in the earned points
        1 AS level_score_multiplier
)

Tweaking the Engine: Common Scenarios

By altering these values, you can fundamentally change the gameplay experience.

Scenario A: Ultra-Fast Mode (Pro Player)

If the early levels feel too slow, you can increase the framerate and start the game at a much higher gravity.

  • Change fps to 120 (Note: requires a very fast terminal emulator to avoid flickering).
  • Change init_drop_delta to 15/60.0 (Pieces drop 3x faster initially).
  • Change min_drop_delta to 2/60.0 (Maximum speed is nearly instantaneous).

Scenario B: Custom Board Dimensions

The logic for wall kicks, rotation, and line clearing is dynamically tied to the width and height variables. You can easily create a massive or tiny board.

  • Change width to 20.
  • Change height to 30.
  • Trade-off Warning: A larger board means a much larger 1D array string to process and render. If you make the width too large (e.g., 50), the RAISE NOTICE output will likely wrap in your terminal window, completely destroying the visual layout of the game. You will need to shrink your terminal font size to compensate.

Scenario C: Classic Static Scoring

By default, the points awarded for clearing lines multiply based on your current level (clearing a Tetris on Level 5 is worth much more than on Level 1). If you want flat, arcade-style static scoring regardless of level:

  • Change level_score_multiplier to 0.

3. Scoring System (points_per_line)

Right below the const CTE is the points_per_line CTE. This defines the base points awarded for simultaneous line clears.

points_per_line(lines, points) AS (
    SELECT *
    FROM (
        VALUES
            (0, 0),
            (1, 100),   -- Single
            (2, 300),   -- Double
            (3, 500),   -- Triple
            (4, 800)    -- Tetris
    ) _
)

You can easily modify these values to heavily reward "Tetris" (4-line) clears, or penalize single-line clears to encourage risky play.