## Tetris in pure BASIC
I’ve been working on a Tetris implementation in Locomotive BASIC, as a Rosetta Code solution.
The program includes:
- All seven pieces (ITOSZJL), in seven different colours
- A 7-bag randomizer, ensuring every piece appears once per set of seven
- Next-piece preview, line clearing, and hard and soft drops
- Scoring based on lines cleared and the current level
- Increasing difficulty, with the level rising every ten lines
- Keyboard control using cursor keys, the numeric keypad, or letter keys, including clockwise and counter-clockwise rotation
A lot of attention went into performance, particularly avoiding unnecessary work in the game loop:
- Precomputed rendering: Each piece and rotation has prebuilt draw/erase strings, so movement doesn’t need to decode the shape or redraw the board.
- Fast collision detection: The four occupied cells of each rotation are decoded once. A flattened board, cached linear offsets, and padded boundaries keep collision checks simple and stop them as soon as a collision is found.
- Efficient line clearing: Row occupancy counts are maintained incrementally, and only rows touched by the locked piece need checking. Completed rows are removed by scrolling a BASIC window rather than redrawing the board.
I also experimented with overlap-aware rendering, but dropped it: the small potential gain wasn’t worth the extra startup work and complexity.
The result is a compact BASIC program with ~175 lines.
Source: [Tetris: Rosetta Code]( Tetris - Rosetta Code )
It can also be run with CPCBasicTS v0.5.8
Comments and suggestions are welcome!
