DEV Community

Cover image for 🚊 How to make a terminal-based metro game
Ivy Chen
Ivy Chen

Posted on

🚊 How to make a terminal-based metro game

I got this idea from an Instagram reel that I've seen recently where these people play a game to see which metro stop it'll take them to! I thought it'd be good practice to digitize this and make it into a terminal game. If you're not familiar with the Taipei metro system, don't worry. You can easily replicate the steps here to suit your city's metro system and make a unique one for the metro system of your choice!

Image description

🚊 Let's get into the code

First, you want to create dictionaries for each of the stations in selected metro lines. My city's MRT stations come with an associated number, but if your metro doesn't, then just assign a number to each from one direction to another. Create a dictionary for each metro line.

brown = {1: 'taipei zoo', 2: 'muhza', 3: 'wanfang community', 4: 'wanfang hospital', 5: 'xinhai', 6: 'linguang', 7: 'liuzangli', 
         8: 'technology building', 9: 'daan', 10: 'zhongxiao fuxing', 11: 'nanjing fuxing', 12: 'zhongshan junior high', 
         13: 'songshan airport', 14: 'dahzi', 15: 'jiannan road', 16: 'xihu', 17: 'gangqian', 18: 'wende', 19: 'neihu', 20: 'dahu park', 
         21: 'huzhou', 22: 'donghu', 23: 'nangang software park', 24: 'nangang exhibtion center'}

Enter fullscreen mode Exit fullscreen mode

Next, it's time for getting the user's inputs. You want to get which station they're starting from and what line that station is on.

starting_station = input("Which station are you starting from? ")
color_line = input("Which color is your station on? ")
Enter fullscreen mode Exit fullscreen mode

You want to convert the color_line input to a dictionary to easier access later. So...

color_dict = eval(color_line)
Enter fullscreen mode Exit fullscreen mode

Then, we want to emulate a coin toss to see if it's heads or tails. If it's heads, you will go north/east from where you are. If it's tails, you will go south/west. Don't forget to import random at the beginning of the file.

coin_flip = ['heads','tails'] 
coin_flip_result = random.choice(coin_flip)
Enter fullscreen mode Exit fullscreen mode

You want to do the same thing for rolling a dice too. In which you can go like this.

dice = [1, 2, 3, 4, 5, 6]
randomize_number = random.choice(dice)
Enter fullscreen mode Exit fullscreen mode

Then comes the fun part, implementing the logic! I used a nested if/else conditional to make it work.


for station in color_dict: 
    if starting_station == color_dict.get(station):
        # station is the key aka number
        if coin_flip_result == 'heads':
        # increase station number 
            destination = station + randomize_number
            print(f"đŸĒ™ Your coin flip turned out to be {coin_flip_result}")
            print(f"🎲 You are going {randomize_number} stops in {heads} direction on the {color_line} line")
        elif coin_flip_result == 'tails':
        # decrease station number 
            destination = station - randomize_number
            print(f"đŸĒ™ Your coin flip turned out to be {coin_flip_result}")
            print(f"🎲 You are going {randomize_number} stops in {tails} direction on the {color_line} line")
Enter fullscreen mode Exit fullscreen mode

I also used a try/exception to catch an index out of bounds error just in case you're starting at a station that's towards the end of the lines and you number from the dice exceeds what is possible.

try: 
    print(f"đŸĨ Your destination is {color_dict[destination]}!")

except:
    print("☚ī¸ Try again, index out of bound")
Enter fullscreen mode Exit fullscreen mode

💭 Conclusion

And that's it! Make sure to run python3 filename to run it while you're working on it! Here's the Github repo if you need a fuller look at it.

Top comments (2)

Collapse
 
mmascioni profile image
Matt Mascioni

Randomizing which stop to end up at sounds like such a fun game, thanks for sharing your project! 🎉

Collapse
 
ivavay profile image
Ivy Chen

Thanks for reading / checking it out!