Creating Your Own Text-Based RPG in Python: Part 2 Overview
Written on
Chapter 1: Introduction to RPG Development
In this second part of our series, we will delve into how to craft a text-based RPG using Object-Oriented Programming in Python. If you missed the first part and want to start coding the game from the ground up, be sure to check it out. For those eager to dive straight into the code, you can find the complete project on GitHub!
By the conclusion of this series, you'll be able to create an engaging RPG experience similar to this:
If you have any feedback or enhancements regarding the code, please share your thoughts. Enjoy the article!
Chapter 2: Key Features of the RPG
In this section, we will explore two essential components: the Enemy Generator and Enemy Attack.
Section 2.1: Enemy Generator
The Enemy Generator functions similarly to any standard function in Python. It accepts a parameter called levelBoss, a boolean variable indicating whether the enemy is a boss (True) or a common foe (False).
To generate names for our monsters, we utilize several text files containing different sets of words:
- adjective.txt
- animal.txt
These text files contain diverse words that we will read to create random names for our monsters, such as "Strong Rat" or "Stinky Bird." Here’s how we implement this:
import random
with open('adjective.txt', 'r') as file:
lines = file.readlines()
adjective = lines[random.randint(0, len(lines)-1)][:-1]
with open('animal.txt', 'r') as file2:
lines2 = file2.readlines()
animal = lines2[random.randint(0, len(lines)-1)][:-1]
Code Explanation:
The objective is to randomly select an adjective from our list. The line adjective = lines[random.randint(0, len(lines)-1)][:-1] accomplishes this by retrieving a random adjective while removing any newline characters.
We apply the same method to animal.txt for generating a random animal name.
Section 2.2: Boss vs. Common Enemy
The levelBoss parameter helps us distinguish between generating a boss enemy and a common one. If levelBoss is set to True, we create a boss enemy; if False, a standard enemy. Here’s how the code works:
if levelBoss == False:
health = random.randint(50, 100)
attack = random.randint(5,10)
special = random.randint(10,20)
chance = random.randint(1,10)
return Enemy(health, attack, special, chance, adjective + " " + animal)
else:
health = random.randint(200, 250)
attack = random.randint(20, 40)
special = random.randint(50, 60)
chance = random.randint(1, 8)
superMove = random.randint(100,200)
return Boss(health, attack, special, chance, adjective + " " + animal, superMove)
The random.randint(x,y) function allows us to select a random number within a specified range. For instance, health = random.randint(50,100) defines the health range for the enemy.
Chapter 3: Implementing Enemy Attack Functionality
Step 3 involves creating an enemy attack function that takes four parameters: hit_chance, attack_value, name, and defence. As we established a monster in the previous section, the only missing element is hit_chance, which I will discuss in the next part. Here’s a preview of the code.
To supplement your learning, check out the following video:
This video provides a walkthrough for creating a title screen for your RPG.
Additionally, for a more detailed exploration of setting up the game, refer to this video:
I hope you are enjoying this series! Please share your thoughts in the comments below.