1

I'm creating a memory game in Python, where a grid of words is displayed for 20 seconds before being replaced by another grid, in which one of the words has been changed. Currently, this grid is printed and then another grid is printed below.

grid = [nineWordsRandom [j:j+3] for j in range(0, len(nineWordsRandom), 3)]

         for x,y,z in grid:

             print(x,y,z)

How could I remove this grid after 20 seconds and replace it with another grid? Thanks.

ben862
  • 11
  • 2
  • 5
    You should use [`curses`](https://docs.python.org/3.5/howto/curses.html) instead of simply printing to terminal. – Bakuriu Mar 25 '16 at 14:24
  • [Over here](http://stackoverflow.com/a/21923021/660921) I wrote an answer which explains the basics of how to do this sort of stuff; the basic idea is that you move the cursor around with special escape sequences and then print text, print spaces to "clear" words, etc. I couldn't agree more that you want to use `curses` though. It'll take you a bit of time to learn, but it'll save a massive amount of time in the long run. – Martin Tournoij Mar 25 '16 at 14:30

1 Answers1

0

Here You have a simple program using the curses module, that pretty much do what You are looking for:

import curses
import time

stdscr = curses.initscr()

stdscr.addstr("line 1\n")
stdscr.addstr("line 2\n")
stdscr.refresh()
time.sleep(3)

stdscr.erase()
stdscr.addstr("edited line 1\n")
stdscr.addstr("edited line 2\n")
stdscr.refresh()
time.sleep(3)

curses.endwin()
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
Tony Babarino
  • 3,355
  • 4
  • 32
  • 44
  • I tried implementing this but get this error: '_curses.error: setupterm: could not find terminal'. Do you have any idea about what's going wrong? – ben862 Mar 25 '16 at 19:17
  • @ben862 Are You running that code in some IDE, because that could be the reason? Try to do what's suggested here: http://stackoverflow.com/questions/9485699/setupterm-could-not-find-terminal-in-python-program-using-curses. It should work in the terminal though. – Tony Babarino Mar 25 '16 at 19:33