Currently, I know only one way to show strings using ncurses library, like as below:
import curses
stdscr = curses.initscr()
stdscr.addch(0,0,'x')
stdscr.getch()
But I've met a problem when I want to make a falling function of string.
import curses
import time
stdscr = curses.initscr()
y=1
def fall():
global y
stdscr.addstr(y,0,'x')
stdscr.move(y-1,0)
stdscr.clrtoeol()
y += 1
stdscr.getch()
while True:
time.sleep(0.2)
fall()
If I remove this getch()
function, I can't see the ncurses screen. But if I put it in. I have to touch some key on my keyboard then the string could fall.
Is there a way I can make the string automatically falling without hit keyboard or mouse?
Refresh at the point you wants to reflect changes on your screen. I am not rectifying but modifying my draw square code in previous answer, below my own code using curses library(added comments so that it can be helpful for someone new):
Snap-sort of one iteration:
two iterations: http://s1.postimg.org/ehnvucp1p/rain.gif
You have to explicitly update the screen, either by calling the
refresh()
method on the window (stdscr
in your example) or by callingcurses.doupdate()
.This is due to the fact that
curses
was written years ago, when terminal where pretty slow and it was really important to make modifications efficiently. With an explicit update you can first change the screen how you want and then update it in a single operation, instead of doing an update for every single operation.