Building a Text-Based Dungeons and Dragons Game with Python
Introduction
As part of my Python Web Development Career Track with CodingNomads, I built a text-based adventure game, inspired by Dungeons and Dragons. The objective of this project was to invest in my understanding of Python fundamentals, such as user input, conditionals, variables, and control flow.
This article provides a walkthrough of this project, including:
- Project concept and requirements
- How the project was built
- A walk-through of its code
- Lessons learnt
- Potential improvements to the project
Project Concept
This game simulates a basic dungeon exploration scenario, where the player must choose between two doors. Depending on their choice of door, they can encounter a sword, face a dragon, or be defeated.
Key Features:
- The user enters their name and gets welcomed to the game
- The player chooses a door (“left” or “right”)
- If the player explores and picks up a sword, they have an opportunity to slay the dragon
- If the player encounters the dragon without the sword, they lose the game
Implementation
This program was implemented in a single Python script, run in the CLI. The following sections describe its main components.
1. User Input and Greeting
name = input("Type your name: ")
print("Welcome,", name + ", to the game world!")
Code in this section:
- input() - for storing and collecting input from the player
- String concatenation, to ensure that the program output is context-specific
2. Door Selection
door_choice = input("Pick between these two doors, left and right: ")
if door_choice == "left":
print("Nothing is behind this door")
if door_choice == "right":
print("DRAGON!")
This section of the script uses branching logic via if statements to create different user outcomes.
3. Returning or Exploring
return_to_previous_door = input("Would you like to return to the previous door (you can say yes)? ")
if return_to_previous_door == "no":
print("DRAGON!")
print("You lose the game, you were defenceless!")
This uses nested user interactions, to provide extra options for the user to choose between.
4. Sword Acquisition
if return_to_previous_door == "yes":
print("You encountered a sword!")
pick_up_sword = input("Pick up sword (you can say yes)? ")
This part of the program uses variables, to track whether or not the user has picked up the sword. The status of this tracking will determine if the user wins the game.
5. Final Encounter
if pick_up_sword == "yes":
can_fight_dragon = True
print("You defeated the dragon!")
if pick_up_sword == "no":
print("DRAGON!")
print("You lose the game, you were defenceless!")
The Boolean called can_fight_dragon
is True if the sword is encountered and picked up. This is the winning condition of the game, if the user encounters the dragon and this variable is True.
Lessons Learnt
This project explored Python fundamentals, such as:
- User Input Handling: acquiring user feedback and implementing this via string concatenation
- Conditional Statements: using branching logic with if statements
- Boolean State: using the boolean
can_fight_dragon
to track the progress of the game - Control Flow: building a project with a certain logical sequence of events
Potential Improvements
The current version of this project is linear. Possible improvements to this include:
- Adding multiple rooms to the dungeon and various character narratives
- Introducing a Health Point (HP) system
- Allowing the user to fight the dragon with different combat moves
- Using an inventory system to log the weapons that the user has picked up
- Adding functions to the code, to make it more modular
- Adding loops to the code to make the game replayable, without having to restart it
- Using Flask or Django, to convert the CLI-based game into a full web application
Source Code
This project is available on GitHub, at: https://github.com/franpanteli/CodingNomads-python-101/blob/main/labs/projects/dungeons_and_dragon_game.py/dungeons_and_dragon_game.py
Conclusion
Building this project provided me with education on Python’s foundational concepts. This program demonstrates how user input, conditional, and state management may be used to create interactive applications. This project could be improved by using web-based interfaces, rather than a CLI. This could secondly be improved by using more complex game mechanics. This would allow more advanced Python concepts to be applied.
Top comments (1)
This is awesome. Great work, @fran_panteli!