I built a terminal-based Speedrun Math game in Python using the curses library. The goal is simple: answer as many quick math problems as you can before time runs out. Every correct answer shrinks the time you get for the next one, so the pressure ramps the deeper you go.
Why curses?
A terminal game usually means typing a number and pressing Enter for every single answer. That kills the "speedrun" feel. The curses library gives you raw key input, so the player can type digits live, hit backspace to fix a mistake, and lock the answer with Enter only when ready.
How the game works
The core loop is just three things:
- Generate a problem.
- Read the typed answer without spamming Enter.
- Score it, then shrink the timer and raise the difficulty.
The model-ready approach
The game logic lives in a MathGame class, separate from the UI. That makes the rules testable on their own. I verified 50 generated problems by re-evaluating them with the operator module, and every answer came out correct.
Difficulty ramps
Levels 1-2 stick to addition of two small numbers. Levels 3-4 introduce subtraction and multiplication. Level 5 and up bring in harder multiplication, subtraction, and even modulo. The time allowed per problem also shrinks by 0.25 seconds with each correct answer, down to a 1.5 second floor.
Scoring
A correct answer is worth 10 points plus 2 per streak point. Streaks reset on a wrong answer. That rewards speed and consistency over just answering.
Reading input without Enter spam
The helper builds the answer digit by digit:
- On a digit key, append it to the buffer.
- On backspace, drop the last digit.
- On Enter, finalize the answer.
- On q or escape, quit.
The cursor stays hidden and the read is non-blocking, so the screen keeps updating.
What I learned
- Raw input handling changes how a game feels. The same math game with Enter per answer feels slow; with curses it feels like an actual speedrun.
- Time pressure is the whole game. Shrinking the timer adds tension without adding any extra code.
- Keeping logic separate from the UI makes testing trivial. I could unit test the problem generator without ever touching the terminal.
Run it yourself
Clone and play:
git clone https://github.com/BlurBisht/game-logic.git
cd game-logic
python speedrun_math.py
It is MIT licensed, free to use and modify. I am building this as Day 4 of a one-project-a-day challenge, alongside a Minecraft toolchain and other small projects. Follow along and build something every single day.
GitHub: https://github.com/BlurBisht
Portfolio: ms.blurbisht.fun
Top comments (0)