Skip to main content

A terminal-based Tetris clone with AI, hint mode, and a persistent leaderboard.

Project description

Pytris

 .----------------.  .----------------.  .----------------.  .----------------.  .----------------.  .----------------. 
| .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. |
| |   ______     | || |  ____  ____  | || |  _________   | || |  _______     | || |     _____    | || |    _______   | |
| |  |_   __ \   | || | |_  _||_  _| | || | |  _   _  |  | || | |_   __ \    | || |    |_   _|   | || |   /  ___  |  | |
| |    | |__) |  | || |   \ \  / /   | || | |_/ | | \_|  | || |   | |__) |   | || |      | |     | || |  |  (__ \_|  | |
| |    |  ___/   | || |    \ \/ /    | || |     | |      | || |   |  __ /    | || |      | |     | || |   '.___`-.   | |
| |   _| |_      | || |    _|  |_    | || |    _| |_     | || |  _| |  \ \_  | || |     _| |_    | || |  |`\____) |  | |
| |  |_____|     | || |   |______|   | || |   |_____|    | || | |____| |___| | || |    |_____|   | || |  |_______.'  | |
| |              | || |              | || |              | || |              | || |              | || |              | |
| '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' |
 '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------' 

A terminal-based Tetris clone written in Python. Features a persistent leaderboard, AI and hint modes, and a ghost piece. Inspired by Michael Fogleman's original QBasic implementation from 2000.


Requirements

  • Python 3.7 or higher
  • Windows only: windows-curses (see below)
  • All other dependencies are Python standard library (curses, json, os, datetime)

Installation

From PyPI

pip install Pytris-B1

From source

git clone https://github.com/basanta-bhandari/Pytris.git
cd Pytris
pip install .

Windows

windows-curses is listed as a conditional dependency and installs automatically via pip. If running from source manually:

pip install windows-curses
python main.py

Running

If installed via pip:

pytris

If running from source:

python main.py

On startup, the banner is printed and you are prompted for a username before the game launches:

 .----------------.  .----------------. ...

  >>> Enter Username: basanta

Pressing Enter without a name sets the username to anonymous.


Game Modes

Select a mode from the main menu using the number keys.

Key Mode Description
1 Normal Standard Tetris
2 Hint Ghost piece + AI suggested landing spot shown in cyan
3 Computer Fully automated — AI plays the game
4 Leaderboard View the top 10 scores

Controls

Key Action
Left / Right Move piece
Up Rotate (with wall kick)
Down Soft drop (+1 point per row)
Space Hard drop (+2 points per row)
P Pause / Resume
ESC Save score and return to menu
Q Save score and quit

Scoring

Action Points
1 line cleared 100 × level
2 lines cleared 300 × level
3 lines cleared 500 × level
4 lines cleared 800 × level
Soft drop 1 per row
Hard drop 2 per row

Level increases every 10 lines. Fall speed increases with level.


Leaderboard

Scores are saved to scores.json in the same directory as main.py. Each player keeps only their personal best. The top 10 are shown on the leaderboard screen.

scores.json format:

[
  {
    "user": "basanta",
    "score": 4200,
    "lines": 12,
    "level": 3,
    "date": "2025-02-27"
  }
]

On game over, your rank and whether you set a new personal best are shown before auto-returning to the menu after 5 seconds.


AI

The computer mode evaluates every valid (x, rotation) placement for the current piece using a weighted heuristic:

  • Completed lines: rewarded
  • Gaps below pieces: penalised
  • Aggregate stack height: penalised
  • Bumpiness: penalised

The highest-scoring placement is chosen each turn. Hint mode uses the same logic to overlay the suggested drop position without taking control.


File Structure

Pytris/
├── main.py          # entire game
├── scores.json      # created automatically on first game over
├── setup.py
├── requirements.txt
└── README.md

Basanta Bhandarigithub.com/basanta-bhandari

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytris_b1-1.0.0.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytris_b1-1.0.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file pytris_b1-1.0.0.tar.gz.

File metadata

  • Download URL: pytris_b1-1.0.0.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for pytris_b1-1.0.0.tar.gz
Algorithm Hash digest
SHA256 68347e2426ca8ee253765e23935da48e26465a8b8b905b533b6b196412f8bb7e
MD5 413a52b4269532ea978b252561822a58
BLAKE2b-256 ffeda59346933d66bff42319bc39d2718f4af075838af7d61ce14aec9773e42e

See more details on using hashes here.

File details

Details for the file pytris_b1-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pytris_b1-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for pytris_b1-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 05b31ea6994baa6a1554e4ccba3135f0aff23ee9e061b7b956ea4f48d6a356c0
MD5 a147855400481ee3d6d5665e4c6f5834
BLAKE2b-256 999ad1f95c9ef52c106dc233ac5d372694c3cd21bde7727c6094cc130f328c59

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page