C++C++17Game DevelopmentIntroduction to C++Learn C++

How To Learn Game Programming In C++

How To Learn Game Programming In C++

C++ and Delphi are great programming languages to develop everything from simple to very professional level modern applications on Windows, iOS, Android and more. If you are new to programming in C++ generally a small amount of code with some logic is really good introduction into developing a love of coding. In this article we teach you how you can create a very simple guessing game with C++ Builder. The code generates a random number then we use the if else C++ conditional logic command to decide if the user has won or lost.

First let’s describe our game.

How to learn programming in C++ with a simple guessing game?

Our game is about to guessing a number between 1 and 10. In this game the computer (actually this is your application that you will develop) will choose a random number between 1 and 10, and the player will try to guess this number in a single run. At the beginning our application will hold a random number, and it will display some info about the game then it will ask for the user to try and guess the random number. If the user guesses the number correctly, the program will print out “Correct! You Win!”. If the user’s guess is wrong, we will print out “Wrong! You Lost! My random number was ” then the random number that it holds.

How start programming in C++

  1. Create a new Project in C++ Builder CE or in Dev-C++
  2. To have a random number we will use rand() function which generates a number between 0 and RAND_MAX. RAND_MAX is maximum number that is a constant defined in <cstdlib>. We cannot change constant numbers, or we must modify which is not recommended.
    Mathematically, in C or C++ we limit this by using modulo operator with %, for example rand()%10 means random number between 0-10. If we want to hold a number between 1 to 10, we must add 1 to this, so our random number will be like this,

Random numbers are not really true random numbers since they pick a predictable starting point in a pseudo-random sequence. So, each time you run the program theoretically can generate the same pseudo-random sequence of numbers. In some games this is advantage to generate predictable but seemingly random maps, roads in a same random series, so you can make different levels but each level will have same shaped maps or roads. To make sure our random number is truly random we can initialize the ‘random seed’ by using the srand() function as given below,

So now we have a number held in our variable.

3. Let’s keep coding with this information. We need these libraries below listed with #include to use our functions.

4. Now we can focus on our main program, we will code inside the main() procedure. Let’s add some variables for counting steps and random input, we can add generating a random number hold it as an integer randomnumber.

At this step, you can compile by pressing F9 run your code by pressing F10, or you can use Compile and Run buttons in Dev-C++ editor. It should hold a random number and then it will exit.

5. Now we will continue to develop our game. First lets print out some information about game then lets get userinput value as a input,

6. Now we have a user input, we need an if clause logic to decide if it is higher or lower or equal to random number. To do this we should use if () ... else if() ... else() serie as below

7. Finally, let’s modify our full example as below,

8. You can compile by pressing F9 run your code by pressing F10 or F11, or you can use Compile and Run buttons in C++ Builder editor. It should hold a random number, display information and then it will show the number that it holds. Try to guess as few steps as possible.

What does an example programming in C++ look like?

Here is an example output when we catch the number,

Here is an example output when we miss the number,

This is a very simple example, you can randomize number between 1-100 or more, you can give user 5 or more chances with do...while() loops.

In this example, there are other ways to do same game with do... while() loop or with while() loop alone. We hope this example helps you to understand some basics and logics of C++ programming. Here is this example below,=.

Where can I learn more about simple games programming in C++?

If you like this example above, you can check our Game Development section here. There are text-based game examples, 2D and 3D examples, and more methods and techniques can be found in other sections too. In C++ programming, only the device hardware is your limit, you can develop any great game in your C++ apps, because it is a strong, fast, native programming language.

Where can I download a free C++ compiler and IDE to learn programming in C++?

Although the free C++ Builder Community Edition is extremely powerful it is intended for students, beginners, and startups. You can use Dev-C++ too. If you are a regular business or 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.

programming in C++ C++ Builder logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or 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 and there is a trial version you can download from here.

Download RAD Studio 11

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.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?