Programming & Scripting Tutorials

C++: Making A Simple Game





Putting Things Together



This lesson is basically going to bring a lot of the things we learnt in previous lessons together to make a very basic (and very messy and overcomplicated) game. When I first planned this lesson I thought I would walk you through how I created the game, however now I'm thinking about it I will only give you a brief explanation on how I did it, as I know that if you have read through all the previous lessons you know how its all done.

Here is the code for the basic game:

/*
This Application was created by Joe Savage, it can be distributed freely as long as this message stays here at the top of this file.
This program was created entirely for the purposes of Dev-HQ.co.uk
*/
#include <iostream>
#include <ctime>
#include <string>

using namespace std;

class Info{
public:
	Info(){

	}

	void AddOneToScore(){
	Score += 1;
	}


void GameOver(){
system("cls");
cout << "You registered the customers order incorrectly.\n You finished with a score of " << Score << ".\nDo you want to try again? (Y/N)" << endl;
		cin >> GameOverYN;
		if(GameOverYN == "Y" || GameOverYN == "y"){
			system("cls");
			Score = 0;
			Loop = true;
			Loop2 = false;
			cout << "Well thats too bad, because I haven't built a restart system." << endl;
			system("PAUSE");
			exit(0);
		} else {
			Loop = false;
			Loop2 = false;
		}
}

void TimeUp(){
system("cls");
		cout << "You served the customer too slowly and were sacked.\n You finished with a score of " << Score << ".\n Do you want to try again? (Y/N)" << endl;
		cin >> GameOverYN;
		if(GameOverYN == "Y" || GameOverYN == "y"){
			system("cls");
			Score = 0;
			Loop = true;
			Loop2 = false;
			int main();
		} else {
			Loop = false;
			Loop2 = false;
cout << "Well thats too bad, because I haven't built a restart system." << endl;
system("PAUSE");
exit(0);
		}
}

	int Score;
	bool Loop;
	int x;
	string GameOverYN;
	bool Loop2;
};


int main(){

int CustomersOrder;
int Item1;
int Item2;
int i = 0;



cout << "You work at a fast food chain called 'McKentuckyKing'.\nRead the customers orders to see what they want and then proccess their order\nas fast as you can!\n" << endl;
cout << "Key:\n1.Burger\n2.Chips\n3.Chicken Wrap\n4.Chicken Burger\n" << endl;
cout << "This is the key, to process peoples orders you will need to remember the key.. " << endl;
system("PAUSE");
system("cls");
Info MyInfo;
MyInfo.Score = 0;
MyInfo.x = 3250;
clock_t begin=clock();
clock_t end=clock();
time_t t; //New time variable t
time(&t); // Allocate the time to t
srand(t); // Make t rand's seed

for(MyInfo.Loop = true;MyInfo.Loop == true;){ // Beginner Game Loop
i++; //Increment i
CustomersOrder = (rand() % 10 + 1); //Min number is 1, Max number is 10
system("cls"); // Clears the screen

if(CustomersOrder == 1){
cout << "I'll 'ave a burger and some chips please.." << endl;
cout << "*Hint: One by one enter what the customer wants (via the numbers on the key)*" << endl;
begin=clock(); //Set the variable begin to the time
cin >> Item1;
if(Item1 == 1){
	cout << "Burger Registered." << "Input item two: " << endl;
	cin >> Item2;
	if(Item2 == 2){
		end=clock(); //Set the variable end to the time
	if((end - begin) >= 5450){ //Find if they made it on time
	MyInfo.TimeUp();
	} else{
	MyInfo.AddOneToScore();
	}
	} else {
	MyInfo.GameOver();
	}

}else if(Item1 == 2){
cout << "Chips Registered." << "Input item two: " << endl;
cin >> Item2;
if(Item2 == 1){
		end=clock(); //Set the variable end to the time
	if((end - begin) >= 5450){ //Find if they made it on time
	MyInfo.TimeUp();
	} else{
	MyInfo.AddOneToScore();
	}
}else{
	MyInfo.GameOver();
}
}else{
MyInfo.GameOver();
}
}else if(CustomersOrder == 2){
cout << "I will have a chicken wrap please.." << endl;
cout << "*Hint: One by one enter what the customer wants (via the numbers on the key)*" << endl; 
begin=clock(); //Sets the variable begin to the time
cin >> Item1;
if(Item1 == 3){
	cout << "Chicken Wrap Registered." << endl;
end=clock(); //Sets the variable end to the time
	if((end - begin) >= 4400){ //Check If they made it in time
	MyInfo.TimeUp();
	} else{
	MyInfo.AddOneToScore();
	}
} else {
		MyInfo.GameOver();
}
}else if(CustomersOrder == 3){
cout << "Chicken burger and some chips please mate." << endl;
cout << "*Hint: One by one enter what the customer wants (via the numbers on the key)*" << endl;
begin=clock(); //Set the variable begin to the time
cin >> Item1;
if(Item1 == 4){
	cout << "Chicken Burger Registered." << "Input item two: " << endl;
	cin >> Item2;
	if(Item2 == 2){
		end=clock(); //Set the variable end to the time
	if((end - begin) >= 5450){ //Find if they made it on time
	MyInfo.TimeUp();
	} else{
	MyInfo.AddOneToScore();
	}
	} else {
	MyInfo.GameOver();
	}

}else if(Item1 == 2){
cout << "Chips Registered." << "Input item two: " << endl;
cin >> Item2;
if(Item2 == 4){
		end=clock(); //Set the variable end to the time
	if((end - begin) >= 5450){ //Find if they made it on time
	MyInfo.TimeUp();
	} else{
	MyInfo.AddOneToScore();
	}
}else{
	MyInfo.GameOver();
}
}else{
MyInfo.GameOver();
}
}else if(CustomersOrder == 4){
cout << "Umm.. Ill just have some chips please." << endl;
cout << "*Hint: One by one enter what the customer wants (via the numbers on the key)*" << endl; 
begin=clock(); //Sets the variable begin to the time
cin >> Item1;
if(Item1 == 2){
	cout << "Chips Registered." << endl;
end=clock(); //Sets the variable end to the time
	if((end - begin) >= 4400){ //Check If they made it in time
	MyInfo.TimeUp();
	} else{
	MyInfo.AddOneToScore();
	}
} else {
		MyInfo.GameOver();
}
}else if(CustomersOrder == 5){
cout << "Ill 'ave two chicken wraps please, one for me, one for my wife." << endl;
cout << "*Hint: Input each individual item seperately (via the numbers on the key)*" << endl;
begin=clock(); //Set the variable begin to the time
cin >> Item1;
if(Item1 == 3){
	cout << "Chicken Wrap Registered." << "Input item two: " << endl;
	cin >> Item2;
	if(Item2 == 3){
		end=clock(); //Set the variable end to the time
	if((end - begin) >= 5450){ //Find if they made it on time
	MyInfo.TimeUp();
	} else{
	MyInfo.AddOneToScore();
	}
	} else {
	MyInfo.GameOver();
	}
}
}else if(CustomersOrder == 6){
cout << "Just one burger please." << endl;
cout << "*Hint: One by one enter what the customer wants (via the numbers on the key)*" << endl; 
begin=clock(); //Sets the variable begin to the time
cin >> Item1;
if(Item1 == 1){
	cout << "Burger Registered." << endl;
end=clock(); //Sets the variable end to the time
	if((end - begin) >= 4400){ //Check If they made it in time
	MyInfo.TimeUp();
	} else{
	MyInfo.AddOneToScore();
	}
} else {
		MyInfo.GameOver();
}
}else if(CustomersOrder == 7){
cout << "Err. Just a chicken burger please" << endl;
cout << "*Hint: One by one enter what the customer wants (via the numbers on the key)*" << endl; 
begin=clock(); //Sets the variable begin to the time
cin >> Item1;
if(Item1 == 4){
	cout << "Chicken Burger Registered." << endl;
end=clock(); //Sets the variable end to the time
	if((end - begin) >= 4400){ //Check If they made it in time
	MyInfo.TimeUp();
	} else{
	MyInfo.AddOneToScore();
	}
} else {
		MyInfo.GameOver();
}
}else if(CustomersOrder == 8){
cout << "Two chicken burgers please; Im feeling a bit peckish" << endl;
cout << "*Hint: Input each individual item seperately (via the numbers on the key)*" << endl;
begin=clock(); //Set the variable begin to the time
cin >> Item1;
if(Item1 == 4){
	cout << "Chicken Burger Registered." << "Input item two: " << endl;
	cin >> Item2;
	if(Item2 == 4){
		end=clock(); //Set the variable end to the time
	if((end - begin) >= 5450){ //Find if they made it on time
	MyInfo.TimeUp();
	} else{
	MyInfo.AddOneToScore();
	}
	} else {
	MyInfo.GameOver();
	}
}
}else if(CustomersOrder == 9){
cout << "Burger and some chips." << endl;
cout << "*Hint: One by one enter what the customer wants (via the numbers on the key)*" << endl;
begin=clock(); //Set the variable begin to the time
cin >> Item1;
if(Item1 == 1){
	cout << "Burger Registered." << "Input item two: " << endl;
	cin >> Item2;
	if(Item2 == 2){
		end=clock(); //Set the variable end to the time
	if((end - begin) >= 5450){ //Find if they made it on time
	MyInfo.TimeUp();
	} else{
	MyInfo.AddOneToScore();
	}
	} else {
	MyInfo.GameOver();
	}

}else if(Item1 == 2){
cout << "Chips Registered." << "Input item two: " << endl;
cin >> Item2;
if(Item2 == 1){
		end=clock(); //Set the variable end to the time
	if((end - begin) >= 5450){ //Find if they made it on time
	MyInfo.TimeUp();
	}else{
	MyInfo.AddOneToScore();
	}
}else{
	MyInfo.GameOver();
}
}else{
MyInfo.GameOver();
}
}else if(CustomersOrder == 10){
cout << "Chips for me, and a chicken wrap for my friend please mate." << endl;
cout << "*Hint: One by one enter what the customer wants (via the numbers on the key)*" << endl;
begin=clock(); //Set the variable begin to the time
cin >> Item1;
if(Item1 == 3){
	cout << "Chicken Wrap Registered." << "Input item two: " << endl;
	cin >> Item2;
	if(Item2 == 2){
		end=clock(); //Set the variable end to the time
	if((end - begin) >= 5450){ //Find if they made it on time
	MyInfo.TimeUp();
	} else{
	MyInfo.AddOneToScore();
	}
	} else {
	MyInfo.GameOver();
	}

}else if(Item1 == 2){
cout << "Chips Registered." << "Input item two: " << endl;
cin >> Item2;
if(Item2 == 3){
		end=clock(); //Set the variable end to the time
	if((end - begin) >= 5450){ //Find if they made it on time
	MyInfo.TimeUp();
	} else{
	MyInfo.AddOneToScore();
	}
}else{
	MyInfo.GameOver();
}
}else{
MyInfo.GameOver();
}
}
if(i > 15){
system("cls");
cout << "\nWell Done. You have passed the begginer level; Now lets shake things up a bit!\n This time you will have and less time to process the order each time!\n Good Luck! ";
system("PAUSE");
MyInfo.Loop = false;
MyInfo.Loop2 = true;
MyInfo.Score += 5; //Add some points for completing the begginer level
}
}
for(MyInfo.Loop2 = true; MyInfo.Loop2 == true;){ //Makes loop2 true, then asks if its true
CustomersOrder = (rand() % 10 + 1); //Min number is 1, Max number is 10
if(MyInfo.x < 1000){
MyInfo.x = 1000;
} else {
MyInfo.x = MyInfo.x - 10;
}
system("cls"); // Clears the screen

if(CustomersOrder == 1){
cout << "Burger plus some chips please" << endl;
begin=clock(); //Set the variable begin to the time
cin >> Item1;
if(Item1 == 1){
	cout << "Burger Registered." << "Input item two: " << endl;
	cin >> Item2;
	if(Item2 == 2){
		end=clock(); //Set the variable end to the time
	if((end - begin) >= MyInfo.x){ //Find if they made it on time
	MyInfo.TimeUp();
	} else{
	MyInfo.AddOneToScore();
	}
	} else {
	MyInfo.GameOver();
	}

}else if(Item1 == 2){
cout << "Chips Registered." << "Input item two: " << endl;
cin >> Item2;
if(Item2 == 1){
		end=clock(); //Set the variable end to the time
	if((end - begin) >= MyInfo.x){ //Find if they made it on time
	MyInfo.TimeUp();
	} else {
	MyInfo.AddOneToScore();
	}
}else{
	MyInfo.GameOver();
}
}else{
MyInfo.GameOver();
}
}else if(CustomersOrder == 2){
cout << "Gimmie a chicken wrap." << endl;
begin=clock(); //Sets the variable begin to the time
cin >> Item1;
if(Item1 == 3){
	cout << "Chicken Wrap Registered." << endl;
end=clock(); //Sets the variable end to the time
	if((end - begin) >= MyInfo.x){ //Check If they made it in time
	MyInfo.TimeUp();
	} else {
	MyInfo.AddOneToScore();
	}
} else {
		MyInfo.GameOver();
}
}else if(CustomersOrder == 3){
cout << "Please could I have a chicken burger and some chips." << endl;
begin=clock(); //Set the variable begin to the time
cin >> Item1;
if(Item1 == 4){
	cout << "Chicken Burger Registered." << "Input item two: " << endl;
	cin >> Item2;
	if(Item2 == 2){
		end=clock(); //Set the variable end to the time
	if((end - begin) >= MyInfo.x){ //Find if they made it on time
	MyInfo.TimeUp();
	} else {
	MyInfo.AddOneToScore();
	}
	} else {
	MyInfo.GameOver();
	}

}else if(Item1 == 2){
cout << "Chips Registered." << "Input item two: " << endl;
cin >> Item2;
if(Item2 == 4){
		end=clock(); //Set the variable end to the time
	if((end - begin) >= MyInfo.x){ //Find if they made it on time
	MyInfo.TimeUp();
	} else {
	MyInfo.AddOneToScore();
	}
}else{
	MyInfo.GameOver();
}
}else{
MyInfo.GameOver();
}
}else if(CustomersOrder == 4){
cout << "Err.. Ill just have some chips please." << endl;
begin=clock(); //Sets the variable begin to the time
cin >> Item1;
if(Item1 == 2){
	cout << "Chips Registered." << endl;
end=clock(); //Sets the variable end to the time
	if((end - begin) >= MyInfo.x){ //Check If they made it in time
	MyInfo.TimeUp();
	} else{
	MyInfo.AddOneToScore();
	}
} else {
		MyInfo.GameOver();
}
}else if(CustomersOrder == 5){
cout << "Ill have two chicken wraps , I'm feeling a bit peckish." << endl;
begin=clock(); //Set the variable begin to the time
cin >> Item1;
if(Item1 == 3){
	cout << "Chicken Wrap Registered." << "Input item two: " << endl;
	cin >> Item2;
	if(Item2 == 3){
		end=clock(); //Set the variable end to the time
	if((end - begin) >= MyInfo.x){ //Find if they made it on time
	MyInfo.TimeUp();
	} else{
	MyInfo.AddOneToScore();
	}
	} else {
	MyInfo.GameOver();
	}
}
}else if(CustomersOrder == 6){
cout << "Just one burger please." << endl; 
begin=clock(); //Sets the variable begin to the time
cin >> Item1;
if(Item1 == 1){
	cout << "Burger Registered." << endl;
end=clock(); //Sets the variable end to the time
	if((end - begin) >= MyInfo.x){ //Check If they made it in time
	MyInfo.TimeUp();
	} else{
	MyInfo.AddOneToScore();
	}
} else {
		MyInfo.GameOver();
}
}else if(CustomersOrder == 7){
cout << "Umm. Just a chicken burger please" << endl;
begin=clock(); //Sets the variable begin to the time
cin >> Item1;
if(Item1 == 4){
	cout << "Chicken Burger Registered." << endl;
end=clock(); //Sets the variable end to the time
	if((end - begin) >= MyInfo.x){ //Check If they made it in time
	MyInfo.TimeUp();
	} else {
	MyInfo.AddOneToScore();
	}
} else {
		MyInfo.GameOver();
}
}else if(CustomersOrder == 8){
cout << "Two chicken burgers." << endl;
begin=clock(); //Set the variable begin to the time
cin >> Item1;
if(Item1 == 4){
	cout << "Chicken Burger Registered." << "Input item two: " << endl;
	cin >> Item2;
	if(Item2 == 4){
		end=clock(); //Set the variable end to the time
	if((end - begin) >= MyInfo.x){ //Find if they made it on time
	MyInfo.TimeUp();
	} else{
	MyInfo.AddOneToScore();
	}
	} else {
	MyInfo.GameOver();
	}
}
}else if(CustomersOrder == 9){
cout << "Burger and some chips please." << endl;
begin=clock(); //Set the variable begin to the time
cin >> Item1;
if(Item1 == 1){
	cout << "Burger Registered." << "Input item two: " << endl;
	cin >> Item2;
	if(Item2 == 2){
		end=clock(); //Set the variable end to the time
	if((end - begin) >= MyInfo.x){ //Find if they made it on time
	MyInfo.TimeUp();
	} else{
	MyInfo.AddOneToScore();
	}
	} else {
	MyInfo.GameOver();
	}

}else if(Item1 == 2){
cout << "Chips Registered." << "Input item two: " << endl;
cin >> Item2;
if(Item2 == 1){
		end=clock(); //Set the variable end to the time
	if((end - begin) >= MyInfo.x){ //Find if they made it on time
	MyInfo.TimeUp();
	} else {
	MyInfo.AddOneToScore();
	}
}else{
	MyInfo.GameOver();
}
}else{
MyInfo.GameOver();
}
}else if(CustomersOrder == 10){
cout << "Chips and a chicken wrap please mate.." << endl;
begin=clock(); //Set the variable begin to the time
cin >> Item1;
if(Item1 == 3){
	cout << "Chicken Wrap Registered." << "Input item two: " << endl;
	cin >> Item2;
	if(Item2 == 2){
		end=clock(); //Set the variable end to the time
	if((end - begin) >= MyInfo.x){ //Find if they made it on time
	MyInfo.TimeUp();
	} else{
	MyInfo.AddOneToScore();
	}
	} else {
	MyInfo.GameOver();
	}

}else if(Item1 == 2){
cout << "Chips Registered." << "Input item two: " << endl;
cin >> Item2;
if(Item2 == 3){
		end=clock(); //Set the variable end to the time
	if((end - begin) >= MyInfo.x){ //Find if they made it on time
	MyInfo.TimeUp();
	} else {
	MyInfo.AddOneToScore();
	}
}else{
	MyInfo.GameOver();
}
}else{
MyInfo.GameOver();
}
}
}
return 0;
}
Download

As you can see that is a LOT of coding. However it really isnt that complex; In all honesty all I did to create this game is come up with a basic idea, and then start trying to create it with techniques I knew. Half of the code that I started with has either been changed of wiped out in the process of creating my game, as I saw functionality flaws etc. But when I had removed code it was only because I had a better coding idea to replace that one. Also note that I have used system functions, which is unadvised; It would be more suggested to use something like "cin.get()" instead of "system("PAUSE");", there are also other alternatives to the "system("cls");" function.
The program also has many other problems which is why I do not class it as a complete game.

I started of with a system based of bool's in which there was a bool (controlled by a class member function) for the customers order, and a bool for what the user put in; However I then later found that it would be much easier just to check the values the user typed in.
Then I had a notepad for day or two and just came up with more functionality ideas (remember a notepad and pen is your best friend), then I wrote a bit more code (with some quite complex if statements) and seemed to come up with a pretty good basis for the game.

Then I searched around for a while for ways of measuring time, and decided to use ctime as it seemed to be one of the easiest ways, as the ctime clock() function seemed to make it very easy to check the current "time".
After that I was just coding for a day or two to get everything working and to add and remove new features I decided I might add.

Thats about it, thats all I really did. Remember this is not a complete game, its what I would call a "simple game" its sort of a game, yet its not quite got absolutely everything correct and perfected; In fact I suggest you get the code, mess around with it a bit, and then if you come up with a good alternative post it in the forums (release section).

I also suggest you do a little project like this yourself, the hardest bit is actually coming up with an idea! Just get a notepad and pen, sit down for a while and write some ideas then start the basic draft coding!

NOTE: This game is VERY messy, more than anything its just to show you the basic idea of a game loop (which will be covered more in the win32 C++ section).

This C++ tutorial was written by


Back to C++

Advertisement: