Text-Based Adventure Game: A Milestone in Learning Python

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.

The Oregon Trail - "You have died of dysentery."?

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.

Python Code - dictionary of rooms

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.)

Python Code - gameplay loop

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.

Python Code - room function example
Python Code - room function example #2

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.

Python Code - if/elif/else branches inside the loop

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.

要查看或添加评论,请登录

Ryan Sapone的更多文章

  • Navigating Chaos: Lessons Learned in Incident Response - Web App Compromise

    Navigating Chaos: Lessons Learned in Incident Response - Web App Compromise

    In cyber security, the common saying is, "it's not if - it's when" So what do we do when the when happens? That's what…

    2 条评论
  • Why You Don't Need a Background in Cyber to be in Cyber

    Why You Don't Need a Background in Cyber to be in Cyber

    We've all heard it and seen it in this industry - stop me if you've heard this one: You need to work help desk and work…

    5 条评论
  • My Journey Into Cyber Security (part 4)

    My Journey Into Cyber Security (part 4)

    It's been a pretty eventful three months since the last time I wrote an article about my cyber security journey. In…

    2 条评论
  • My Journey Into Cyber Security (part 3)

    My Journey Into Cyber Security (part 3)

    I am publishing part three of my journey to get into a cyber security career. A lot has happened since I wrote part two…

    1 条评论
  • My Journey Into Cyber Security (part 2)

    My Journey Into Cyber Security (part 2)

    It's time for my follow-up to the first article I wrote on June 7th. In this article, I'll get to talk a little bit…

    5 条评论
  • Practical Labs: Nmap Scans and Wireshark Pcaps

    Practical Labs: Nmap Scans and Wireshark Pcaps

    No matter where you are in your IT career, chances are you've heard of at least one of these two tools: Nmap (network…

  • My Journey Into Cyber Security (part 1)

    My Journey Into Cyber Security (part 1)

    This is the first article that I have ever written on any professional type of website. Sure, we've all made forum…

    1 条评论

其他会员也浏览了