Skip to content
Sign upLog in
← Back to Community

Using python's curses module to reduce flickering in terminal games

Profile icon
CSharpIsGud

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!

Voters
Profile icon
LUCASHERIDAN
Profile icon
programmeruser
Profile icon
z80
Profile icon
cs906941
Profile icon
xxpertHacker
Profile icon
PyRoshan
Profile icon
DynamicSquid
Profile icon
RohilPatel
Profile icon
mkhoi
Profile icon
NoelB33
Comments
hotnewtop
Profile icon
xxpertHacker

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

Profile icon
RohilPatel

Respect

Profile icon
CodeLongAndPros

There is also curses.wrapper.

Profile icon
DynamicSquid

Whoa, now that's some smooth movement!

Profile icon
CSharpIsGud

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

Profile icon
Jakman

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

Profile icon
Navinor

Nice!