I Built Tetris in Python from Scratch in 1 Hour

I Built Tetris in Python from Scratch (Pygame Beginner Tutorial)

You want to build a classic game like Tetris. You know Python basics but need a project to level up. I did just that. I coded Tetris using Pygame. It runs smoothly. You control pieces, score points, and restart after game over. Follow along. You’ll end up with working code.

Python stays popular. In 2025, it claimed 23.28% of the TIOBE index share. Game development with Python grows too. Mobile gaming surges in 2025 trends. Beginners pick Pygame for simple 2D games. Over 30,000 developers surveyed in 2025 use Python for fun projects like this.

Al Sweigart says, “Just like how Python comes with several modules like random, math, or time that provide additional functions for you to use, Pygame is a module that provides tons of functions that assist you in making games.” That fits here. You focus on logic, not low-level details.

Jesse Schell notes, “The player’s the boss; it’s your duty to entertain him or her.” Keep that in mind. Make your game fun.

Why Build Tetris in Python?

You learn loops, classes, and event handling. Tetris teaches collision detection and grid management. I built it to practice. You can too. It takes a few hours.

Benefits for Beginners

You see immediate results. Run the code, play the game. Fix bugs fast. Python’s simplicity helps. No complex setups.

Current Trends in Game Dev

In 2025, Python engines like Pygame thrive for indie devs. Cloud gaming rises, but start local. You build skills for bigger projects.

Setting Up Your Environment

Install Python first. Download from the official site. Then add Pygame.

Installing Python and Pygame

Go to python.org. Grab the latest version. Run the installer. Check “Add to PATH.”

Open your terminal. Type pip install pygame. Done. Test it. Import pygame in a script. No errors? Good.

For help, check the Pygame docs.

Defining Constants and Colors

Start simple. Set grid size. Use 10 wide, 20 high. Cells at 30 pixels.

Colors and Shapes

Pick colors for pieces. Cyan for I, yellow for O. Define shapes as lists. Each rotation variant.

I used dictionaries for TETROMINOES and colors.

# Tetromino shapes
TETROMINOES = {
    'I': [['.....',
           '..#..',
           '..#..',
           '..#..',
           '..#..'],
          ['.....',
           '.....',
           '####.',
           '.....',
           '.....']],
    
    'O': [['.....',
           '.....',
           '.##..',
           '.##..',
           '.....']],
    
    'T': [['.....',
           '.....',
           '.#...',
           '###..',
           '.....'],
          ['.....',
           '.....',
           '.#...',
           '.##..',
           '.#...'],
          ['.....',
           '.....',
           '.....',
           '###..',
           '.#...'],
          ['.....',
           '.....',
           '.#...',
           '##...',
           '.#...']],
    
    'S': [['.....',
           '.....',
           '.##..',
           '##...',
           '.....'],
          ['.....',
           '.#...',
           '.##..',
           '..#..',
           '.....']],
    
    'Z': [['.....',
           '.....',
           '##...',
           '.##..',
           '.....'],
          ['.....',
           '..#..',
           '.##..',
           '.#...',
           '.....']],
    
    'J': [['.....',
           '.#...',
           '.#...',
           '##...',
           '.....'],
          ['.....',
           '.....',
           '#....',
           '###..',
           '.....'],
          ['.....',
           '.##..',
           '.#...',
           '.#...',
           '.....'],
          ['.....',
           '.....',
           '###..',
           '..#..',
           '.....']],
    
    'L': [['.....',
           '..#..',
           '..#..',
           '.##..',
           '.....'],
          ['.....',
           '.....',
           '###..',
           '#....',
           '.....'],
          ['.....',
           '##...',
           '.#...',
           '.#...',
           '.....'],
          ['.....',
           '.....',
           '..#..',
           '###..',
           '.....']]
}

Creating the Tetromino Class

Pieces need position, rotation, and cells.

You can watch our YouTube Video for a detailed Tutorial

https://youtu.be/ChahWqn6bfc

Piece Movement

Class holds x, y, shape, and color. Get cells from the current rotation.

Rotation Logic

Cycle through rotations. Check if valid before applying.

Building the Tetris Game Logic

Core class handles grid, pieces, and score.

Initializing the Game

Create an empty grid. Black cells. New current and next piece. Set score to zero.

Handling Movement and Rotation

Move left, right, down. Rotate up key. Validate positions. No overlap or out of bounds.

Dropping and Placing Pieces

Drop every 500 ms. Adjust for level. Place when hits bottom. Clear full lines.

Scoring and Leveling Up

Score based on lines cleared. 40 for one, up to 1200 for four. Level up every 10 lines. Speed increases.

Drawing the Game

Use Pygame to render.

Grid and Cells

Draw lines for the grid. Fill cells with colors.

Pieces and Stats

Draw the current piece. Show the next piece aside. Display score, lines, and level.

External link: See Pygame drawing examples at https://www.pygame.org/docs/ref/draw.html

The Main Game Loop

Run at 60 FPS. Handle events. Update fall time. Drop piece.

User Input

Keys for move, rotate, and hard drop with space.

Game Over Handling

If a new piece can’t place, game over. Show message. R to restart, Q to quit.

Full Code and Running It

Download the Full code and run it.

Tweak as you like. Add sounds later. See our audio in the Pygame guide.

Link to adding sounds in Pygame

External link: For more projects, visit https://realpython.com/tutorials/games/

FAQ

How do I install Pygame for Tetris?

Run pip install pygame in your terminal after Python setup.

What version of Python works with Pygame?

Python 3.8 or higher. Check compatibility on pygame.org.

Can I build Tetris without Pygame?

Yes, use Tkinter or console. But Pygame handles graphics better.

How to fix common errors in Pygame Tetris code?

Check imports. Ensure no indentation issues. Run in IDE for traces.

Is Pygame good for beginner game devs?

Yes. It simplifies drawing and inputs.

How to add multiplayer to Python Tetris?

Use sockets. But start single player first.

Disclaimer

Sengideons.com does not host any files on its servers. All point to content hosted on third-party websites. Sengideons.com does not accept responsibility for content hosted on third-party websites and does not have any involvement in the same.

LEAVE A REPLY

Please enter your comment!
Please enter your name here