Wanting to go deeper into Object-Oriented Programming with Python, today I want to share the code of a simple RPG game and its analysis for better understanding. If you are new to the topic, I leave you a couple of links that may be helpful:
1 – Object-Oriented Programming in Python
1 – Inheritance and Polymorphism in Python
The code is not my own; I did the Spanish translation of the system and its corresponding analysis. In the law and at the end of the article, I locate the source.
Game Analysis
The code starts importing the random library (it is the only one that we need to import), this library allows us to work with arbitrary data. If you want to know more about this library, you can see an article that published some time ago: Module random in Python with examples
Then define the class Character () that will later be inherited by the class Player () and Enemy ().
In the character class () the constructor __init__ is defined, and some parameters are initialized (name, health, etc.), a method is also defined to do_danium () that will give us a random value of the damage caused in the attack.
Then the Enemy class is defined ()that as mentioned above, inherits the class Character (). This class contains only the constructor __init__ that initializes the name of the enemy (an Orc) and health, which is going to be between 1 and the health of the Player.
Then comes the class Player (), this class contains a lot of methods, I will explain to them one by one:
Method __init_ _: this is the constructor of the course, initializes the state, health, and maximum health of the player.
Method to exit: when in the game we type “exit” this method is called, it displays a message and puts in 0 the health of the player, therefore leaves the loop and closes the program.
Help method: when in the game we type “help” this method is called and shows on the screen the keys of the “commands” dictionary.
Method state: when in the game we type “state” this method is called and returns the health of the player.
Fatigue method: when this method is called, a “feel tired” message is displayed, and the player’s health property will decrease by 1 point. Rest method: when this method is called a condition arises: if the player’s status is different from “normal” the character will not be able to rest (it is supposed to be in “fight”), if not, the style is put to rest. If the character is put to rest two things can happen: that nature wakes up suddenly because they want to attack him or that he manages to rest peacefully and recover a health point.
Method to explore: when this method is called, a condition arises: if the player’s state is different from “normal” the character will not be able to explore (it is supposed to be in “fight”), if not, the role explores ” a sinuous passage “and it can happen (randomly) that an enemy finds (the state changes to” fight “.
Method to flee: if the player’s state is different from “fight,” the character starts running and calls the method tiredness (). Otherwise, if the country is in “fight” a condition arises: depending on a random value the player may or may not flee from the enemy. In the case of fleeing, the enemy disappears and the state changes to “normal,” if it can not escape, the enemy attacks.
Attack method: when this method is called, a condition arises: if the player’s state is different from “fight,” a message is displayed, and the fatigue method () is called. Otherwise, a condition arises: if the method make_danio () is True, the enemy is destroyed and the state changes to normal (and in case of luck the health and health_maxima rise to 1), otherwise the method enem_ataques ( ) is called.
Enemy_attack method: this method is called when the Player runs out of health points. Finally, a Commands dictionary is defined, which contains the actions that the player can write in the console to interact within the game. You can see more about dictionaries in the following entry: Data types in Python
Now play the part of initializing a player (in this the object “p” is created), and a small introduction is made asking the name of the character, giving help data and the first entry into the cave “buuuuuuu.” The ” while ” loop will make the game run as long as the player’s health is greater than 0. Up to here the analysis, I hope you can understand it, can modify it to your liking (the best way to learn) and of course, that help them.