Beginner C++ Game Programming: Mastering Variables and Operations

Welcome back to the second tutorial in our beginner C++ game programming series. Today, we’re diving into the exciting world of variables and operations. But before we get started, let’s recap the homework from our previous lesson.

In the last tutorial, I challenged you to solve a problem. If you followed along, you should have achieved a simple transformation effect where a reticle appeared in the bottom right corner of the screen. This effect was achieved by applying the same operation to each X and Y coordinate.

Now, let’s talk about variables and why they are so crucial in programming. A variable is like a box where you can store different types of data. In this case, we’re dealing with integers (int), which are used to store whole numbers.

To declare a variable, simply write the type of variable you want, followed by its name. For example, int X; creates an integer variable named X.

But here’s the thing: when you create a variable, you need to assign a value to it. This is like washing a newly purchased cup before using it. In programming, we don’t want any “germs” of uncertainty in our variables. So, let’s go ahead and assign a value to our variable X, like X = 20;.

Now, we can use this variable in our code. For example, by replacing the hardcoded X values in our put pixel function with X, we can change the position of our reticle by simply modifying the value assigned to X.

But why stop there? Let’s take it to the next level. We can even perform mathematical operations with variables. For example, by adding X with another number, like X + 400, we can dynamically change the position of our reticle on the screen.

Further reading:  3D Modeling a Computer Mouse: A Step-by-Step Tutorial

The same principle applies to subtraction or any other mathematical operation you can think of. And the best part is that by changing just a couple of numbers, you can easily position the reticle anywhere on the screen.

Now, here’s a challenge for you: I want you to rewrite the code for drawing the reticle. Your task is to create a code that allows you to position the reticle anywhere on the screen by modifying just two variables. Use your newfound understanding of variables and operators to complete this challenge.

Don’t worry if you can’t solve it immediately, especially if this is your first programming language. Give it your best shot, and don’t hesitate to move on to the next lesson if you need more practice. Remember, you’re a transformer, and you can do it!

Thank you for watching, and if you enjoyed this video, please don’t forget to like it. Your support means the world to us. Stay tuned for more exciting C++ game programming lessons coming soon!

[Techal]

YouTube video
Beginner C++ Game Programming: Mastering Variables and Operations