Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!






A Complete Graphics-less Game #3

Posted by Squared'D, 25 May 2013 · 473 views

prototyping prototype text text based game development beginner
More Design and the First Basic Prototype
In this journal entry, I'll talk about the prototype that I have written. Prototyping is a way to test concepts without committing to anything. Many like to use prototypes to test potential features. I think this is a good idea and I also do it. I also like to prototype code. Sometimes I do this in conjunction with UML to establish relationships and make sure the code structure makes sense. I think it's pointless to just start making the "final" production code right away unless you're working with an established framework or you're a crazy genius. Otherwise you'll end up re-factoring a lot. Re-factoring isn't always bad, but if I have to re-factor I rather find problems and do it earlier.

In my opinion, every prototype should have a clear and well defined purpose. The code doesn't have to be pretty and the same goes for the output, but after the prototype, I should learn something. I'm always careful about using code from a prototype. Usually the code sucks and at best should only be used as a guideline.

In this prototype, I want to quickly make a sample text-based game just so I can get a feel for what I'm about to make. I want to show 4 main things: the main menu, the world map, the battle system, and dialog. To prototype this, I'll make a simple function for each one. I'll code the real game in C++, but for this prototype at least, I'll use more of a C-style code. Why? For me, it's just easier to prototype simple console apps in C.

I'll explain some of the things I did in the code.

WARNING: The code you're about to read is bad. Proceed with caution

I'll start with some forward function declarations.
#include <conio.h>

/* declare our forward definitions. */
void DoMainMenu();
void DoWorldMap();
void DoBattleSystem();
void DoDialog();

bool DoTownOne(); /* true - goto next town */
bool DoTownTwo(); /* false - quit world map */

int main()
{
    DoMainMenu();
}



As you can see, I'm using separate functions to handle the game states and places. In a real game some sort of a state system should be used, but remember, I'm not making a "real" game yet.

Here's the main menu code. It's very simple. Just a few loops for checking input.
void DoMainMenu()
{
    bool finished = false;

    while(finished != true)
    {
        /* display the menu */
        printf("\n============================================================================\n");
        printf("You Got Punk'd\n\n");
        printf("1) Play\n");
        printf("2) Quit\n");
        printf("\n");
        printf("\n============================================================================\n");

        /* get the user's input */
        char input = _getch();

        /* check for valid input */
        bool valid;
        do
        {
            /* initialize valid for this loop */
            valid = true;

            /* the user wants to play */
            if(input == '1')
            {
                DoWorldMap();
            }

            /* the user wants to quit */
            else if(input == '2')
            {
                finished = true;
            }

            /* invalid input */
            else
            {
                printf("Invalid Input\n");
            }
        }  while(valid == false);
    }
}


The world map is also just a loop and it goes back and forth between the towns.
void DoWorldMap()
{
    // the current town
    int cur_town = 0;

    // the return value from the town function
    bool town_return;

    printf("\nYou enter a strange new world.\n");

    do
    {
        /* go to the town */
        if(cur_town == 0) town_return = DoTownOne();
        else town_return = DoTownTwo();

        /* set to the next town */
        cur_town = 1 - cur_town; 
    } while (town_return == true);
}
Here's the code for the two towns.
bool DoTownOne()
{
    while(true)
    {
        /* display the menu */
        printf("\n============================================================================\n");
        printf("Welcome to Bean Town\n\n");
        printf("1) Talk to a stranger. \n");
        printf("2) Go to Gotham City\n");
        printf("3) Quit\n");
        printf("\n");

        /* get the user's input */
        char input = _getch();

        /* check for valid input */
        bool valid;
        do
        {
            /* initialize valid for this loop */
            valid = true;

            /* the user wants to talk */
            if(input == '1')
            {
                DoDialog();
            }

            /* the user wants to go to the other city */
            else if(input == '2')
            {
                return true;
            }

            /* the user wants to quit */
            else if(input == '3')
            {
                return false;
            }

            /* invalid input */
            else
            {
                printf("Invalid Input\n");
            }
        }  while(valid == false);
    }
}

bool DoTownTwo()
{
    while(true)
    {
        /* display the menu */
        printf("\n============================================================================\n");
        printf("Welcome to Gotham City\n\n");
        printf("1) Fight for your life\n");
        printf("2) Run to Bean Town\n");
        printf("3) Quit\n");
        printf("\n");

        /* get the user's input */
        char input = _getch();

        /* check for valid input */
        bool valid;
        do
        {
            /* initialize valid for this loop */
            valid = true;

            /* the user wants to talk */
            if(input == '1')
            {
                DoBattleSystem();
            }

            /* the user wants to go to the other city */
            else if(input == '2')
            {
                return true;
            }

            /* the user wants to quit */
            else if(input == '3')
            {
                return false;
            }

            /* invalid input */
            else
            {
                printf("Invalid Input\n");
            }
        }  while(valid == false);
    }
}

I think it's very easy to see a lot of redundancy in the code. Noticing this will help me decide on a future code architecture.

And for lazy ones like me that never feel like downloading zips to look at code:
void DoBattleSystem()
{
    int my_hp = 200;
    int enemy_hp = 400;

    char input = 0;

    printf("\n\nWelcome to the fight of your life.\n");
  
    while ((my_hp > 0) && (enemy_hp > 0))
    {
        printf("Your HP: %i\n", my_hp);
        printf("Enemy HP: %i\n\n", enemy_hp);
        printf("What will you do?\n");
        printf("1) Use a gun. \n");
        printf("2) Use a knife. \n");
        printf("3) punch him. \n");
        printf("4) Scream\n");
        printf("\n============================================================================\n");

        input = _getch();
        if(input == '1')
        {
            printf("Stranger: You don\'t have a gun.\n");
            printf("You idiot. Stop pointing at me.\n");
            printf("Super Uppercut\n");
            printf("You -75\n");
            my_hp -= 75;
        }
        else if(input == '2')
        {
            printf("Crap. No knife.\n");
            printf("Kill him with a spoon.\n");
            printf("Stranger -199\n");
            printf("\nYou are very powerful.\n");
            enemy_hp -= 199;
        }
        else if(input == '3')
        {
            printf("Stranger: You hit like my sister.\n");
            printf("You: I know your sister well.\n");
            printf("Stranger: Mother #&@ Die.\n");
            printf("You -150\n");
            my_hp -= 150;
        }
        else if(input == '4')
        {
            printf("Stranger: What? You\'re so weak.\n");
            printf("You stab the stranger with a spoon as he walks away.\n");
            printf("Stranger -399\n");
            printf("\nStranger: It hurts a lot.\n");
            enemy_hp -= 399;
        }
        else
        {
            printf("Stranger: What are you trying to do?\n");
            printf("You -15\n");
            my_hp -= 15;
        }

    }

    if(my_hp > 0)
    {
        printf("Stranger: I'm so sorry. Take some money.\n");
    }
    else
    {
        printf("Stranger: I'll let you live because the programmer was too lazy to program a way to end the game here.\n");
    }

    _getch();
}

void DoDialog()
{
    bool finished = false;

    printf("\n============================================================================\n");
    printf("Stranger: What's up?\n");
    printf("1) Nothing much.\n");
    printf("2) The Sky.\n");
    printf("3) I don't know. I'm lost.\n");

    char input = _getch();
    if(input != '2')
    {
        printf("Stranger: I'll kill you!");
        DoBattleSystem();
    }
    else
    {
        printf("Stranger: Ha.Ha. Here's some money.");
        _getch();
    }
}

I'd explain the code more, but I think it's pretty self-explanatory. Even though this is a text based game, I hope readers have a working knowledge of C and C++. If you have any questions though, feel free to leave a comment. In my next entry, I'll analyze this prototype some more and I'll also introduce some things that are missing from it.

Attached Files






The code's not that bad I guess.

Recent Comments

December 2014 »

S M T W T F S
 123456
78910111213
14151617181920
21222324 25 2627
28293031   
PARTNERS