Disco // (08-12-2010)
well. This is my first tutorial on c++, normally I can write good tutorials but it is my first on this topic so it may not be the best.
The code for the simple text based game we will be creating is as follows
in a console application I have created the following two files with the following code within them.
functions.h[php]
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void Felse(){
cout << "that wasn't an answer...\n";
}
void test (string a, char c[40], char e[40] ){
if(a == "y"){
cout << c << endl;
}
else if(a == "n"){
cout << e << endl;
}
else { Felse(); }
}[/php]
functions.cpp
[php]#include "functions.h"
int main(){
string b;
char d[40] = "you answered yes";
char f[40] = "you answered no";
cout << "please enter y/n"<< endl;
getline (cin,a);
test(a, c, e);
}[/php]
If you put these in your compiler, it will be a VERY simple text based game.
If this makes sense to you, you shouldn't be reading this. If it doesn't make sense, well you came to the right place. I hope.
Lets take this apart, piece by piece.
The first things we notice in functions.h is [php]#include <iostream>
#include <string>
#include <sstream>[/php]
What this is saying, is to include the iostream.h file which includes input/output streams (cout/cin etc)
to include string, so we can use the string variable which is quite handy and to include sstream so we can use the getline (cin,a); which is far less glitchy the cin >>
as we continue down the code we see [php]using namespace std;[/php]
this handy piece of code makes it so we don't have to add std:: before just about everything. I like that.
Now we get to the confusing part of it... [php]void Felse(){
cout << "that wasn't an answer...\n";
}[/php]
Well, if this were to be void Felse; it would just be a variable with no type declared. But, it's a function with no type declaredbecause its void Felse(){}
what this does, is it makes the code within the '{' '}' run whenever you call Felse();
which MEANS when we calle Felse(); it will basically be replaced by cout <<"that wasn't an answer...\n";
and THAT is what functions is all about (kinda)
[php]void test (string a, char c[40], char e[40] ){
if(a == "y"){
cout << c << endl;
}
else if(a == "n"){
cout << e << endl;
}
else { Felse(); }
}[/php]
now, its void test(string a, char c[40], char e[40]){} now, the strings and chars inside the () are replaced by whatever variables we use when we call this function in our code... more on that later.
Then we have the basic if statements, and what have you.
What you may or may not have noticed, is [php]else { Felse(); }[/php] Yes, thats right. I didn't wanna right cout <<... so I used the Felse function. Really handy and cleans up the code a lot.
Alright, now to expain functions.cpp
[php]#include "functions.h"
int main(){
string b;
char d[40] = "you answered yes";
char f[40] = "you answered no";
cout << "please enter y/n"<< endl;
getline (cin,a);
test(a, c, e);
}[/php]
You may be wondering, WHAT THE HELL1!?!?! but don't worry.
I included function test.h which included everything else we need (chain reaction type thing) By including our own header file, the .cpp file now contains all the code from that header file. Which means, we can use the functions in our header file in our .cpp file. Make sense?
Well, I basically have three variables, a string and two chars. char c is "you answered yes"
char e is "you answered no"
string a has nothing assigned to it yet.
We then use the cout function to display enter y/n then the getline function to get whatever was on that line, take that and put it into a.
get line works a little like cin, just a bit differently. It only works for strings, so you can't do
[php]
int a;
getline (cin,a);
/have to do
string a;
getline (cin,a);
[/php]
and it just inputs whatever was on the line into the variable. quite simple really, not as buggy as cin.
weeellll now we arrive at the functions.
[php]
test(b, d, f);
[/php]
remember how we made the function
[php]
void test (string a, char c[40], char e[40] )
[/php]
well, see how they both have three variables within the brackets? thats important.
when you call a function, whatever is in the brackets replaces any other instance of what is in the brackets when you create the function.
soo, by using test(b,d,f);
we are changing the
[php]
void test (string a, char c[40], char e[40] ){
if(a == "y"){
cout << c << endl;
}
else if(a == "n"){
cout << e << endl;
}
else { Felse(); }
}
[/php]
function too
[php]
void test (string b, char d[40], char f[40] ){
if(b == "y"){
cout << d << endl;
}
else if(b == "n"){
cout << f << endl;
}
else { Felse(); }
}
[/php]
If this STILL does not make sense, please PM me and I'll explain further.
Please, if there is any way I could clean this up please PLEASE tell me. I want to help, not confuse.
Favourite quotes:
Definition of computing: The use or operation of computers.Code:I don't need easy, I just need possible. ~ Me There are three birds on a fence. Two decide to fly away, how many are left? Three, just because you decide to do something doesn't mean you've done it. ~ Don't know who said this Do not go where the path may lead, go instead where there is no path and leave a trail. ~ Ralph Waldo Emerson
Turning on my computer = computing = programming
LAWLFAIL
Disco // (08-12-2010)
Cool
I wouldn't call them Text Based games. Just Console Applications. Text based games would be like Games for a cp/m where its like an rpg just with text
Last edited by Auxilium; 08-07-2010 at 06:48 PM.
Favourite quotes:
Definition of computing: The use or operation of computers.Code:I don't need easy, I just need possible. ~ Me There are three birds on a fence. Two decide to fly away, how many are left? Three, just because you decide to do something doesn't mean you've done it. ~ Don't know who said this Do not go where the path may lead, go instead where there is no path and leave a trail. ~ Ralph Waldo Emerson
Turning on my computer = computing = programming
LAWLFAIL
It'd be a little more than that.
Most text based games were like you choose the direction to go and you get another string of text.
And you'd need to incorporate stuff like classes![]()
shh don't scare me with classes yetevery time I do anything with classes something happens to me... Last time I saw a code with classes in them my mother came home.
But no, you could make an entire RPG text based game without using any functions or classes. It just makes the code A LOT simpler and A LOT cleaner to read.
And yes, this is all you need to make a simple game, perhaps not a fighting game but a lifestyle game.
"Hi there, this is the bus. Where would you like today?
1. Victoria
2. Duncan
3. The Beach"
3
"Alright, off to the beach we goo"
"A lady wearing a yellow poka dot bikini walks up to you."
"Hi, I was wondering if you've seen my shoes anywhere... I've seem to have lost them"
"1. yes"
"2. no"
2
"Oh, sorry to bother you"
Later on I may add in how to make an RPG text based game but I'm kinda half asleep right now.
Favourite quotes:
Definition of computing: The use or operation of computers.Code:I don't need easy, I just need possible. ~ Me There are three birds on a fence. Two decide to fly away, how many are left? Three, just because you decide to do something doesn't mean you've done it. ~ Don't know who said this Do not go where the path may lead, go instead where there is no path and leave a trail. ~ Ralph Waldo Emerson
Turning on my computer = computing = programming
LAWLFAIL
You can't make them without Classes or functions. Period.
未来が見えなくて怖いから
未来が見えてしまって悲しいから
目を閉じて優しい思い出に浸ってしまう
Last edited by Kallisti; 08-09-2010 at 07:36 AM.
未来が見えなくて怖いから
未来が見えてしまって悲しいから
目を閉じて優しい思い出に浸ってしまう
I hope you can understand this
I just came back from my 1337 post ban TODAY
And i came back to MPGH mid july, after i quit during school.
And
You are a jealous ****** who leeches code, then enters the leeched code into VBOTW. Easy enough to understand? I think you are a foreigner who cant read properly
LEECH MORE CODE
Last edited by Kallisti; 08-09-2010 at 10:43 AM.
未来が見えなくて怖いから
未来が見えてしまって悲しいから
目を閉じて優しい思い出に浸ってしまう
I have a couple things to say about this from what i read,
1. [php]void felse;[/php] you said would be a variable with no type. you cant do that in C++
2. I wanna know how [php]getline(cin,shit);[/php] is 'far' less glitchy than [php]cin >> shit;[/php]
未来が見えなくて怖いから
未来が見えてしまって悲しいから
目を閉じて優しい思い出に浸ってしまう
Yeah, I meant to say "which is not allowed..." on the variable with no type.
And I've found that cin >> shit; makes it combine all the lines and doesn't wait for input. Getline works perfectly for me, for other people I've recommended it for so I prefer to use it.
And btw
you absolutely can... it's just a LOT more code... like 100s to maybe even hundreds of thousands more lines... depending on how big of a game it would be.
Last edited by VvITylerIvV; 08-09-2010 at 05:33 PM.
Favourite quotes:
Definition of computing: The use or operation of computers.Code:I don't need easy, I just need possible. ~ Me There are three birds on a fence. Two decide to fly away, how many are left? Three, just because you decide to do something doesn't mean you've done it. ~ Don't know who said this Do not go where the path may lead, go instead where there is no path and leave a trail. ~ Ralph Waldo Emerson
Turning on my computer = computing = programming
LAWLFAIL
1. [php]cin >>[/php] does wait for input,
2. If youre making a REAL game, that would cause spaghetti code and unwanted behavior. And not hundreds or thousands. If you were making a REAL game without classes and functions, code would be close to a million (or more) lines longer.Code:Enter number (waits till number entered and enter is press)And youd still deal with the spaghetti code
未来が見えなくて怖いから
未来が見えてしまって悲しいから
目を閉じて優しい思い出に浸ってしまう