Monday, October 19, 2015

Starting a smaller project... an ASCII Rogue-like!

Let's make an ASCII Rogue-like Part 1!

I wanted to make this Rogue-like to practice my C/C++ whenever I get a little overwhelmed by Handmade Hero. It's a good way to reinforce skills I've learned while providing a guidepost for my own game development.  This project is based on the awesome YouTube channel of Making Games With Ben. You should check it out!

Step 1: Read in a map and print the map

We're going to read in our maps for this Rogue-like from a text file.  So let's get organized.  

  1. We should set up our entry point for Windows (aka your main.cpp)
  2. We should have an entity that controls the game flow and logic. 
    1. Let's call the class GameSystem.
  3. And finally, we should have a class that assembles our map from the file and prints it out. 
    1. Let's call this class Level.

Step 2: Setting up main.cpp

  • We're going to come at this problem top down as a means to connect concepts together, so bear with me!  
  • As described about, GameSystem will handle the game logic and flow, so we'll need to include it (I know, it's not created yet)
  • Create a GameSystem type and we'll send the constructor the name of our file.
  • Our GameSystem class will, at least for now, handle our main game loop called playGame()
  • And finally, return a 0, to let the compiler now everything executed fine.
  • From here, let's make that GameSystem class! 

Step 3: Setting up GameSystem.cpp & GameSystem.h



  • GameSystem is going to act as the nexus for our game.  
  • So let's include "Level.h" (which hasn't been created yet!) in our header file.
  • Just like we declared in our main.cpp, the GameSystem constructor will make the level with the text file named in the ().
  • Let's make void playGame(), like we had promised in main.cpp.
  • And finally, we need to make a reference to our Level class.  
    • Remember, Level will do everything level related.
    • GameSystem is simply the conductor calling on all the pieces.

  • And now, we get to implement the header file!
  • For now, the constructor will load our textfile & then print it.
  • The system pause is a Visual Studio thing, nothing to worry about.
  • And finally, implement playGame(). Nothing need to be there just yet!

Step 4: Setting up Level.cpp & Level.h



  • We need to forward declare all the Level function we promised in GameSystem.
  • public will hold the print() and the load(string fileName).
  • Under private, we'll make our string vector that will hold the data we get from our files.

  • In our load function, we need to use the fstream library to bring files in.  
  • Create the file, open the file, and declare a local string variable to hold the line.
  • Set up a while loop that will continue to read lines as long as they exist. 
  • Use vector's pushback function to add them to our vector.
  • And close the file.
  • Don't forget to use perror for useful debug messages.
  • And finally, the print function will just loop though levelData to print our level to our screen.

Step 5: Make you ASCII file and test it out!

Mine looks like this... And when you run this, it should print out your level. Have fun!

No comments:

Post a Comment