C++C++17Game DevelopmentLearn C++

How To Program A Game In C++

How To Program A Game In C++

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

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17Learn C++Templates

Learn How To Eliminate Pointers In Templates in C++

C++C++11C++14C++17Learn C++Templates

Learn To Export And Import Template Members From A DLL Or A Package In C++

C++C++11C++14C++17Learn C++Templates

What Is A Function Template In C++?

C++C++11C++14C++17Learn C++Templates

What Is A Class Template In C++?