Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python curses tty screen blink

Tags:

python

tty

curses

I'm writing a python curses game (https://github.com/pankshok/xoinvader). I found a problem: in terminal emulator it works fine, but in tty screen blinks. I tried to use curses.flash(), but it got even worse.

for example, screen field:

self.screen = curses.newwin(80, 24, 0, 0)

Main loop:

def loop(self):
    while True:
        self.events()
        self.update()
        self.render()

render: (https://github.com/pankshok/xoinvader/blob/master/xoi.py#L175)

self.screen.clear()
#draw some characters    
self.screen.refresh()
time.sleep(0.03)

Constant time in sleep function is temporary, until I write 60 render calls controller.

How to implement render method correctly?

Thanks in advance, Paul.

like image 219
MoSt Avatar asked Jul 25 '14 21:07

MoSt


People also ask

How do you clear the screen curse in Python?

To clear characters until the end of the line, use clrtoeol(), To clear characters until the end of the window, use clrtobot().

Are curses still used?

It isn't used very often, because its functionality is quite limited; the only editing keys available are the backspace key and the Enter key, which terminates the string. It can optionally be limited to a fixed number of characters. See the library documentation on curses.

How do you install a curse?

How do I install a curse module in Python? To install the regular curses extension, you only have to uncomment the corresponding line of Modules/Setup in your Python source tree, and recompile. You may have to tweak the required libraries, as described in the comment: # Lance's curses module.


1 Answers

Don't call clear to clear the screen, use erase instead. Using clear sets a flag so that when you call refresh the first thing it does is clear the screen of the terminal. This is what is causing the terminal's screen to appear to blink. The user sees the old screen, then a completely blank screen, then your new screen. If you use erase then it will instead modify the old screen to look like the new one.

You may still see some odd flashing or other artifacts on slow terminals. Try calling screen.idcok(False) and screen.idlok(False) to stop curses from using insert and deletion operations to update the screen.

like image 144
Ross Ridge Avatar answered Oct 23 '22 23:10

Ross Ridge