In this Python command line text RPG (role playing game) video we make a simple text-based game where the player finds themselves in a dark dungeon and must choose between two doors. The objective is to choose the correct door that leads to a desirable outcome, such as finding treasure, while avoiding the dangerous outcome, such as encountering a monster.
Where available I use affiliate links in my posts. 🔗
The script starts by importing the necessary modules: Python
random for generating random events and Python
time for adding pauses in the game.
There is a function
print_pause that prints a message and adds a pause of 2 seconds for a better game experience.
intro function presents the initial scenario to the player, describing the dark dungeon and the two doors. It prompts the player to choose between door "1" or door "2".
choose_door function handles the player's choice. If the player chooses door "1", they find a room filled with gold and win the game. If the player chooses door "2", they are attacked by a monster. A random event occurs to determine the outcome: a 50% chance of defeating the monster and finding a hidden treasure, leading to victory, or a 50% chance of being defeated by the monster, resulting in game over. If the player enters an invalid input, they are prompted to try again.
play_game function orchestrates the flow of the game by calling the
intro function to set the initial scene and then calling the
choose_door function to handle the player's choice.
Finally, the script executes the
play_game function, starting the game and allowing the player to play and experience the outcomes based on their choices.
Complete script from the video
Here's the full Python script for the text RPG, from the video:
print_pause("You find yourself in a dark dungeon.")
print_pause("In front of you, there are two doors.")
print_pause("One door leads to treasure, the other leads to danger.")
print_pause("Which door do you choose (1 or 2)?")
door = input("Enter 1 or 2: ")
if door == "1":
print_pause("You open the door and find a room filled with gold!")
print_pause("Congratulations! You win!")
elif door == "2":
print_pause("You open the door and are attacked by a monster!")
if random.randint(0, 1) == 0:
print_pause("You defeat the monster and find a hidden treasure!")
print_pause("Congratulations! You win!")
print_pause("The monster is too powerful! You are defeated! Game over.")
print_pause("Invalid input. Try again.")
Learn Python game development
Here are some additional resources you might like to check out if you're interested in Python game development.
If you're into game development, and want to take it a step further, you might want to check out Pygame. Pygame is a popular open-source library for Python that is specifically designed for game development and multimedia applications. It provides a powerful set of tools and functionalities that simplify the process of creating games and interactive applications.
Here's a comprehensive overview of Pygame:
Graphics and Rendering
Pygame allows developers to handle graphics and rendering efficiently. It provides a versatile surface module for creating and manipulating images, shapes, and animations. Developers can draw lines, circles, rectangles, and polygons, as well as apply transformations and blending effects to create visually appealing graphics.
Audio and Sound Effects
Pygame offers features for working with audio and sound effects. It supports loading and playing different audio file formats, such as WAV and MP3, enabling developers to incorporate background music, sound effects, and interactive audio elements into their games. Pygame also provides functions for controlling playback, adjusting volume, and implementing audio filters.
Pygame simplifies the process of handling user input. It provides functions to detect and respond to keyboard, mouse, and joystick events. Developers can easily capture keystrokes, mouse movements, mouse clicks, and joystick inputs, allowing for intuitive and responsive user interactions in games.
Pygame offers built-in collision detection functions that help developers detect and handle collisions between game objects. This is crucial for implementing game mechanics like object interactions, character interactions, and obstacle avoidance. Pygame's collision detection capabilities enable developers to create dynamic and interactive game environments.
Pygame provides a comprehensive event handling system. It allows developers to respond to various events, such as keyboard and mouse inputs, window resizing, and system events. This system facilitates creating event-driven games and interactive applications where different actions are triggered based on user interactions and system events.
Pygame simplifies the management and manipulation of sprites, which are essential elements in 2D game development. It provides a sprite module that includes classes and functions for creating, animating, and controlling sprites. Developers can easily handle sprite movements, collisions, and animations, enabling the creation of dynamic and visually appealing game characters and objects.
Game Development Support
Pygame offers a range of features and utilities that support game development. These include functions for managing game loops, timing and frame rate control, managing game states and transitions, loading and saving game data, and handling fonts and text rendering. Pygame's built-in functionalities streamline the development process, allowing developers to focus on game design and logic.
Cross-Platform and Community Support
Pygame is designed to be cross-platform, meaning games developed with Pygame can be run on multiple operating systems, including Windows, macOS, and Linux. Pygame has an active and supportive community, with numerous online resources, tutorials, and examples available. This community support makes it easier for developers to get started with Pygame, troubleshoot issues, and find inspiration for their game projects.
In summary, Pygame is a powerful library for Python that simplifies game development and multimedia applications. Its rich set of features for graphics, audio, input handling, collision detection, and event management make it a popular choice among developers. Pygame's cross-platform compatibility and supportive community further contribute to its popularity as a game development tool.
Codecademy, an online learning platform, offers several courses and learning paths related to game development.
Codecademy offers a range of game development courses that cater to different skill levels and programming languages. Their offerings include introductory courses for beginners, courses focused on specific game development engines or frameworks, and comprehensive learning paths dedicated to game development.
The courses cover various aspects of game development, including game design, programming logic, graphics rendering, audio integration, and implementing game mechanics. It's recommended to visit the Codecademy website for the latest information on their game development course offerings.
Udemy Python game development courses
Udemy has a robust offering of Python game development courses. I've taken several full (think 20+ hour long) courses on Udemy, and have had an excellent experience with each of them.
Often, when I run out of YouTube videos about a Python technique I'm learning, I'll turn to Udemy for help.
Udemy offers a wide range of Python game development courses that cater to beginners, intermediate learners, and experienced developers. These courses are designed to provide comprehensive guidance and hands-on practice in creating games using Python programming language and relevant libraries or frameworks.
Here are some key aspects of Udemy's Python game development course offerings:
Udemy offers a diverse selection of Python game development courses, covering different aspects and levels of game development. You can find courses that focus on specific libraries or frameworks like Pygame, as well as courses that cover broader game development concepts using Python.
Many of Udemy's Python game development courses are designed with beginners in mind. They assume little to no prior experience in game development or programming, providing step-by-step instructions, explanations of core concepts, and practical examples to help learners grasp the fundamentals.
Udemy courses often emphasize hands-on learning through projects. Learners are guided through the development of complete games, starting from scratch or building upon existing templates. This approach allows learners to apply their knowledge in a practical manner, reinforcing the concepts and skills they acquire.
Udemy courses typically cover a wide range of topics and techniques related to Python game development. This may include game mechanics, graphics rendering, collision detection, user input handling, audio integration, game physics, and more. The courses aim to provide a well-rounded understanding of game development principles and practices.
Udemy's Python game development courses are often led by experienced instructors who have a strong background in game development and programming. These instructors share their expertise, insights, and best practices throughout the course, offering valuable guidance and tips for creating high-quality games.
Udemy courses offer the flexibility of self-paced learning. Learners can access the course materials, including video lectures, code examples, and supplementary resources, at their convenience. This allows learners to learn at their own speed, pause and review lessons as needed, and apply the acquired knowledge in their own game development projects.
Udemy provides a platform for learners to engage with instructors and fellow students. Discussion boards, Q&A sections, and community forums enable learners to ask questions, seek clarification, and share their experiences. This fosters a supportive learning environment and enhances the overall learning experience.