Learn to Code via Tutorials on Repl.it!

← Back to all posts
Using python's curses module to reduce flickering in terminal games
CSharpIsGud (880)

Im not a huge fan of text adventures and the like.
But you may have noticed that they suffer from terminal flicker if they include things like a minigame where the player can move around outside of being narrated by text.

So, I created this well commented example of python's curses module that can reduce or completely eliminate such flickering.

Why it helps

Normally to do something like draw a map of where the player is in an environment, you would clear the screen and redraw the map.
This is how most terminal games on replit do it.

But redrawing the map takes some time, and since the screen has been cleared you get a brief moment where the screen is completely clear until the map data is written onto it character by character

So what if instead of clearing the screen first then drawing the map, you do them both at the same time?

Thats where curses comes in.
Curses buffers the changes you make and only displays them after you call its refresh function, this way all the changes you made are displayed at the same time!

xxpertHacker (768)

Hmm... 150 lines? Too long and complicated for the average Repl Python user.

CodeLongAndPros (1578)

There is also curses.wrapper.

DynamicSquid (4602)

Whoa, now that's some smooth movement!

CSharpIsGud (880)

@DynamicSquid It still flickers a little, but you can make it even smoother by only updating tiles that actually changed since the last "frame"

Jakman (473)

This helps those that would enjoy this. You should make this into a class so that it is easy to incorporate at multiple times.