Text-Based Adventure Game: A Milestone in Learning Python
You know how it is for kids growing up: we don't want to eat our vegetables, we don't want to do homework, we want to play some video games.
It's 2022 - but that doesn't mean that the older style of text-based adventure games have gone out of style...alright maybe that's a lie. In the era of Call of Duty and Fortnite, text-based adventure games are a way of enjoying your late 20th century nostalgia. Part of my Python scripting course for my college required me to develop a game using Python, including loops, branches, dictionaries, lists, and functions in order to demonstrate efficiency with the Python language. I would like to share how I went about designing this simplistic game as my first tangible milestone.
For this game, the main theme is that you wake up naked in a dark room with no lighting, with a mattress on the floor, and a gushing wound on your leg. Your goal is to navigate throughout the game's rooms and find items that will help you escape from the situation.
I first started my code out by defining a dictionary of all of the rooms, which creates a map of your possible move directions. There are 8 rooms in total, with some of the rooms having multiple different paths to take to get to another room.
The next thing that I developed was the actual gameplay loop itself. The entire game takes place within a while loop, which makes it so that if a user enters "exit", it can immediately exit the game and stop the running program. I also created a list for the user's inventory of items, as well as put the player into the starting room. The direction variable is set to a blank string right before the loop, but will be set to user input inside of the gameplay loop. The loop says that while user input is not "exit", it will continue the game. (Note the use of f strings here, which I absolutely love, as it makes the code easier to read for the human - also the *pos_moves with the asterisk character is used to unpack the elements inside of the dictionary.)
领英推荐
The next piece of my code that I worked on were the functions for each of the 8 rooms. The functions serve the purpose for appending the item into the inventory list, so that when the player enters a room, the item is automatically added (and printed) in their inventory. The functions also serve two other purposes: 1. printing a unique message the first time the player enters that room (as well as adding the item into the inventory), and 2. printing a different unique message if the player visits that room again. The function for the final room is super important, as it has 6 different if/elif/else statements with different outcomes of the game. For example, if the player did not gather one of the required items prior to making it to the final room, the code will print a unique "game over you lose" dialogue depending on which item the player did not have. See below for an example of one of the rooms.
One of the final steps of my development required me to return back inside of the gameplay loop in order to change the player's current location based on the input of their direction. Each time that they entered a move, I had to use a loop or an if/else branch to find out if the input was good or bad. If the input was not a cardinal direction or if the input did not connect to another room, the player was told: "Invalid direction, try a different one". Inside of this loop, the variable of the player's current location gets changed, as well as calling the function for the room that they have entered. A snippet of the if/elif/else branches can be seen below.
Overall, this was a project that I started off my term looking forward to. However, as the weeks went on, we were learning more detailed information that started to really wear me down mentally. But the week before this was due, we were required to work on a smaller assignment to help us prepare for utilizing a dictionary with key/value pairs in order to move in different directions. This smaller assignment was critical in developing the main code here. Although I don't plan on doing any sort of further game development in my spare time, this was an extremely fun project to work on, and it went a lot smoother than I thought at the beginning of the year.
Thanks for sharing!??
SOC Analyst ?? Views are my own
2 年??