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++,
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.
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,
Let’s print some information about our game,
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,
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,
Now we should ask where the player wants to go and we should get 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,
Here is a full example of how to program a Walking Person game in C++
As a result full example should be as follows,
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