My first game

Started by
2 comments, last by pulpfist 16 years, 2 months ago
Okay, for the past few weeks I've been messing around with some programming languages and have gotten to the point to where I'd like to make a text based game. Mainly I'm doing Visual Basic for my VB classes I'm taking, but I've been using what I learn there and applying it to C++...sorta; either way, the stuff I've learned in my VB classes has helped C++ click with me better and I've been able to understand more a lot better. I'm using Dev-C++ and I've already made a few programs just doing simple equations, etc. etc. Anyway, I have recently started making a text based game where you use the number pad to navigate (ex. 8 would be North, 2 would be South, 6 would be East, 4 would be West) and you move from rooms to rooms. The main problem I have is moving back and forth between the "rooms". Here is a sample of what I have: #include <iostream> using namespace std; int main() { int a; int b; cout << "Welcome to the maze." << "\n"; cin >> a; if (a == 8) { cout << "You progress farther." << "\n"; cin >> b; if (b == 2) { } } system("pause"); } I plan on having it move you back to the original room for the function after "if (b == 2) but I can't figure it out. If anyone can offer advice on how to do this I would greatly appreciate it; and if you have a way to simplify the code I'd really appreciate that too.
Advertisement
Yikes! You're not going to get very far that way. That type of "giant if statement" program will quickly become unmanageable. What you want is a data-driven program. You want level data, either read from a file or embedded in the program. There's no better way to explain this than to show it.

#include <iostream>#include <limits>using namespace std;struct room {    int n, s, e, w;    bool can_go( int dir ) {        switch(dir) {        case 8:            return n != -1;        case 2:            return s != -1;        case 6:            return e != -1;        case 4:            return e != -1;        default:            return false;        }    }    int go( int dir ) {        switch(dir) {        case 8:            return n;        case 2:            return s;        case 6:            return e;        case 4:            return e;        default:            return 0;        }    }    void display() {        cout << "You can go:" << endl;        if( can_go(8) )  cout << "  North" << endl;        if( can_go(2) )  cout << "  South" << endl;        if( can_go(6) )  cout << "  East" << endl;        if( can_go(4) )  cout << "  West" << endl;    }} rooms[] = {    { -1,  -1,  1,   -1 },   // 0    { 2,   -1,  3,   0  },   // 1    { -1,  1,   6,   7  },   // 2    { -1,  -1,  4,   1  },   // 3    { 5,   -1,  -1,  3  },   // 4    { -1,  4,   -1,  -1 },   // 5    { 8,   -1,  -1,  2  },   // 6    { -1,  -1,  2,   -1 },   // 7    { -1,  6,   -1,  -1 }    // 8};int main( int argc, char* argv ) {    // The player starts in room 0    int player = 0;    // Calculate the number of rooms    int num_rooms = sizeof(rooms) / sizeof(room);    // Go until the player reaches the last room    // We add 1 because there are 9 rooms, but since arrays start    // at index 0, the last room is room #8    while( player + 1 != num_rooms ) {        cout << "You are in room #" << player << endl;        rooms[player].display();        cout << "Which way do you want to go?" << endl;        int move;        cin >> move;        // Check for bad input.        if( cin.fail() ) {            cout << "Invalid input" << endl << endl;            cin.clear();            cin.ignore( numeric_limits<streamsize>::max(), '\n' );            continue;        }        // Can we go that way?        if( rooms[player].can_go(move) ) {            cout << "You have moved to a new room" << endl;            player = rooms[player].go(move);        } else {            cout << "You cannot go that way" << endl;        }        cout << endl;    }    cout << "You win!" << endl;    system("pause");    return 0;}


The first thing to look at is the "room" structure. The structure has 4 variables, one for north, south, east and west. If you can go to a room by that direction, there is a room number. If not, there is -1 (an invalid room number). Each room struct also has 3 functions: can_go to tell the program if the player can go in that direction, go to return which room is in that direction, and display to show which directions the player can go.

Next is the map data. This is an array of room structs. The player starts at room #0 and is trying to get to room #8. Each of the numbers in each struct is a n, s, e or w room number, or -1 if you can't go that way.

The main function is pretty simple. The room the player is in is stored in the player variable. All the "checking" work is done in the functions in the room struct.
Your best bet is to come up with a simple system of storing a map in code, and automatically heading to the next room. The simplest method I can think of is this: (You are going to need arrays, structures, and functions.)

struct room{  bool door_north;  bool door_east;  bool door_south;  bool door_west;};room map[10][10];int player_x,player_y;void initialise_map(){  player_x = 5; player_y = 0;  map[5][0].door_north = true;    map[5][0].door_east = false;  map[5][0].door_south = false;  map[5][0].door_west = false;  map[5][1].door_north = true;  map[5][1].door_east = true;  map[5][1].door_south = true;  map[5][1].door_west = false;  map[6][1].door_north = false;  map[6][1].door_east = false;  map[6][1].door_south = false;  map[6][1].door_west = true;}void describe_room(int x, int y){  std::cout<<"Your current room has these doors""<<std::endl;  if(map[x][y].door_north)    std::cout<<"A north door"<<std::endl;  if(map[x][y].door_east)    std::cout<<"An east door"<<std::endl;  if(map[x][y].door_south)    std::cout<<"A south door"<<std::endl;  if(map[x][y].door_west)    std::cout<<"A west door"<<std::endl;}


Get some square dotted paper and draw your map on it, then program the stuff into the initialise_map function. Your game looks first in the room at (player_x,player_y).

Then, depending on which room your player is in, you can present them with the options to go forwards, backward, east or west. ensure the option to go in a direction where there isn't a door is not found. So long as you don't mess up the doors in the initialisation function, you can simply add +/- 1 to the player coordinates, depending on what the player selected. You game loop, therefore, will look like this [psudocode this time]

INT MAIN  initialise()  WHILE player doesn't want to exit    describe_room()    present_options_menu()    take_input()    perform_action()  END WHILEENDVOID perform_action()  moving right:  IF we are actually able to move right    player_x = player_x + 1;  ELSE    tell user to enter a direction we can move at.  END IF  moving left:  IF we are actually able to move right    player_x = player_x - 1;  ELSE    tell user to enter a direction we can move at.  END IF  moving up:  IF we are actually able to move right    player_y = player_y + 1;  ELSE  tell user to enter a direction we can move at.  END IF  moving down:  IF we are actually able to move right    player_y = player_y - 1;  ELSE    tell user to enter a direction we can move at.  END IF





}


[/source]
Don't thank me, thank the moon's gravitation pull! Post in My Journal and help me to not procrastinate!
Here is another example of an "event driven" game.
Notice how the while loop in main works as the heartbeat of the game.
In this game you navigate through the rooms by typing the room index you want to go to [0, 3]. Any other index will quit the game. Pretty simple. Might not be what you want but it illustrates some basic concepts.
#include <iostream>#include <string>std::string world[] = {	"You are in the first room...",	"You are in the second room...",	"You are in the third room...",	"You are in the forth room...",};int main(){	bool stillRunning = true;	int currentRoom = 0;	while(stillRunning)	{		std::cout << world[currentRoom] << std::endl;		std::cin >> currentRoom;		if(currentRoom > 3 || currentRoom < 0)			stillRunning = false;	}}

This topic is closed to new replies.

Advertisement