Python RPG Game (Part 1)

How to create your own text-based RPG in Python using Object Oriented Programming.

David Torres
Feb 12 · 3 min read
Photo by Cláudio Luiz Castro on Unsplash

I spent 15 days creating this RPG Game in Python, if you want to skip the tutorial and go straight to the code and read it, here’s the project in GitHub!

By the end of this series of articles, you’ll be able to create something like the GIF you see down below, where you’ll have a text-based RPG which I’ll be covering in a set of articles.

If you have any improvements on the code or any suggestions, please let me know. Have fun and enjoy the article!

In the following article I’ll be covering the:

  • Hero Class (with its Getters and Setters)

Step 1: Hero Class

Hero Class:

There are 7 different attributes for the Hero class, which would be the hero:

  • Health = Hhealth

Setters and Getters

We’re going to use these attributes for the setters and getters which are ways to call the attributes of the Hero and edit its attributes. For example if the Hero gets attacked, we can call the attribute “Hhealth” and reduce its health or if the Hero level up, then we can increase the attribute “Hhealth”.

We use getters & setters to add validation logic around getting and setting a value. The way we create a Getter in Python, is like this:

def getHealth(self):
return self.health

Is like any other method for a class, and we’re calling the function “getHealth”, because we’re going to use it for later in the game whenever we want to call the function to edit the health of the Hero.

The way we create a Setter in Python, is like this:

def setHealth(self, newHealth):
self.health = newHealth

Here’s a pretty good example from geeksforgeeks which explains how setters and getters work in Python:

class Geek:def __init__(self, age = 0):
self._age = age
# getter method
def get_age(self):
return self._age
# setter method
def set_age(self, x):
self._age = x
raj = Geek()# setting the age using setter
raj.set_age(21)
# retrieving age using getter
print(raj.get_age())
print(raj._age)
....
----->21
----->21

Hero Class Code:

Step 2: Enemy Class

Enemy Class:

Similar to the Hero Class, in the Enemy Class we’re going to use setters and getters, but we’re going to create a subclass: “The Boss”

The way we’re going to do this is by simply inheriting the enemy class as the parent class and we’re going to use the boss class as the child class. You can do it by doing the following:

class Boss(Enemy):    
def __init__(self, Ehealth, Eattack, Especial, Echance, Ename, EsuperMove):
# in order to actually inherit the Enemy class we need to use the super method
super().__init__(Ehealth, Eattack, Especial, Echance, Ename)
self.superMove = EsuperMove

Credits: Youtube, Advanced Python Text Adventure.

Writers’ Blokke

The publication for writers and readers to create and read…

Thanks to The Startup

Sign up for What's New On The Blokke?

By Writers’ Blokke

Newsletter from your editor of Writers' Blokke Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

David Torres

Written by

Technical Consultant | Python Developer | Machine Learning | Data Analytics | DataScience | Mexican. LinkedIn: https://www.linkedin.com/in/david-tc/

Writers’ Blokke

The publication for writers and readers to create and read amazing content

David Torres

Written by

Technical Consultant | Python Developer | Machine Learning | Data Analytics | DataScience | Mexican. LinkedIn: https://www.linkedin.com/in/david-tc/

Writers’ Blokke

The publication for writers and readers to create and read amazing content

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store