P@c_M@n Posted December 22, 2011 Share Posted December 22, 2011 (edited) So, I thought some people would enjoy this blast from the past. For the final project in my Programming class, i decided to program a text-based game in C++. And this is the result of my hard work. Source Code is included. Tell me if you like it. Game EDIT: By the way, this is also being released under GPL. Edited December 22, 2011 by P@c_M@n Quote Link to comment Share on other sites More sharing options...
Jason Cooper Posted December 22, 2011 Share Posted December 22, 2011 Good start, especially as you are just learning to program. It is definitely more complex than people usually tackle for a final project in a programming class. A piece of constructive criticism, for the next version look at separating the game data from the game engine (i.e. load the data from your events, rooms, items, etc. from data files). That way you can extend, develop and fix the game or even reuse your game engine for a completely different game without having to alter the engine itself. Quote Link to comment Share on other sites More sharing options...
P@c_M@n Posted January 2, 2012 Author Share Posted January 2, 2012 Thanks Morfir. I got top marks for it in my class. And Jason, are you suggesting putting data in external files? And then loading it when i need to? Quote Link to comment Share on other sites More sharing options...
Jason Cooper Posted January 2, 2012 Share Posted January 2, 2012 And Jason, are you suggesting putting data in external files? And then loading it when i need to? That is exactly it, the main aim is to be able to reuse your engine for more games without having to alter the engine itself each time. It also enables you to then create tools to help you build your game. Creating a simple game with 5 rooms is easy enough to do manually but creating one manually that has 100+ rooms will slowly drive you insane. If you have a tool that lets you create rooms and connect them together on a map then you will be able to concentrate on the game design. Quote Link to comment Share on other sites More sharing options...
P@c_M@n Posted January 10, 2012 Author Share Posted January 10, 2012 (edited) Well i am trying to actually write a load function that will take values from a file and use them to set my coordinates and events. But i am having trouble working with the Room class values. I probably need to do something with pointers (if i dont want to resort to global variables), but i am not very good with them. Could someone help me out? These are the load and main functions: void load(); int main() { Room room11; load(); return 0; } void load() { ifstream load("rm.dat"); int x,y; bool event; if (load.is_open()) { load>>x; load>>y; load>>event; room11.set_coordinates(x,y); room11.set_event(event); } load.close(); } Heres the rm.dat. The first two values are the room coordinates and the second is the boolean value for the Room event: 1 1 1 1 2 0 So, any ideas? Thanks. Edited January 10, 2012 by P@c_M@n Quote Link to comment Share on other sites More sharing options...
Jason Cooper Posted January 10, 2012 Share Posted January 10, 2012 If you don't want to use C style pointers you could just pass your variable as a reference. e.g. load(room11);...void load(&room11){....[code] Quote Link to comment Share on other sites More sharing options...
P@c_M@n Posted January 10, 2012 Author Share Posted January 10, 2012 Thanks Jason. Also I was having some trouble when i tried to eliminate some global variables (namely, x and y). i know that somewhere in the main function, the position of the player is getting set to 0,0. So, this results in the player not being able to move. while(g) { if (player1.get_x()==0 && player1.get_y()==0)//starting point { if (room00.get_event()) { room00.set_exits(true,false,false,false); cout<<"As you venture into the Elderwood you are filled with an ominous feeling."<<endl; room00.get_exits(); console(room00,player1); } else { cout<<"You can't go home! You have to find that package!"<<endl; player1.set_position(0,1); } } if (player1.get_x()==0 && player1.get_y()==1)//bandit attack { system("cls"); room00.set_event(false);//you can't go home at this point. You have to find the package stolen from you room01.set_exits(true,true,true,true); if (room01.get_event()) { cout<<"As you walk down the path of the Elderwood, a pair of bandits leap from the trees, swords drawn!"<<endl<<endl; cout<<"Prepare for battle!"<<endl; battle(player1, 2, Bandit,true); system("cls"); cout<<"As you strike down the last bandit, you feel a wooden club hit you over the back of the head, throwing you to the ground, "; cout<<"throwing you into unconciousness"<<endl; cin.get(); cout<<"You wake up in the clearing where you were attacked. Judging by the position of the sun in the sky, "; cout<<"it has been a few hours since you were knocked unconcious. You check for your sword and other equipment "; cout<<"and panic as you realize that the package you were sent to deliver is missing! "; cout<<"The bandits must have stolen it! But, strangely, all of your other equipment is where you left it. "; cout<<"How strange.... "; room01.set_event(false); } else { cout<<"This is where the bandits attacked you before. The bodies of those you managed to kill lay where you left them. Apparently bandits don't bury their dead."<<endl<<endl; room01.set_mon_num(); battle(player1,room01.get_mon_num(),Goblin, false); system("cls"); } room01.get_exits(); console(room01,player1); } ... and the while loop continues on for all the rooms. This is the console function here. Thanks for all your help Jason. I have been messing with this code for a day or two now and still can't get it to work right. void console(Room room,Player player) { cout<<room.get_description()<<endl; string command; string start; string end; int temp; bool kg=true; cout<<"Please enter your command."<<endl; while(kg) { cout<<"> "; getline(cin,command); if (command.substr(0,2)=="go") { end=command.substr(3); if (room.check_exit(end)) { player.move(end); cout<<player.get_x()<<","<<player.get_y()<<endl; kg=false; } else { cout<<"there is no exit in that direction"<<endl; } } else if (command.substr(0,5)=="stats") { cout<<"Your stats are: "<<endl<<endl; cout<<"STR: "<<player.get_str()<<" "<<"+"<<player.get_str_mod()<<endl; cout<<"DEX: "<<player.get_dex()<<" "<<"+"<<player.get_dex_mod()<<endl; cout<<"CON: "<<player.get_con()<<" "<<"+"<<player.get_con_mod()<<endl; cout<<"INT: "<<player.get_intel()<<" "<<"+"<<player.get_intel_mod()<<endl<<endl; cout<<"HP: "<<player.get_hp()<<"/"<<player.get_max_hp()<<endl; cout<<"AC: "<<player.get_ac()<<endl; cout<<"You have "<<player.get_gold()<<" gold pieces."<<endl; cout<<"Your level is "<<player.get_level()<<" and you have "<<player.get_exp()<<" exp!"<<endl; } else if (command.substr(0,6)=="search") { player.search(room); } else if (command.substr(0,4)=="save") { player.save(room00,room01,room10,room12,room_neg21,room11,room_neg20); } else if (command.substr(0,4)=="rest") { cout<<"You set up camp for the night..."<<endl; player.rest_hp(); cout<<"You wake up feeling refreshed!"<<endl; } else if (command.substr(0,4)=="help" || command.substr(0,1)=="?") { cout<<"*****************COMMANDS*******************"<<endl; cout<<"Stats Displays your stats"<<endl; cout<<"Go <direction> travels in a given direction\n(north,south,east,west)"<<endl; cout<<"Search searches the room for any clues or hidden items"<<endl; cout<<"Exit exits the game"<<endl; cout<<"Save saves your game"<<endl; cout<<"Rest you set up camp for the night,restoring hp. But watch out!"; cout<<" You never know what might be there when you wake up..."<<endl; } else if (command.substr(0,4)=="exit") { cout<<"exiting the game..."<<endl; exit(0); } else if(command.substr(0,12)=="console-exit") //developer administrator command; exits the function instead of just exiting the game { cout<<"exiting console.."<<endl; kg=false; } else { cout<<"invalid string"<<endl; } } } Quote Link to comment Share on other sites More sharing options...
Jason Cooper Posted January 11, 2012 Share Posted January 11, 2012 Variables being set to 0 and you can't spot where; my first instinct is to check all the places you compare that variable to anything else and make sure that you are using double = (eg ' if(x==0)') and not a single = ('if(x=0)'). Both will compile and run but operate very differently. It is a common mistake even for experienced C programmers. If you can't spot anything that way the break out the debugger and start stepping through your running code watching the variables that are being cleared, you should soon find out where they are being cleared and hopefully that will be enough to figure out what the bug is. Quote Link to comment Share on other sites More sharing options...
P@c_M@n Posted January 11, 2012 Author Share Posted January 11, 2012 (edited) Thanks a lot Jason. I figured out what was happening. Apparently I had to pass the player argument to the console() function by reference. After that everything worked great. I will post the result after i get that new system for saving and loading of room variables integrated into the project. Edited January 12, 2012 by P@c_M@n Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.