Screen system by console (python)

Asked

Viewed 350 times

-1

I am developing a text-based RPG (I am still very beginner and I am applying my learnings in this little game), and I would like to know if there is any way to create 'displays' within the console itself, as well as in pygame, if the player is in the main menu and wants to go to the options screen, he writes the command 'options', the screen (console) completely clears and shows only the options screen, and so will be for all functions, another example, the player wants to access the inventory during the game, then he writes 'inventory', the console is completely empty and then shows the inventory. The way my code is, things happen successively, as always on the console, follow my code.

EXAMPLE:

def main_menu():
    print('Jogar')
    escolha = str(input(''))
    if escolha in 'jogar':
        jogo()


def jogo():
    print('Você está no jogo')

main_menu()

The Program starts in the maine_menu(), when typing 'Play', the program forwards the player to the game (game()). However, when the game() is called, the above is still visible (main_menu()), i would like when the user typed the word 'Play', the screen was cleared and only the message appeared "You are in the game"

  • 1

    Rodrigo, for what is describing the cited answer will completely meet your need. If it didn’t work, maybe it did it wrong; or its need is different from what you described in the question. Whatever it is, we lack information to proceed, so I recommend that, if you consider that the cited answer does not meet you, edit the question and elaborate a [mcve] demonstrating the problem.

  • 1

    Rodrigo, I don’t think you read the link about [mcve], so I ask you to read (or reread). A complete (and huge) code does not help us to reproduce the problem, especially when it is not complete because it is too big. The term "Minimum" refers precisely to the fact that you must reduce the scope of the problem to the point where the error is, eliminating all that is noise and unrelated to the problem.

  • 1

    Taking advantage, I recommend you read the Stack Overflow Survival Guide in English.

  • If you really want to do this application on the terminal, it is worth taking a look at the standard library library library library library library library library library library ncurses - https://docs.python.org/3/library/curses.html - for windows: https://pypi.org/project/windows-curses/

  • This issue has space to be expanded with interface suggestions, and libraries to be used on the console and this will much in addition to "delete screen".

  • uses QT to create windows

Show 1 more comment

1 answer

3

Zérima thing - you have to evaluate if you really want to make a larger application with console, only with input and print, or want to create something with a more familiar interface for users - whether with Tkinter (or Qt, GTK, Kivy), Pygame or Web. But as there is a whole category of "vintage" games that are the "Word Reader" - this decision can make a lot more sense to what you want, than if it were an "agenda" app or anything else that people usually codate to learn.

First thing - at the limit of simplicity, the entire screen that the player will enter will simply print out all the new content - in this case, you just need, at the beginning of each function that represents a "commode", put the code to erase the screen.

To delete the screen, the simplest is to print a number of blank lines equal to or greater than the number of lines in the terminal -then just print a " n"- the 'new line' character multiplied by a value greater than the number of lines in the terminal. A value of 130 will even account for a terminal on a 4K screen with a small font, then print("\n" * 130) .

The answer found in several places to call os.system("cls") or os.system("clear") (depending on the system ) is generally bad, because the cost of an external process just to erase the screen gives a jump in the heart of anyone looking at your code. (In practice, in a single-user application on the console, the performance of this makes no difference, however).

Second thing - if you want to do something sophisticated in the thermial, besides erasing the screen, it’s nice to be able to print colored text, position the cursor, use information in windows, etc... The default solution for this more straightforward, without using third-party libraries, is to use ANSI Codes - special print sequences, starting with the special character "ESC" (code 0x1B) that can change the color, position the cursor, etc... https://en.wikipedia.org/wiki/ANSI_escape_code

The problem is that it doesn’t work in windows directly without a fixed configuration in Registry - the solution is to use the third library "Core" https://pypi.org/project/colorama/

Third: If you want more sophisticated things - like having little windows (imagine all the screens have a corner, a frame where you put the character’s tats and the invention, for example), you can use the "curses" library that comes with Python. https://docs.python.org/3/library/curses.html

Again, because of the attention falat that microsoft gave to the terminal from 1985 until 2019, windows requires additional settings to use curses: https://pypi.org/project/windows-curses/ (is promised a level terminal that developers need in some Windows 10 update for this year)

Fourth: And finally, if you want really interesting effects, of things being able to move on the terminal screen, and geometric shapes as is possible in pygame, there is the "finished" project (my own) - the Windows support and preliminary, And it’s very slow and it still doesn’t work colors - but on the Linux terminal you can get some very interesting effects. For now it is necessary to install from the master branch, and explore the example programs that come along: https://github.com/jsbueno/terminedia (to install from the master using Pip, just type: pip install git+https://github.com/jsbueno/terminedia.git)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.