Hello community, I haven't written articles in a while, but I want to change that. I've been very busy, as always, developing development projects like the example I'm about to show you, which is more educational than anything else.
This small project was written in Python as part of an assignment I'm doing on Codecademy, as part of a review I need to keep my knowledge fresh or up to date. The project consists of demonstrating how to program in OOP, or object-oriented programming, which in software development is vital for encapsulating part of the programs. In other words, it's essential for creating an abstract program and, more easily, managing program design based on the type of object and its characteristics.
Project Overview
What this program, which I called "Inventory Management RPG," does: It's a role-playing game program, or RPG, where you buy items, and at the same time, the player manages those items. It sounds easy, but really think carefully first and take notes on the concepts, including the type of information, then how many classes should be created, data persistence, and data entry management.
For example:
The object, as we already know, in this case would be the player with its properties in the game. In this case, we also determine the type of information:
- name, which is a string;
- health is a string;
- gold is also a string;
- inventory, which is an array where we store the player's items.
Now that we know the properties and type of information, we can proceed to add methods to this class. For the other classes, which were shop and item, I did the same, adding the type of information and methods. I'm being brief to cover more features and gameplay.
In addition to this, I used Python 3.6+, file I/O, dictionaries, etc., and other language features to implement the game flow in a logical and understandable way. What was notable is that, like other languages, the one I'm more familiar with, such as JavaScript, only the methods change, but almost all of them use the same methods for file management and data entry.
Key Features Implemented
- Dictionary management for the inventory, in which we extract key and value pairs.
- File persistence with the save/load functionality, for which we use file handling operations.
- User input for error handling, which we do at the game's entry in the main.py file, which is the central hub for managing the various classes.
Code Highlights
from player import Player
from shop import Shop
We imported the necessary classes so that the main file can interact with these classes.
def main():
print("Welcome to Inventory Management RPG!")
print("Type 'help' for available commands!")
player_name = input("Enter your character name: ")
player = Player(player_name)
shop = Shop()
print(f"\nWelcome, {player.name}!")
This is the entry point where we print a warm welcome to the users, then ask for the player's name, then we instantiate the classes and print a welcome from the user's input previously asked.
while True:
user_input = input("> ").lower().strip()
if user_input == "quit":
print("Thanks for playing")
break
elif user_input == "help":
print("Available commands: help, stats, inventory, shop, buy [item], save, quit")
elif user_input == "stats":
player.show_stats()
elif user_input == "inventory":
player.show_inventory()
elif user_input == "shop":
shop.show_items()
elif user_input.startswith("buy "):
item_name = user_input[4:].title() # Remove "buy " and capitalize
shop.sell_item(item_name, player)
elif user_input == "save":
player.save_game()
else:
print("Unknown command. Type 'help' for available commands.")
Now we continue with the while loop part, which we use as a true condition. To implement if/elif statements, we could also use the match case in Python 3.1, which is similar to the switch statement in JavaScript. For this implementation, we use if/elif, which is more standard in Python in general. The first thing we do is declare the user input variable, removing spaces and using lowercase. After that, we create the various user input options, comparing them with the input string, and then printing the instructions or methods of the player or store class.
if __name__=="__main__":
main()
Finally, if we have the entry point, we invoke it.
Difficulties and Solutions
The hardest part of the game may be coming up with the idea for how to make the game, because it's the part where you don't know what you can invent, so you start looking at ideas on the internet. I'd say that's the hardest part. On the other hand, we currently have a lot of help, like AI, but this really is a complement to our ideas; it's not 100% bulletproof. Once we have the mix of ideas in mind, we begin to look at the parts of the game and how to model the data types in the classes, as well as the interconnection of the classes and how to program their methods with the basic tools of the language.
And finally, the implementation of the ideas themselves, the design of the game or program as it would be in real life, such as when we implement classes with APIs, this is another level.
Future Improvements
Sometimes when I create my repositories, I forget about them for months, if not years, but one truly loves several projects, and this is one of them. What I would do is add another player, make online RESTFUL API calls. Of course, we'd have to implement a lot of testing, ensuring security is already at an online level. We could create a graphical interface. Truly an online game, and instead of saving data in .txt files, we could save it in NoSQL databases so that players have a central point to save or load data, also using caching.
But sometimes we don't have time, we're busy with work, and for me this is no exception to the rule, I hope it's not for you either (just kidding).
Conclusion
Well, I've talked about a little bit of everything in this experience. Finally, I'm back writing for this great community that we love so much, even if it's been from a distance for months since I last wrote. Yes, I was busy because I was developing my own blog-like platform, https://trovetrends.com, but I still have a long way to go, but here we go. My shame about you. Regarding our game, let me know if you want me to write more articles like this one. I'll be more attentive, since we only saw the tip of the iceberg of how to build a simple OOP-based game: ideas, design, implementation, difficulties, and future improvements. I have nothing more to add; nothing more to say, in advance: thank you for taking the time to read this short article.
About the Author
Ivan Duarte is a full-stack developer, Node.js and Python developer, content writer, entrepreneur, and founder of ByteUp LLC, a software development company.
Top comments (0)