A minimal terminal Tetris

Things I had to puzzle over:

  • I’d never used Zig’s package system before. The key thing I needed was to do zig fetch --save git+https://github.com/xyaman/mibu.git and then GitHub - xyaman/mibu: Pure Zig library for low-level terminal manipulation.
  • Initially, I forgot that terminals have non-square characters. To print something on a square grid, each cell needs to be two characters wide (see stage.zig)
  • Also, writing the entire game to the terminal for every redrawn frame would be quite inefficient, so I wanted to do double buffering, a comparison then only send the changes (see display.zig). This could be done much more neatly and with less cursor movement
  • I forgot how strict Zig is about casting (compared to C) and the syntax for @intCast() might have changed since I remember last using it. E.g. const xo = px + @as(isize, @intCast(x)) in player.zig
  • AFAICT, Zig’s range .. operator only supports positive and increasing integers, so I ended up using while in some cases which I had to look up the syntax for

Everything else I spent time on was to do with how Tetris works. Probably the thing which took the longest was remembering how to rotate a bitmap in 2D. Initially I went with a “proper” trigonometry type solution, until I realised I could just write out the mapping Simplify rotation · ringtailsoftware/zigtris@408fd24 · GitHub

4 Likes