Close

22/08/2017

A text game in Python

In the new issue of blog about programming, I bring a text game LAM-40 to alpha version and is ready to start public beta testing.

My imitation of the development cycle reached its climax a week ago. I’ve hit the deadline and had no time to prepare a version of lam-40, which was supposed to accompany the new release, why the blog took a week break. During this time I managed to bring the game to alpha version: now it is theoretically possible to go from beginning to end. I have, however, never managed to get to 40th floor: time was running out somewhere on the 21st. While the game is too complex and needs a complete rebalance, but, fortunately, from the alpha version much Shine and is not required. And most importantly, I am able to follow the plan specified in the design document from the previous release.

Image result for game in Python

NOTE: all code is provided for Python versions 3.0 and older, and therefore may not work on older versions.

Most of the changes, as always, in the file game.py. To be honest, the alpha version of it again has turned into a real mess: now in this abundance which interface with each other features is easy to get confused. To beta I hope to fix it, spreading the functions on different files and thus making the code more readable.

LINES 17-40: the next permutation in the basic logic of the game. At this time, in addition to a welcome screen describing the game and a disclaimer was added the concept of “floor”. “Floor” is an obstacle from 1-3 bureaucrats, a kind of “level” and the entire game is already familiar to us a for-in loop 39 floors of iterations that need to go to get to the 40th floor and see the ending. Bureaucrats_number is stored in a variable the number of bureaucrats who need to win at this level. Of these battles formed another for-in loop. The cycle and random function.choice(), it is possible to present only an iterative element — for example, the list. Because of this, I give them the range() function on 1-3, i.e., it lists (0), (0, 1) and (0, 1 ,2)), not the integers 1, 2 and 3.

Please note time module is added to the file and the function sleep () that I use. It hinders the execution of the interpreter, all operations in the code. In our case, it allows you to display all the text gradually, and not update the entire screen — now to read what happens in the game, much easier. The argument, which is fed to the time function.sleep () is the number of seconds that the program gets paused. It can be a whole number (integer) and decimal (float). I also added in the main logic of the game random encounters with other visitors Institution, I have mentioned in the design document. We will focus on them next time: while they refer to a placeholder in the file text.py. If the player completes all 39 cycles and the time expires, the function is executed game_completed() — more on that below.

LINES 56-66: I removed the creation of a new model of the Bureaucrat class from basic logic and moved it in functions associated with the actions of the player. Likely a bug of the game at this stage that I do not always get to play, is that the timer sometimes goes over the mark of 20:00 and not ending the game. Perhaps this is due to the fact that the function evaluate_time(), is responsible for the offensive game_over () not called anywhere you need it. I thought I already fixed this bug, but just in case will continue to look out.

LINE 143-158: function react_negatively() and react_positively() moved to the main file of the game — hope that’s temporary, but while it was nowhere. Generally, when the code file exceeds 200 lines, reading it becomes quite difficult — especially in comparison with the other files, which are quite short.

LINES 192-201: the function to be called if a player was LAM-40. The screen is cleared improvised function clear_screen(), and then comes a fork: if the player passed LAM-40 less than 6 in-game hours, he will see the secret ending, if it took more time then usual. Finally, the program stops and finally displays the congratulations message.

In the file bureaucrat.py much more modest. Apart from the already mentioned functions of time.sleep(), I added this class visualization bureaucrats using symbols-stars indicating rank, and emoticons denoting mood. There is nothing complicated: for a neat formatting of the text put in the right places and spaces line breaks, and interlock string variables with the +operator.

In the file character.py one major change: I did a breakdown of the action variable actions by means of function split(). In the previous version, I have assigned a variable a list in which there were very many commas and single quotes. Function split() splits a single string into a list whose elements are groups of characters between spaces. Goes essentially the same thing, but to edit such a list of action much easier — and I could use that while working on the beta version.

Here are some screenshots from the alpha version. Yes, in front of a lot of work, but it’s something.

Image result for game in Python

For the next release I want to get a lot of things. Still, I will give the first game of testing others, and therefore have a lot to put in order: both externally and internally. Interface it’s time to improve the long-awaited selection menu and the game itself would be good to add a sense of progress on it by changing the available actions and continuous display of the current floor. Code want an end to the restructuring, it is more meaningful comments. Finally, I want to add the ability to save the best score of the player or the room floor, which he managed to get, or the time in which he was playing. In order to do this for a week, you will need a development of classic rush but I’ll manage!

If you do not understand something, write comments under the material and social networks — will be glad and any other feedback. If you are more experienced in programming people than I, with pleasure will listen to substantive criticism. Thank you and until next time!

Leave a Reply

Your email address will not be published. Required fields are marked *