Crafting Your Own Python Text-Based RPG: Part 2
Written on
Chapter 1: Introduction to RPG Development
In this second installment, we'll continue our journey in crafting a text-based RPG using Python and Object-Oriented Programming principles. If you're looking to start from the very beginning, don't forget to check out part 1 for the foundational steps. For those eager to dive straight into the code, our project is available on GitHub!
By the conclusion of this series, you’ll have developed a game similar to the one showcased below:
RPG Game by David Torres
I welcome any suggestions or improvements you might have regarding the code. Enjoy the tutorial!
In the upcoming sections, I will discuss:
- Enemy Generator
- Enemy Attack Mechanics
Section 1.1: Enemy Generator
The enemy generator functions just like any standard Python function. It takes a boolean argument, levelBoss, which indicates whether the generated enemy is a boss (True) or a regular foe (False).
To assign a name to our enemy, we utilize several text files:
- adjective.txt
- animal.txt
These files each contain lists of words, allowing us to randomly generate a name for our enemy, 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 Breakdown:
The objective is to randomly select an adjective from our list. Since Python lists are zero-indexed, we fetch a random adjective using the following line:
adjective = lines[random.randint(0, len(lines)-1)][:-1]
Here, [:-1] is used to remove the newline character at the end of each line. The same approach is applied to animal.txt to generate a random animal name.
Section 1.2: Differentiating Enemies
The levelBoss argument helps us distinguish between a boss and a regular enemy. If levelBoss is set to True, we create a boss with higher stats; if False, we generate a common enemy.
if levelBoss == False:
health = random.randint(50, 100)
attack = random.randint(5, 10)
special = random.randint(10, 20)
chance = random.randint(1, 10) # Create a common enemy with these stats
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 a Boss object with these stats
return Boss(health, attack, special, chance, adjective + " " + animal, superMove)
The function random.randint(x,y) generates a number between x and y, for instance:
health = random.randint(50, 100)
This means the enemy's health will range from 50 to 100. Ultimately, we return the enemy along with its designated attributes.
Chapter 2: Implementing Enemy Attacks
Next, we will explore the enemy attack function, which takes four parameters: hit_chance, attack_value, name, and defence. Since we have already created a monster in the previous step, the only missing component is the hit chance, which will be discussed in part 3. Here's a brief preview of the code:
In this video, "Python Text RPG (Part 2) - Making a Title Screen!" we delve into creating an engaging title screen for our RPG.
Additionally, "Python Text-Based RPG Tutorial Part 2 - Setting up the Game" provides insights into setting up the foundational elements of the game.
I hope you're enjoying this series! Feel free to share your thoughts in the comments.
Credits: YouTube, Advanced Python Text Adventure.