Path to CS Certificaiton : Python Terminal Game
Tags: python, gamedev, beginners, terminal
Published: 2024-04-22 05:18:13

For the past few weeks, I've been working on finishing the Codecademy Computer Science career path certification. (and no, this is not at all sponsored, the opposite actually, they took my money. :< )

I have some coding experience in C++ and C# so I'm not a total beginner. I did however put off learning Python for quite a while. It was something I knew I was going to learn eventually. But I always felt internal resistance to actually doing it.

So, here I am finally getting it done! I had to make a portfolio project for the CS cert, and the prompt was to make a Python terminal game. Examples were tic-tac-toe and making calculators. I wanted to make something that would draw an ASCII art map and let you move a 'character' around the map. I decided to name this 'Dungeon Crawler' because I'm original and full of creativity.

## The Game

!['Dungeon Crawler' Map example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9vbdu3xux238iw7nig8g.png)

There is a sequence of events that happens once the program is run.

1. The main menu is printed on the terminal and shows you options for Starting the game, Exiting, or reading the Rules.

2. When you start the game you're asked to choose a keyboard character to represent yourself on the map. in the image above, I choose my first initial but any key will work as long as it is not a key already in use.

3. After you select a key on the keyboard to represent yourself, the game map is randomly generated. It can be anywhere between a 5x5 map to a 20x20 map and will generate a random amount of items represented as (*) and a random amount of enemies represented as (&). Everything including your character symbol and a goal represented by (#) is randomly placed on the map

4. Player information is printed below the game map. It shows you what symbol you picked to represent yourself, what directions you can move in, and where you're currently located on the map.


![rules screenshot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zlis9v3o0aztdybxihq5.png)

You can type in 'rules' to print out how to play the game, and what the goals of the game are.

## The Code

Under the hood, this game consists of 5 files; characters.py, components.py, map.py, vectors.py, and game.py. There are a total of 7 classes in this project: Player, Enemy, Item, Map, Movement, Stat, and Vector2. 

Game.py there are 5 functions defined: Intro, Start, Rules, PlayGame, and ValidateResponse.

the game starts by calling intro, which prints the start menu. When the player types 'Start' it runs the Start function, which handles spawning items, enemies, the map, and asks the player what symbol will represent them. It calls the  PlaceItems method of the Map to randomly place everything and make sure it gets printed on the screen.

The Start function calls the PlayGame function, passing it the Player, Enemies, and Map. The PlayGame function preforms a while loop, that will continue as long as the game is being played, and the player hasn't won, lost, or exited.

The PlayGame loop takes in an input from the player, passes it to the ValidateResponse function, and assures that the player typed in a valid input. If the player is moving, it uses the Maps UpdatePlayerLocation method to check if a move in the requested direction is within the map boundaries, if the player hits an enemy, or picks up an item. If the player runs into an enemy the player's health status decreases. Each enemy has a random damage  stat, so we find the correct enemy by using the location the player tried to move in to find them. If the player runs into an item, their health stat will heal based on the item's value

After the player moves, if the game is not won or lost we iterate through the list of enemies, and let each enemy move. Enemies can only move if the space in the direction they randomly choose to move in is considered a 'floor' space. they cannot overlap items, players, other enemies, or the goal space. If an enemy hits a player during their turn, the player's health stat will take damage.

As long as the player has not won or lost the game, the loop will continue. Once the player has won or lost the game, a thank you message is printed and the game is exited.

## Conclusion

This took me about 3 days of work to put this together. It's a cute little project and while I still struggle with some Python concepts as a primarily C user. this project goes down in my book as a Win. 

Now a short disclaimer: I am a beginner at using git. I have always used Perforce professionally as a game designer. So It's my first time using git to version control to publish a repository onto GitHub. I didn't add a .ignore file because I didn't know what to ignore yet. So please look past that beginner mistake. 

If you want to check out this small little game, you can grab it here: https://github.com/BrittanyBlairDesign/python_terminal_game