C++ is a great programming language to develop games. In fact, I think C++ is the best language to develop games – it is the most popular language in game development industry. You can use several free C++ compilers to create some small games as a console app. In games, mostly we use do-while loops to repeat actions. You can use the C++ switch function too to efficiently check for various conditions during the main game loop. In this article we explain how you can build a very simple console application in C++.
How to program a simple game in C++?
The Walking Person Game
First let’s describe our game, : our game is very simple, our game world is a one-dimensional world. We are on a road, and we can go left or right. Our street has 5 sections 0 to 4. And we have an apple in 0 position and player starts in position 3. We can go Left or Right by the ‘L’ or ‘R’ input, or we can exit from the game by entering ‘X’ as the input. After every move we print a line of text to tell the user if there is an object or any information about that position.
First, Let’s create or main function in C++,
1 2 3 4 5 6 |
#include <iostream> int main() { } |
Inside this main function after the ‘{'
symbol, we should write C++ code that runs inside this main function. First lets declare our variables. we have a char input to go Left or Right or to eXit from the game. We have a street to walk on.
1 2 3 |
char input; // char input to go Left or Right char street[5]; // one dimensional game base int posX = 2; // X position of the player |
Now let’s setup our initial street. We have an apple tree in the position 0, we will use ‘A’ character for this apple tree. And position 2 is center,
1 2 |
street[0] = 'A'; // an apple tree on this position street[2] = 'C'; // center on this position |
Let’s print some information about our game,
1 2 |
std::cout << "Walking Person Game" << std::endl; std::cout << "Input L for the Left, R for the Right, X to Exit" << std::endl << std::endl; |
After this line, inside the main
function, we keep continue coding. Now we should repeat to walk on the street, to the left or right, or may be player wants to exit from this repeat. To do this we use do-while()
loop as below,
1 2 3 4 5 6 |
do { std::cout << "Your position is:" << posX << std::endl; }while( input != 'X' ); |
Inside this loop, after the line that has “Your position is:” prompt, we should give information about that position in accordance with the data in that street position. We can use switch function to switch in actions for each situation as below,
1 2 3 4 5 6 7 8 9 10 |
switch( street[posX]) { case 'A': std::cout << "Here is an apple tree" << std::endl ; break; case 'C': std::cout << "Here is center of the road"<< std::endl ; break; } |
Now we should ask where the player wants to go and we should get input,
1 2 |
std::cout << "Where u go (L, R, X) ?"; std::cin >> input; |
If user inputs ‘L
‘ we should decrease the position of the player (minimum 0) and if the user inputs ‘R
‘ we should increase the position (maximum 5). We can check this input and we can do actions as given below,
1 2 3 4 5 6 7 8 9 10 11 12 13 |
switch(input) { case 'L': case 'l': if(posX>0) posX--; break; case 'R': case 'r': if(posX<5) posX++; break; } |
Here is a full example of how to program a Walking Person game in C++
As a result full example should be as follows,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#include <iostream> int main() { char input; // char input to go Left or Right char street[5]; // one dimensional game base int posX = 2; // X position of the player street[0] = 'A'; // an apple tree on this position street[2] = 'C'; // center on this position std::cout << "Walking Person Game" << std::endl; std::cout << "Input L for the Left, R for the Right, X to Exit" << std::endl << std::endl; do { std::cout << "Your position is:" << posX << std::endl; switch( street[posX]) { case 'A': std::cout << "Here is an apple tree" << std::endl ; break; case 'C': std::cout << "Here is center of the road"<< std::endl ; break; } std::cout << "Where u go (L, R, X) ?"; std::cin >> input; switch(input) { case 'L': case 'l': if(posX>0) posX--; break; case 'R': case 'r': if(posX<5) posX++; break; } }while( input != 'X' ); return 0; } |
Although the free C++ Builder Community Edition is extremely powerful it is intended for students, beginners, and startups. If you do not qualify for the free Community Edition then you can download a free trial of the very latest full RAD Studio C++ Builder version.
C++ Builder is the easiest and fastest C and C++ IDE for building everything from simple to professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder an
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition