console programming without scrolling

I am not really sure how to describe it, but here it goes... I started with python a couple of months ago. It is going pretty good. Several of my bash scripts have been converted over to python. I use python every other day at work. Sometimes, I want to challenge myself with new projects.

Back in high school, we had a computer club. The teacher wrote a fish-shark simulation (Wa-Tor). For some reason, I remembered it and I started to write it program. Here is my problem, the program gets my terminal window's rows and columns. My entire fish world is size of the terminal window. Every other move, I want the fish world to be re-drawn. My only problem is that 'print my_ocean' ends up just scrolling.

Example: Chronos 1: print Chronos 2: print

I just want to update the same screen over and over.

Is it possible to stay on your current screen and update it, instead of scrolling? The scrolling does not always look that pretty.

Does that make sense?

6 comments · 93% upvoted · other discussions

comments

MonkeyNin

You're looking for access where you can update parts of the screen, by using curses or even libtcod

StorageNinja [OP]

I started to dig into curses this morning. Let's see how that works out :)

infinull

You really want to use blessings which is an abstraction layer on top of curses.

Won't work on windows if that's a problem.

something like:

import blessings
term = blessings.Terminal()
with term.fullscreen():
    while True:
        term.move(0, 0) # move to top of screen
        print('fish simulation')
        os.sleep(1)

should work ok.

Ran4

Prepend each line with four spaces or use backticks (`like this` → like this) to show code on Reddit.

This is hard to answer without knowing more details. How is the game world drawn to the terminal? If it's just using regular prints, you can reset the terminal's output with os.system("clear") (assuming Linux or OS X, use os.system("cls") for windows). You obviously have to import the os module.

You might want to consider using curses. It's archaic, but very commonly used for TUI's in Linuxland. See here for the docs.

42thehammer42

If it's just one or two lines that need updating, you can simply do the following: http://stackoverflow.com/questions/4897359/output-to-the-same-line-overwriting-previous-output-python-2-5

infinull

the '\r' trick won't work because OP is doing a full screen rewrite (If I understand correctly)