This is a simple Tetris game implemented in Swift for the terminal. The game uses raw mode to handle keyboard input and a timer to drop pieces automatically.
Started this project to learn a bit about CLI's. Definitely not as straight-forward as expected, since there is no super easy way to get user input without blocking the main thread (at least i couldn't think of one yet). So I ended up with a super convoluted workaround including a thing called raw mode, which I didn't even know existed.
All in all, a fun two day project to laern Swift and google stuff for many hours.
- Basic Tetris gameplay with piece rotation and movement.
- Random piece generation with a preview of the next pieces.
- Line clearing
- Game over detection.
- TODO: Implement score tracking some time
- Swift
- A computer that can run swift
-
Clone the repository:
git clone https://github.com/MarcelOlsen/cli-tetris-swift.git cd shitty-terminal-game
-
Compile the Swift file:
swiftc main.swift -o tetris
-
Run the game:
./tetris
swift main.swift
a
: Move piece leftd
: Move piece rights
: Move piece downw
: Rotate piece
The main components of the game are:
- Raw Mode Handling: Functions to enable and disable raw mode for terminal input.
- Piece Management: Struct to represent a Tetris piece and handle its rotation.
- Grid Rendering: Functions to render the game grid and the upcoming pieces.
- Game Logic: Functions to handle piece movement, collision detection, line clearing, and game over conditions.
- Input Handling: Asynchronous input handling to read user commands without blocking the main thread.
- Game Timer: A timer to drop pieces automatically at regular intervals.
This project is licensed under the MIT License. See the LICENSE file for details.
- Inspired by the classic Tetris game.
- Uses Swift's Foundation and Darwin libraries for terminal handling and input.