In the new issue of blog about programming, I’m getting a prototype created in the previous release.
Last time we brought a text game lam-40 to the first working prototype, which, however, still far from the final version. At the end of the article, I outlined the main disadvantages of this prototype, and today we will deal with some of them. First, all code is now contained in a single file. This disorder: replicas bureaucrats are best stored elsewhere, as the main logic of the game. Second, the current prototype looks bad: to navigate in the text, which displays the game uncomfortable. Today we are not going to radically change the functionality, but simply bring to mind what we did in previous times.
NOTE: all code is provided for Python versions 3.0 and older, and therefore may not work on older versions.
First, let’s look what the code was left from last time.
I have focused on restructuring the code and the look of the game: first in any case cannot be postponed when the code will be more, and for a second it is better to start looking as early as possible. In the end, instead of a single file became three files. In one of them, text.py now the text information is stored: in particular, replica bureaucrats. To disassemble there is nothing special: it’s just a set of lists, which has not changed since last time, but if you want to see it then here it is. And we’ll move on to another new file game.py where the Game class contains the main logic of the game.
ROW 1: in the beginning of the file I added a comment between the triple quotes, also known as the docstring. This is a very useful practice, which you should always follow: it not only easier to read code other people, but also helps you to test the program using doctest (that’s what we do in the last stages of development, although many suggest to write tests from the beginning). In addition, when you use the function help () outputs the description of how a particular object language, it takes the description from docstring.
ROWS 3-9: new modules! First, the OS module, which provides access to operating system features. We’ll use writing a function for cleaning the screen. Second, with the from — import we import to the current file one class from another module or file. In this case the Bureaucrat class from a file bureaucrat.py and the class of the Terminal module of the blessings designed for text formatting. We create a sample class Terminal and assign it to a variable with a short name t it will be convenient to insert into your code.
LINES 12-15: the initialization of a class is the basic logic of the game, which in previous versions was at the bottom of the file bureaucrat.py. It added a function clear_screen(), cleaning the screen (do not forget about the keyword self-inside a class). The function is defined on lines 25-29. The function looks at what operating system the user is. If Windows is identified as ‘nt’, the system-specific command ‘cls’, which becomes an attribute of the os.system(). The else branch and the command ‘clear’ is designed for Mac and Linux.
LINE 16: we see in the beginning of the game its name and version. To the name was highlighted, I use the function underline(), which is included in the Terminal module class blessings. Its attribute becomes a string, inside of which is a newline character \n, adopted in many programming languages. He lets the interpreter know that the following \n characters should be output on a new line.
LINES 31-38: function act() and react() moved from bureaucrat.py in game.py because do not relate directly to the actions of a bureaucrat. Inside it I put all currently available actions, including ignored in the previous version of the wait and exit.