Close

18/10/2017

Writing a game in Python Part 1

In the new issue of blog about programming, I bring the game LAM-40 (we started writing it in the last issue) to an elementary prototype that already can be tested.

Last time we figured out how to prepare your computer to work with python, and now can move on to writing games. In creating any game is almost the most important thing is as soon as possible to begin to prototypically to identify the main vulnerabilities and address them. We, of course, the game is pretty simple, and most importantly for us, not quality, and exercises with code. However, early prototyping is a useful habit to develop anything, so let’s follow it.

Image result for Design document for the text game

For those who are forgotten in two weeks the essence of what we do, let me remind you. LAM-40 is a text game where you play as a person who needs to get over nine hours to get help on the 40th floor of a mysterious government agencies filled with bureaucrats. Each floor of the institution is generated randomly: the player hits the bureaucrats of different levels, each of which you need to look for a special approach to go further and get to the last floor.

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

First, let us remember what code have left in the last time, and figure out what we want to add to it.

We have a simple class bureaucrat who knows how to greet with a player and has two properties — rank and mood. The challenge for today is to create a simple, endlessly repetitive cycle of battles with bureaucrats, which in fact, is the main mechanic of our game. The resulting I in the end, the prototype took 95 lines — will start to disassemble it in order (the last empty string should be according to the standards of PEP 8, but embeds you chop).

LINE 2: in addition to random in the beginning of the program we import the sys module. From him, we need the function exit() — option to leave the game, that is not stated explicitly.

LINES 5-13, 16: I added a list of positive and negative reactions of the bureaucrats. Please note how to use the transfers. The fact that, according to PEP8, maximum line length should be 79 characters or code would be too difficult to read. Therefore, for convenience, each new utterance is on a new line and written in the same indent as the previous one. I added ACTIONS variable to the string data type with basic operations of the player (the usual question, bribing, pleading, pressure and threat), not to make a too long string where we display all of these actions.

LINE 25, 34-44: class Bureaucrat appeared characteristic of negative Boolean data (i.e. true or false) with the value False. It shows whether the player’s successful — if not, its value becomes True, and the game offers us to make a new action on the same bureaucrat, and does not create a new bureaucracy. In order to display the reaction of the bureaucrat to the action of the player, you use the functions with those names react_positively() and react_negatively () that change the value of the variable is negative and output a random reaction of the bureaucrat to the incident.

Image result for game coding for beginners

LINE 46-85: function act() displays all possible actions of the player and invites him to choose one of them by entering the first letter of the word. It is in order not to load this string, we have string with all the actions in a separate variable ACTIONS. Behind this is branching out and we for the first time in this blog, use the keyword elif is an abbreviation of else if. It specifies the condition in addition to what is specified in the if, and the block with him following the implementation of these conditions of action. In our case, we check that the entered player. If he introduced the lowercase letter Q, the program will end because of the previously mentioned functions sys.exit(). If the player enters the lowercase letter W (wait), he will drop a new bureaucrat is too far methadone maintenance opportunity. If the player enters any other character the program will go to the function react(), where branching to a new level. At this stage I did so that for certain combinations of rank and the mood of the bureaucrat is triggered only one action. Moreover, there are unbeatable combinations (e.g., high grade and in a bad mood), in the case that it is necessary to introduce W to fell the next bureaucrat.

Leave a Reply

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