Can I show strings on ncurses screen without getch () function?

Currently, I know only one way to display strings using the ncurses library, for example, as shown below:

import curses

stdscr = curses.initscr()
stdscr.addch(0,0,'x')
stdscr.getch()

But I ran into a problem when I want to make a falling row function .

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 function getch(), I do not see the ncurses screen . But if I put it in. I need to touch some keys on the keyboard, then the line may fall.

Is there a way to make a string fall automatically without pressing a keyboard or mouse?

+1
source share
2 answers

You must explicitly refresh the screen by calling a method refresh()in a window ( stdscrin your example) or by calling curses.doupdate().

, curses , , . , , , , .

+1

, . , draw , , curses ( , - ):

from curses import *
import random, time 
def main(stdscr):
  start_color() # call after initscr(), to use color, not needed with wrapper 
  stdscr.clear() # clear above line. 
  stdscr.addstr(1, 3, "Fig: RAINING", A_UNDERLINE|A_BOLD)
  # init some color pairs:    
  init_pair(10, COLOR_WHITE, COLOR_WHITE) # BG color
  init_pair(1, COLOR_RED, COLOR_WHITE)
  init_pair(2, COLOR_BLUE, COLOR_WHITE)
  init_pair(3, COLOR_YELLOW, COLOR_WHITE)
  init_pair(4, COLOR_MAGENTA, COLOR_WHITE)
  init_pair(5, COLOR_CYAN, COLOR_WHITE)
  # First draw a white square as 'background'
  bg  = ' '  # background is blank 
  for x in range(3, 3 + 75): # horizontal c: x-axis
    for y in range(4, 4 + 20): # vertical r: y-axis
      stdscr.addstr(y, x, bg, color_pair(10))
  stdscr.refresh()  # refresh screen to reflect 
  stdscr.addstr(28, 0, 'Press Key to exit: ')
  # Raining   
  drop = '#' # drop is # 
  while True: # runs infinitely 
    xl = random.sample(range(3, 3+75), 25) # generate 25 random x-positions
    for y in range(5, 4 + 20): # vertical 
      for x in xl:
        stdscr.addstr(y-1, x, bg, color_pair(10)) #clear drops @previous row
        stdscr.addstr(y, x, drop, color_pair(random.randint(1, 5)))
      stdscr.refresh() # refresh each time,  # ^^ add drops at next row
      time.sleep(0.5)  #sleep for moving.. 
    for x in xl: # clear last row, make blank  
      stdscr.addstr(23, x, ' ', color_pair(10))
  stdscr.getkey() # it doesn't work in this code
wrapper(main) #Initialize curses and call another callable object, func,

Snap-sort :

rain

: http://s1.postimg.org/ehnvucp1p/rain.gif

+2

All Articles