Categories :

Classes and objects in Python Part 1

After a week break caused by WWDC, we’re back to Python. We finished work on a program that calculates the square of a given integer, so it’s time to undertake something more ambitious — for example, a text game that can help you to understand key concepts of object-oriented programming. If you are interested in programming, you are probably faced with this term. What does it mean?

In the language which follows object-oriented paradigm, all entities are treated as objects with set of properties and methods functions that they provide. With objects closely related to the concept of class is a neutral prototype of the object, or rather structure, on the basis of which objects are created. Speaking in images, a class is a box which has several labels or without labels. When we create an object based on the class, we make these stickers unique signature, inheriting the form given class. While this might sound too abstract and unclear, but hopefully the examples will make it intelligible.

Image result for Classes and objects

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

Before you write something, we need to understand what we will do is to make a plan. Let’s start with the simplest game design. The game, which we will write, tells about the struggle of a simple man with a bureaucracy. The player’s task — to get to the end of the day a certificate under the name of LAM-40, the 40th floor of a huge state institutions. In honor of this record, I play and call.

How LAM-40? At the beginning of each cycle of the game (and any game is, in fact, infinite loop) randomly generated floor of the building with all its bureaucratic — endless, identical doors and Windows, unfriendly employees and the long, annoying queues. The player performs a sequence of actions, which spends a certain amount of time and either goes to the next floor, randomly generated or remain on the same floor, or rolled back to one of the previous floors, or even lose the game if he screwed up.

The nerve of the game is in the balance between risk and time limit. You can behave arrogantly and faster to move through the building, but it is not for all state employees — if you overdo it, somebody, for example, will call the police and help you get. On the other hand, if you act too cautiously you will not have time to get to the 40th floor because the hospital has a total of nine hours. Therefore, in order to achieve the goal, the player must assess the situation and make decisions based on it — somewhere to take risks, and somewhere to be cautious.

Now briefly describe the structure of the game — or rather, what it encountered recurring types of objects. The largest of them is the floor. Each floor has a set of properties that are randomly generated. Accordingly, we will need to create a class floor, which will create floor-objects, where the action will occur in the game. What objects are on each floor? This, for example, civil servants, whom I have already mentioned in the description of the game (very important! I call the civil servants only from the point of view of programming!). Let’s create a class civil servant on the basis of which will create all the civil servants in the game. Basically, they will act as the opponents of the player, but sometimes can become allies.